usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;usingUnityEngine.EventSystems;usingUnityEngine.UI;publicclassSubmenu:MonoBehaviour{ [System.NonSerialized]publicSubmenuItemSelectedItem;SelectablefirstAvailable;publicSubmenuItem[]Items;GameObject[]objects;publicSelectablePrevious;publicSelectableNext;publicSpriteInactiveFrame;publicSpriteClearFrame;EventSystemevt;publicenumButtonDirection{Horizontal,Vertical}publicButtonDirectionDirection;privatevoidStart(){InitChildren();}boolhasInit=false;publicvoidInitChildren(){if(!hasInit){for(inti=0;i<Items.Length;i++){InitChild(Items[i]);}RewireControls();hasInit=true;}}voidInitChild(SubmenuItemchild){Debug.Log("Setting "+child.name+" to "+this.name);child.Container=this;child.Selectable.SetNavigationType(Navigation.Mode.Explicit);}voidSetNextAndPrevious(Selectablesel,Selectableprevious,Selectablenxt){if(Direction==ButtonDirection.Horizontal)sel.SetLeftAndRight(previous,nxt);elsesel.SetUpAndDown(previous,nxt);}voidSetPrevious(Selectablesel,Selectableprev){if(Direction==ButtonDirection.Horizontal)sel.SetLeft(prev);elsesel.SetUp(prev);}voidSetNext(Selectablesel,Selectablenxt){if(Direction==ButtonDirection.Horizontal)sel.SetRight(nxt);elsesel.SetDown(nxt);}publicvoidRewireControls(){firstAvailable=null;int?lastIndex=null;for(inti=0;i<Items.Length;i++){// Set enter/exit navigationif(Direction==ButtonDirection.Horizontal)Items[i].Selectable.SetUpAndDown(Previous,Next);elseItems[i].Selectable.SetLeftAndRight(Previous,Next);// Interactable: set this in orderif(Items[i].Selectable.IsInteractable()){// Is first enabled item in the list if(!lastIndex.HasValue){// Clear all the navigationSetNextAndPrevious(Items[i].Selectable,null,null);firstAvailable=Items[i].Selectable;}else{// Set the previous item to the last enabled itemSetNextAndPrevious(Items[i].Selectable,Items[lastIndex.Value].Selectable,null);// Reverse the wiringSetNext(Items[lastIndex.Value].Selectable,Items[i].Selectable);}lastIndex=i;}// Not interactable: remove this from the orderelse{// Clear all the navigationSetNextAndPrevious(Items[i].Selectable,null,null);}}RewireNextAndPrevious();}publicvoidSelectItem(SubmenuItemselected){SelectedItem=selected;RewireNextAndPrevious();}publicvoidForceSelection(SubmenuItemselected){if(evt==null)evt=EventSystem.current;evt.SetSelectedGameObject(null);evt.SetSelectedGameObject(selected.gameObject);//SelectItem(selected);}SelectableSelectedOrFirst{get{return(SelectedItem!=null&&SelectedItem.Selectable!=null)?SelectedItem.Selectable:firstAvailable;}}publicvoidRewireNextAndPrevious(){if(Direction==ButtonDirection.Horizontal){if(Previous!=null)Previous.SetDown(SelectedOrFirst);if(Next!=null)Next.SetUp(SelectedOrFirst);}else{if(Previous!=null)Previous.SetRight(SelectedOrFirst);if(Next!=null)Next.SetLeft(SelectedOrFirst);}}}