I've made a basic UI with the following interface in Unity:
I have a few different states that I want to switch between by pressing buttons - the "in-game" the "menu" and "submenus". There's buttons that can close and open certain menus, and in addition I have the functionality of the escape button doing one of the following: if in-game, esc goes to main menu if in main menu, esc goes to in-game if in one of the sub-menus, esc goes to the menu above it
I implemented this by having a specific menu element structure that I added as a component to each of the in-game, main-menu, sub-menu objects. It contains a pointer/reference called next
, that points to another menu-element. In particular, it points to the object that gives the "esc" functionality above. So for example, the menu-element component on "in-game" contains a pointer to the menu-element component on "main-menu". I also have a static reference current
, that points to the current object that should be active, and at the start of the game, it's set to the main menu.
Now when esc is pressed, the current
object is disabled, the current
is set to `current.next, and then this object is enabled. If in addition the switch was from in-game to main menu, we do additional stuff like turn off the in-game GUI, set time.scale to 0 etc. (and the opposite when going from menu to in-game).
Is all of this reasonable and reliable? Everything works fine, but I have no experience with UI and I don't know if what I did is good practice.
Second - UI obviously has to be reliable. Is it even reliable to do UI like this? How can I assure this is safe? What if I press esc, and current
goes to current.next
, but for some reason the things that should happen during this switch (various objects turning on/off) doesn't happen. It could happen that now I'm stuck in-game with no crosshair, and when I go to menu, the cross-hair will turn on instead. This shows that I should probably determine what the esc button exactly based on what object I'm pointing at currently, and not just say "switch" between true and false on each "esc" button press, as then they could go out of sync.
What are some good practices for making reliable UIs, that will still work and won't cause horrible issues if something goes wrong?