- Home /
Distinguish between clicks in 'game area' and on UI elements
I'm making an advance wars clone. For those of you who don't know what that means, look at this image:
That's what the game view currently looks like. Right now I have an input system that uses (if (Input.GetMouseButtonDown(0)) ... ) to select and move elements around.
I want to add an 'end turn' button in the bottom corner of the play area, but I'm concerned that if I click on it, the code within my Input.GetMouseButtonDown(0) block will also run. This might, for example, cause me to simultaneously cause a selected unit to move AND end my turn, which would be bad. Is there any way to prevent this, or am I just wrong in my assumption?
Answer by jprocha101 · Jan 02, 2016 at 07:05 PM
Hi @brentshermana - try this
using UnityEngine.EventSystems;
void Update() {
if (Input.GetMouseButton(0)) {
if (!EventSystem.current.IsPointerOverGameObject()) Debug.Log("over a UI element!");
else Debug.Log("not over UI element!");
}
}
Jprocha, your answer pointed me to the right section of the unity docs, but I couldn't find any definition for the method you have there. Ins$$anonymous$$d, the following worked:
!EventSystem.current.IsPointerOverGameObject()
Would you $$anonymous$$d editing your answer?
Done. Thank you for pointing that out. :)