Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: blacklabgames on September 30, 2015, 03:43:08 AM

Title: Scrollview : Keep selected item in view
Post by: blacklabgames 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?
Title: Re: Scrollview : Keep selected item in view
Post by: ArenMook 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.