Skip to content

Instantly share code, notes, and snippets.

@yagero
Last active March 18, 2024 06:39
Show Gist options
  • Save yagero/43a51e1c631e7be641d37b4d07b90b73 to your computer and use it in GitHub Desktop.
Save yagero/43a51e1c631e7be641d37b4d07b90b73 to your computer and use it in GitHub Desktop.
Unity UI ButtonSmartNav
using UnityEngine.UI;
// Use this class instead of Unity's default Button
// to automatically skip disabled and inactive Buttons
// if you define your navigation as explicit
public class ButtonSmartNav : Button
{
bool CanReachSelectable(Selectable select)
{
return !select || (select.interactable && select.gameObject.activeInHierarchy);
}
public override Selectable FindSelectableOnLeft()
{
var cursor = base.FindSelectableOnLeft();
while (!CanReachSelectable(cursor)) cursor = cursor.FindSelectableOnLeft();
return cursor;
}
public override Selectable FindSelectableOnRight()
{
var cursor = base.FindSelectableOnRight();
while (!CanReachSelectable(cursor)) cursor = cursor.FindSelectableOnRight();
return cursor;
}
public override Selectable FindSelectableOnUp()
{
var cursor = base.FindSelectableOnUp();
while (!CanReachSelectable(cursor)) cursor = cursor.FindSelectableOnUp();
return cursor;
}
public override Selectable FindSelectableOnDown()
{
var cursor = base.FindSelectableOnDown();
while (!CanReachSelectable(cursor)) cursor = cursor.FindSelectableOnDown();
return cursor;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment