Author Topic: UICenterOnChild limits on either end  (Read 2743 times)

briangibson

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 26
    • View Profile
UICenterOnChild limits on either end
« on: April 05, 2014, 04:42:15 PM »
I've got a horizontal draggable panel that has 5 visible slots, and 10 items from a UIGrid in it.
If I put the UICenterOnChild script on it, and drag all the way to the right, it will center on the first item. In this case, it SHOULD be centering on the 3rd item.
Similarly, if i drag all the way to the left, it centers on the last item. it SHOULD be centering on the 3rd from the last item.

What changes would I need to make to make sure that UICenterOnChild respects the draggable range properly at the extents? If you need an illustration of what I'm talking about, I can take screenshots.
« Last Edit: April 05, 2014, 05:36:22 PM by briangibson »

briangibson

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 26
    • View Profile
Re: UICenterOnChild limits on either end
« Reply #1 on: April 05, 2014, 05:06:17 PM »
Edit: this seems to do it, dunno if it's a good approach. first i added a UIGrid grid to UICenterOnChild, and GetComponent<UIGrid>() in Awake, then:
  1.                 int visibleItems = 0;
  2.                 if(grid.arrangement == UIGrid.Arrangement.Horizontal){
  3.                         visibleItems = Mathf.RoundToInt(mScrollView.panel.width / (float)grid.cellWidth);
  4.                 }
  5.                 else{
  6.                         visibleItems = Mathf.RoundToInt(mScrollView.panel.height / (float)grid.cellHeight);
  7.                 }
  8.                 int halfItemCount = Mathf.FloorToInt(visibleItems / 2.0f);
  9. //then later on...
  10.  
  11.                 if( index < halfItemCount){
  12.                         index = halfItemCount;
  13.                         closest = trans.GetChild(index);
  14.                 }
  15.                 else if(index > trans.childCount - (halfItemCount+1)){
  16.                         index = trans.childCount - (halfItemCount+1);
  17.                         closest = trans.GetChild(index);
  18.                 }
  19.  
  20.  
« Last Edit: April 05, 2014, 05:35:55 PM by briangibson »