Author Topic: scrollview + keynavigation  (Read 2882 times)

hellwalker

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 5
    • View Profile
scrollview + keynavigation
« on: July 10, 2014, 06:00:47 PM »
hello,

I wanted to add keyboard and joystick navigation to my touch based menu.

Basically imagine something like a list of weapons that is generated inside a scrollview. you can scroll through this vertical list and buy or upgrade items.
You know sort of similar to this:
https://lh3.ggpht.com/Hq26HqtgxOsFF9PXIfEWF2aANw9DlgjTGOFQbK4rbpqVKkBWKN0_y7AD7bxRfyVCcw=h900

with keyboard this would work similar to console games interfaces, you navigate scrollview items with Up/Down keys. you hit down bottom item will get centered in scrollview and is now active.

the problem i ran into is how to tie the keynavigation to scrollview items?
how do I detect which inputs where clicked while button was selected? so I can have equip/upgrade options tied to separate joystick button?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: scrollview + keynavigation
« Reply #1 on: July 10, 2014, 09:40:34 PM »
OnKey events get sent to the currently selected object. You can also set UICamera.genericEventHandler to catch all events going out to objects, whether they were handled or not. This would be a good way to react to key events that may or may not be forwarded to a specific object. To center a scroll view on a specific object after making a key press, I would suggest using code similar to what's in UICenterOnClick using Springpanel:
  1.                         UIScrollView sv = panel.GetComponent<UIScrollView>();
  2.                         Vector3 offset = -panel.cachedTransform.InverseTransformPoint(transform.position);
  3.                         if (!sv.canMoveHorizontally) offset.x = panel.cachedTransform.localPosition.x;
  4.                         if (!sv.canMoveVertically) offset.y = panel.cachedTransform.localPosition.y;
  5.                         SpringPanel.Begin(panel.cachedGameObject, offset, 6f);