Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by IronGirl · Aug 04, 2014 at 09:00 PM · androidcamera

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");
         }
 }
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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)
   {
     ...
     ...
   }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image andrea_romagnoli · Jan 23, 2016 at 09:48 PM 0
Share

This is brilliant! Thank you!

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image IronGirl · Aug 04, 2014 at 09:51 PM 0
Share

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

avatar image
0

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"))
     {
     }
     
     }
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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 :)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lgarczyn · Dec 08, 2019 at 02:04 AM 0
Share

Please use the code formatting tool.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges