Block Input.GetMouseButtonDown() when clicking on UI element
Sorry if this has been asked-answered before. I wasn't able to find the right solution yet.
Context: I have a list of images in the screen Canvas. Each image is a slot of a sort of inventory and they have a script attached that implements IPointerClickHandler.
I have a player gameobject with a script which in the "Update" evaluates if Input.GetButtonMouseDown() was triggered to move or attack the player to the point of the terrain clicked.
When I click on one of the images with the script that implements IPointerClickHandler, the script of the player is also triggered, so the player moves, which I don't want to happen. How can I avoid the Input.GetButtonMouseDown() from getting triggered?
I read somewhere about CanvasGroup. Not sure if it is the solution for this, but I tried it with no luck.
Ideas? Thanks
Answer by ADAMN721 · Apr 28, 2017 at 03:31 PM
It sounds like you just need to disable that script when the UI is open. I'm assuming you have a script which handles opening and closing your UI. Just add a reference to the script needing to be disabled and disable it when the UI is opened. IE.
Player.Getcomponent<MoveScript>().enabled = false;
No, this wouldn't work. The UI is always visible at the bottom of the screen. Anyway, there has to be a more elegant solution than this. I believe CanvasGroup could be the way but I couldn't make it work, I was probably using it the wrong way.
If the UI is always visible then you need to be detecting for focus on the UI. So in that case, you need to check for when the player clicks on the UI or however they first interact with it and disable in the same manner. Concept would be the same, just a different trigger depending on how they interact with the UI.
Although thinking about this after the fact it sounds like it could be annoying. If the player is in a fight and accidentally clicks the UI at the bottom and controls become disabled? $$anonymous$$ay want to base this off a key press or something to avoid that miss clicks.
Answer by kkrac · Apr 30, 2017 at 12:10 AM
Can anyone help on this one?
Ins$$anonymous$$d of using : IPointerClickHandler , simply use IPointerDownHandler. IPointerClickHandler takes place AFTER the regular Update, while IPointerDownHandler takes place BEFORE the regular Update.
public void OnPointerDown(PointerEventData ped) { // add content here; }
Sorry for being late to the party, hope this helps :) .
Your answer
Follow this Question
Related Questions
Canvas not filling up the whole screen in Unity 5 0 Answers
Set different sorting layer for each child of a canvas, (Background and UI) 2 Answers
Control UI Input Field entirely with code? 1 Answer
Can i treat multiple image components as a single image? 0 Answers
Problem with Images during Method-Call 0 Answers