- Home /
How to trigger specific button at specific condition when player press back button Android
I have multiple button in my scene that I want to trigger when user press back button on Android but at different circumstances.
For example : if user open the shop(a panel), an on-screen button that close the shop will trigger, but when the shop panel isn't opening then a on-screen button that load another scene will trigger.
This is my code
Code (CSharp): private Button butt;
void Start () {
butt=GetComponent<Button>();
}
// Update is called once per frame
void Update () {
if(Input.GetKeyUp (KeyCode.Escape))
butt.onClick.Invoke ();
}
The problem is when I press the back button all of the script get trigger. Anyone know a workaround? Like somehow get the lowest button in the hierarchy(aka the one that on top)?
Ps: In the example I could just add a simple if else
statement but my scenes is pretty complicated and I don't want to hard code everything.
Answer by Voxel-Busters · Aug 16, 2015 at 05:34 PM
Try to make system where you execute only the code which is required. As a simple example,
Consider you have MainMenu and Shop components. When you want to have Shop screen, push the shop as new screen and pause Main Menu.
Another simple approach is to maintain stack for menus and just consider what action needs to be taken based on current top item of the stack (I would recommend going for a good menu system which only runs required code at any instance and pause if code is not used.)
Your answer
Follow this Question
Related Questions
How to handle button highlighted state on Android with touch/mouse/controller? 0 Answers
UI Buttons are Tiny on Android... 2 Answers
Problem with UI sprites in Android build 0 Answers
Can I implement Sorting Layers behavior in new UI? 2 Answers
How to change UI rendering order without changing hierarchy 2 Answers