- Home /
How to prevent touch to propagate through new UI Button?
Hi All,
I have couple of buttons on the screen and unfortunately when I press one of these buttons the touch propagates through to the screen below which then gets handled by a separate Update() function used to handle touches off the UI buttons.
I've tried to utilize the new Eventsystem, but so far with now success. Here's my code:
private bool WasAButton()
{
UnityEngine.EventSystems.EventSystem ct
= UnityEngine.EventSystems.EventSystem.current;
if (! ct.IsPointerOverGameObject() ) return false;
if (! ct.currentSelectedGameObject ) return false;
if (ct.currentSelectedGameObject.GetComponent<Button>() == null )
return false;
return true;
}
void Update ()
{
if (Input.touchCount > 0)
{
if (!WasAButton())
{
Input.ResetInputAxes();
DoSomething();
}
};
}
DoSomething()
gets called even if the touch is on top of a UI button. Buttons work well, but I'd like to make sure that DoSomething() does not get called when pressing a UI button.
Answer by opri · Aug 03, 2015 at 08:44 PM
... and here is the answer that helped me: http://forum.unity3d.com/threads/touch-press-pass-through-all-ui-elements.272892/#post-1818009
Your answer
Follow this Question
Related Questions
How to detect which UI Button was triggered via Script (and not actually clicked)? 0 Answers
Click/touch object to enable/disable buttons inside Canvas 1 Answer
UI: Execute action instead of "Navigation" 0 Answers
How to send mouse events directly to the canvas in world pos 0 Answers
SteamVR Slider won't drag using VR Controller,SteamVR-controller can't drag Slider handle 0 Answers