- Home /
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");
}
}
}
Answer by IronGirl · Sep 02, 2014 at 03:02 PM
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)
{
...
...
}
}
Answer by fafase · Aug 04, 2014 at 09:11 PM
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.
Hello fafase , Thanks for your help , can you see the Update part , i modified the script like this but it doesn't work. Is There something wrong. Thanks a lot for your help
Answer by blenderblender · Jan 11, 2015 at 12:38 PM
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"))
{
}
}
}
Answer by PiranhaStudios · Dec 08, 2019 at 11:04 AM
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 :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613155821im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to open Device camera in full view in unity 3d. 1 Answer
how to open the android camera in unity without using Qualcomm'AR 0 Answers
how to open the android Camera use C# script in unity? 1 Answer
Unity lighting/rendering issues on Android 0 Answers
make android camera video as the background of the unity scene? 0 Answers