- Home /
UGUI keyboard navigation direction for different sounds
Hi all,
I'm wondering if there's a way to listen for specific directions when using UGUI? Basically I want to play different sounds depending on the navigation direction used, but I can't seem to find a way to determine this?
Thanks for any help!
Answer by hexagonius · Aug 02, 2018 at 06:12 AM
you could derive from whatever UI element you want to navigate that derives from Selectable and override the OnMove method. you'll have to do this for every type you navigate. in there, depending on the navigation direction, a method is called that either returns the next selectable in that direction or null. you can hook in there and play a sound depending on direction and or available next selectable. check this out so you know what the default is:
https://bitbucket.org/Unity-Technologies/ui/src/a3f89d5f7d145e4b6fa11cf9f2de768fea2c500f/UnityEngine.UI/UI/Core/Selectable.cs?at=2017.3&fileviewer=file-view-default
the alternative would be to call the methods that get you the UI element in the specified direction the moment you want to navigate. I think this happens in the event system, but In not sure about that, you'll have to look through the UI classes. this would generalize the behaviour from the outside and you don't need the overrides.
This is a good solution! I can override On$$anonymous$$ove and then simply call the base implementation after I deter$$anonymous$$e which sound to play. Thanks!
you don't need the base. just replicate the code that's in there plus your sound code
Calling the base implementation accounts for any potential future updates. Allowing for this where possible is better, in my opinion.