Author Topic: Scrollview : Keep selected item in view  (Read 2356 times)

blacklabgames

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 3
    • View Profile
Scrollview : Keep selected item in view
« on: September 30, 2015, 03:43:08 AM »
I'm using a scrollview for a list of save slots. The number of slots is more than can be seen at once. The only input device is a controller.

At any given time, one of the save slots is the UICamera.selectedObject, and the player can use the controller to change the selected object/save slot (using KeyNavigation). However it's possible to select slots that are out of view of the scrollview.

Is there a way to ensure that the selected object is inside the viewable region of the scrollview?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Scrollview : Keep selected item in view
« Reply #1 on: October 01, 2015, 02:12:30 AM »
Something like this:
  1. if (UICamera.selectedObject != null &&
  2.     UICamera.selectedObject.GetComponent<UIWidget>() != null &&
  3.     !UICamera.selectedObject.GetComponent<UIWidget>().isVisible)
  4. {
  5.         UIPanel panel = NGUITools.FindInParents<UIPanel>(UICamera.selectedObject);
  6.         UIScrollView sv = panel.GetComponent<UIScrollView>();
  7.         Vector3 offset = -panel.cachedTransform.InverseTransformPoint(UICamera.selectedObject.transform.position);
  8.         if (!sv.canMoveHorizontally) offset.x = panel.cachedTransform.localPosition.x;
  9.         if (!sv.canMoveVertically) offset.y = panel.cachedTransform.localPosition.y;
  10.         SpringPanel.Begin(panel.cachedGameObject, offset, 6f);
  11. }
...however I suggest adding a way to scroll your list instead. NGUI supports controller scrolling automatically if you specified panning axes on UICamera.