Unity touch and ui button
10,180
I think when you touch the gameobject
both of your UI and other gameobjects get affected so you should exclude the times that the user uses UI elements like this,
public class TouchExample : MonoBehaviour {
void Update ()
{
// Check if there is a touch
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
// Check if finger is over a UI element
if(EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
{
Debug.Log("UI is touched");
//so when the user touched the UI(buttons) call your UI methods
}
else
{
Debug.Log("UI is not touched");
//so here call the methods you call when your other in-game objects are touched
}
}
}
}
Author by
Dennis Lu
Updated on June 04, 2022Comments
-
Dennis Lu almost 2 years
Just like the image above, we have a model and a canvas in front of it. It have collider at foots, and i need onclick event in the button also. What i want is the gameobject no action when user click button, but now it's hard to click the button.
Here is my code below:
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) { EventSystem es = EventSystem.current; Debug.Log("IsPointerOverGameObject === " + es.IsPointerOverGameObject(Input.GetTouch(0).fingerId)); Debug.Log("currentSelectedGameObject === " + es.currentSelectedGameObject); if (!(es.IsPointerOverGameObject(Input.GetTouch(0).fingerId) && es.currentSelectedGameObject != null)) { Debug.Log("Handle touch === "); HandleTouchEvent(); } }
Does someone has any ideas?