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 frankyboy450 · Dec 15, 2013 at 01:20 PM · c#guitouchbuttonsrect

Problem with rect and touch

Hi. due to unity's GUI system not working great with multi-touch on android devices I had to make a work-around where I create 3 rect that I draw a texture in and check if the player is touching within that rect. but for some reason. it only works with one of the buttons :/

The variables: public Texture redButtonTexture; public Texture greenButtonTexture; public Texture blueButtonTexture;

 public Rect redButtonRect;
 public Rect greenButtonRect;
 public Rect blueButtonRect;

Setting the value of the rects in Start() redButtonRect = new Rect(0, colorButtonHeight * 2, Screen.width / 5, colorButtonHeight);

 greenButtonRect = new Rect(0, colorButtonHeight * 3, Screen.width / 5, colorButtonHeight);
 
 blueButtonRect = new Rect(0, colorButtonHeight * 4, Screen.width / 5, colorButtonHeight);


Drawing the texture in OnGUI()

 GUI.DrawTexture(redButtonRect, redButtonTexture);
 
 GUI.DrawTexture(greenButtonRect, greenButtonTexture);
 
 GUI.DrawTexture(blueButtonRect, blueButtonTexture);


and checking for touch in the Update()

 foreach (Touch touch in Input.touches) 
                 {                    
                     if(redButtonRect.Contains(touch.position))    
                     {
                         Debug.Log ("red");
                     }
                     
                     if(greenButtonRect.Contains(touch.position))    
                     {
                         Debug.Log ("green");
                     }
                     
                     if(blueButtonRect.Contains(touch.position))    
                     {
                         Debug.Log ("blue");
                     }
                 }

For some weird reason, that I can't explain. Then it's only the Red button that works and I can't get the green and blue buttons to work. no matter what I do...

It's possible for me to place GUITexture onto the screen and use them but then I'd have to position them correctly every time the game starts so that the size and position fits the device...

does anyone have a solution to this problem? or can you see what I did wrong in my code?

Thanks

-Frank

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

1 Reply

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

Answer by Tomer-Barkan · Dec 15, 2013 at 01:27 PM

There's this weird thing where the screen position for GUI and for mouse input are reversed...

So for mouse input, (0,0) is bottom left, while for GUI, (0,0) is top left. I'd guess the red one just happens to be in the center of the screen, which is the same for both coordinates.

So you need to convert your input to the same coordinate system as the GUI before checking:

   foreach (Touch touch in Input.touches) 
       {             
           Vector3 inputGuiPosition = touch.position;
           inputGuiPosition.y = Screen.height - inputGuiPosition.y;

           if(redButtonRect.Contains(inputGuiPosition)) 
           {
              Debug.Log ("red");
           }
 
           if(greenButtonRect.Contains(inputGuiPosition))   
           {
              Debug.Log ("green");
           }
 
           if(blueButtonRect.Contains(inputGuiPosition))    
           {
              Debug.Log ("blue");
           }
       }
Comment
Add comment · Show 6 · 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 Mahesh4Unity · Feb 27, 2014 at 07:25 AM 0
Share

@tbkn how can i do it in java script?

avatar image hero_kenshin · Mar 06, 2014 at 05:19 AM 0
Share
 var inputGuiPosition : Vector3;
 inputGuiPosition = touch.position;
 
 inputGuiPosition.y = Screen.height - inputGuiPosition.y;

etc

Only difference between JAVA and C# in this case is the declaration of the variable, which is a simple fix

avatar image AndrewBilotti hero_kenshin · Feb 05, 2016 at 02:42 AM 0
Share

http://answers.unity3d.com/comments/656745/view.html

JAVASCRIPT, not JAVA xD

avatar image AndrewBilotti AndrewBilotti · Feb 05, 2016 at 02:42 AM 0
Share

xdXdxdxdxdxd #SOFUNNY

Show more comments

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

19 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

Related Questions

Rect.contains to replace gui 2 Answers

Main menu help needed (C#) 2 Answers

Wraping text to fit in a gui box 1 Answer

Can't destroy an ui element 2 Answers

Putting Dictionary/List using foreach as buttons in a scroll view? 3 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