After reading your comment. What you need is EventSystem.current.IsPointerOverGameObject() which checks if pointer is over UI. true
when pointer is over UI, otherwise false
. You can use it with '!
' and run your rotation code only if the mouse is not over the UI.
For Desktop
if(Input.GetMouseButtonUp(0) && !EventSystem.current.IsPointerOverGameObject())
{
//Your code here
}
// For Mobile
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended && !EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
{
//Your code here
}
The solution above should work but there is a bug. When the mouse button is pressed over the UI Button
then released outside of the UI Button
, it will register as a click. So, basically, the solution works only when you click on UI Button
and release on UI Button
. If you release outside the U Button
, the original problem will be back again.
The best solution is to use a temporary boolean
value and check if button was originally pressed on a UI with Input.GetMouseButtonDown
. That saved boolean
, you can then use when the Button
is released with Input.GetMouseButtonUp(0)
. The solution below is provided to work with both Mobile and Desktop. Mobile tested and it works.
bool validInput = true;
private void Update()
{
validateInput();
#if UNITY_STANDALONE || UNITY_EDITOR
//DESKTOP COMPUTERS
if (Input.GetMouseButtonUp(0) && validInput)
{
//Put your code here
Debug.Log("Valid Input");
}
#else
//MOBILE DEVICES
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended && validInput)
{
//Put your code here
Debug.Log("Valid Input");
}
#endif
}
void validateInput()
{
#if UNITY_STANDALONE || UNITY_EDITOR
//DESKTOP COMPUTERS
if (Input.GetMouseButtonDown(0))
{
if (EventSystem.current.IsPointerOverGameObject())
validInput = false;
else
validInput = true;
}
#else
//MOBILE DEVICES
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
validInput = false;
else
validInput = true;
}
#endif
}