Touch anywhere on screen EXCEPT GUI button Android Unity

I m developing an android game, i have an orthographic camera that i can move left right , up and down with touch , i created a gui button. I export the apk to device when i touch anywhere i can move the camera, but when i touch the gui button that i created, the camera also move. I want when i click on the button .the camera stop moving and when i touch anywhere to the screen the camera move.Or is there a possibility to move the camera when i touch the screen and i double click on the button .I created a boolean [ButtonPressed]but it’s not working the camera move when i also click on GUI Button Here is my code:

Touch touch;
public Vector2 startPos;
Vector2 endPos;
public bool fingerHold = false;
public bool ButtonPressed = false;


void Update()
{
if(!ButtonPressed)
{
  if (Input.touchCount > 0)
    {
       touch = Input.GetTouch(0);
       if (touch.phase == TouchPhase.Began)
       {
          startPos = touch.position;
          fingerHold = true;
       }
       else if (touch.phase == TouchPhase.Moved)
       {
          endPos = touch.position;
       }
       else if (touch.phase == TouchPhase.Ended)
       {
          fingerHold = false;
        }
    }
        if (fingerHold)
        {

            float deltaX = endPos.x - startPos.x;
            float deltaY = endPos.y - startPos.y;
            bool horizontal = false;

            if (Mathf.Abs(deltaX) > Mathf.Abs(deltaY))
                horizontal = true;

            if (horizontal)
            {
                if (deltaX < 0 )
                    transform.Translate(Vector3.left * Time.deltaTime * 20);
                else if (deltaX > 0)
                    transform.Translate(Vector3.right * Time.deltaTime * 20);
            }
            else
            {
                if (deltaY < 0)
                    transform.Translate(Vector3.down * Time.deltaTime * 20);
                else if (deltaY > 0)
                    transform.Translate(Vector3.up * Time.deltaTime * 20);
            }
        }
      }
    }
void OnGUI()
{
if (GUI.Button(new Rect(10, 10, 158, 54), "Click Button"))
        {
           ButtonPressed = true; 
           Print("Button Clicked");
        }
}

Thanks for your help.

///////////////////////////////////////////////////////////////////////////////////////
UPDATE
////////////////////////////////////////////////////////////////////////

Rect buttonRect = new Rect(10, 10, 158, 54);
void Update(){
    if (Input.touchCount > 0)
   {
         touch = Input.GetTouch(0);
         if(buttonRect.Contains(touch.position))return;
         if (touch.phase == TouchPhase.Began)
       	 if (touch.phase == TouchPhase.Began)
        {
          startPos = touch.position;
          fingerHold = true;
        }
        else if (touch.phase == TouchPhase.Moved)
        {
          endPos = touch.position;
        }
        else if (touch.phase == TouchPhase.Ended)
        {
          fingerHold = false;
        }
          fingerHold = false;
        }
    }

        if (fingerHold)
        {

            float deltaX = endPos.x - startPos.x;
            float deltaY = endPos.y - startPos.y;
            bool horizontal = false;

            if (Mathf.Abs(deltaX) > Mathf.Abs(deltaY))
                horizontal = true;

            if (horizontal)
            {
                if (deltaX < 0 )
                    transform.Translate(Vector3.left * Time.deltaTime * 20);
                else if (deltaX > 0)
                    transform.Translate(Vector3.right * Time.deltaTime * 20);
            }
            else
            {
                if (deltaY < 0)
                    transform.Translate(Vector3.down * Time.deltaTime * 20);
                else if (deltaY > 0)
                    transform.Translate(Vector3.up * Time.deltaTime * 20);
            }
        }

void OnGUI()
{
if (GUI.Button(new Rect(10, 10, 158, 54), "Click Button"))
        {
           ButtonPressed = true; 
           Print("Button Clicked");
        }
}
}

Checking the GUI hot control id would solve that issue, i.e. if the value of GUIUtility.hotControl is greater than 0, it means that the user holds down a button. Hence, you could simply modify your if statement as below to bypass the camera movement when the user presses down the button:

void Update()
{
  ...
  ...

  // Check additionally if the GUI button is currently free
  if (GUIUtility.hotControl == 0 && fingerHold)
  {
    ...
    ...
  }
}

Rect buttonRect = new Rect(10, 10, 158, 54);
void Update(){
if (Input.touchCount > 0){
touch = Input.GetTouch(0);
if(buttonRect.Contains(touch.position))return;
// Rest of your code
}
}

If you perform the check right from the beginning of the Update, if the positionis contained in the rect then you will return the method and skip whatever computation is coming next.

using UnityEngine;
using System.Collections;

public class Touch : MonoBehaviour {
	Rect buttonRect = new Rect(10, 10, 158, 54);
	Touch touch;
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.touchCount > 0){
			touch = Input.GetTouch(0);
			if(buttonRect.Contains(touch.position))return;
			//Your Action
		}
	
	}
	void OnGUI()
	{
	
	if(GUI.Button(new Rect(buttonRect),"HI"))
	{
	}
	
	}
}

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
UIController.instance.btnPlayClicked();
}
}
make sure to test it by deploying your build to device or running through unity remote. hope this helps :slight_smile: