- Home /
Unity5 UI - How to trigger button click event while preventing menu item deselect event?
So I've got a load game menu using Unity's new UI system, basically just a list of save games you can select with a mouse click along with a couple of buttons at the bottom to delete or load the selected game file.
I've been disabling the Load Game and Delete Game buttons using the interactable attribute whenever a saved game item in the list is deselected:
deleteButton_LoadPanel.GetComponent<Button>().interactable = false;
loadButton_LoadPanel.GetComponent<Button>().interactable = false;
This disables the buttons fine, but when I click on a saved game and then click a button, the deselect event for each save file is triggered BEFORE the onclick event for each button. This is no good.
If I have no deselection event for each individual save file I can click on each save file and then click the load or delete buttons and every thing works fine, but the buttons never get disabled which is not ideal.
My question is: How can I disable the buttons when an item is deselected, but still trigger the onclick event when they are selected? Are there any workarounds to this?
Answer by moment_um · Apr 10, 2016 at 03:54 PM
This might not be the most ideal for you, but you might want to try doing an event trigger of type PointerDown. The OnClick event (or the select event) happens on the release of a button, where as the PointerDown event is called as soon as the pointer is pressed, not released. The bad thing about this is it will remove the keyboard inputs (If you aren't using them then this isn't a problem). This also means that if you click on something you didn't want to... you won't be able to move your mouse before you release to avoid selecting it. If that just won't work for you, it seems like you might need to set a variable when you actually select a save file, and then when you load or delete, just use that saved variable.
Your answer
Follow this Question
Related Questions
How to get component of any button that is clicked? (Using same function for every button) 2 Answers
Highlight images in buttons children 0 Answers
Hidden Canvas, doesn't disable interaction with buttons. 1 Answer
Is Some UI Element Focused or not ? 0 Answers
Augmented reality - UI - How do I augment an User Interface Canvas properly? 0 Answers