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 hardmode2236 · May 27, 2013 at 06:20 PM · buttonmouserectdrawtexture

texture click detection is off

im trying to setup a texture to be clickable. Before anyone says it, i know that there are button for this. But I'm using texture.

my code looks like this:

 #pragma strict
 
 var background: Texture;
 var tweak: Texture;
 var level: Texture;
 var jam: Texture;
 var gear: Texture;
 var toTheMax: Texture;
 var gearButton: Rect;
 var tweakButton: Rect;
 
 function Start () 
 {
     gearButton = Rect(20,Screen.height/2,Screen.width/6.5,Screen.height/8);
     tweakButton = Rect(Screen.width/6.5+40,Screen.height/2,Screen.width/6.5,Screen.height/8);
 }
 
 function Update () 
 {    
     if(Input.GetMouseButtonUp)
     {
         if(gearButton.Contains(Input.mousePosition))
         {
             Debug.Log("GEAR");
         }
         if(tweakButton.Contains(Input.mousePosition))
         {
             Debug.Log("TWEAK");
         }
         
     }
 }
 
 function FixedUpdate()
 {
     
 }
 
 function OnGUI() 
 {
     GUI.DrawTexture(Rect(0,0,Screen.width, Screen.height),background);
     GUI.DrawTexture(Rect(20,20,Screen.width/3,Screen.height/3),toTheMax);
     GUI.DrawTexture(Rect(20,Screen.height/2,Screen.width/6.5,Screen.height/8),gear);
     GUI.DrawTexture(Rect(Screen.width/6.5+40,Screen.height/2,Screen.width/6.5,Screen.height/8),tweak);
     
 }

with this code i have 2 errors that i cant figure out: 1: the Rects that I'm assigning the 2 public Rect variables to in start seems to be above the actual texture and i cant figure out why, because I'm using the exact same dimensions.

2: when im hovering my mouse over those 2 invisible Rects it does the debug.log even when im not touching the mouse button at all. (i also tried it with GetMouseButtonDown and it still runs the line regardless of the mouse button state)

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 robertbu · May 27, 2013 at 06:33 PM

GUI drawing uses GUI coordinates which start in the upper left corner. Screen coordinates (which are returned by Input.mousePosition) start in the lower right. You can convert between the two by using the GUIUtility class. But a better solution is to use GUI events and detect the click inside of OnGUI():

 #pragma strict
  
 var background: Texture;
 var tweak: Texture;
 var level: Texture;
 var jam: Texture;
 var gear: Texture;
 var toTheMax: Texture;
 var gearButton: Rect;
 var tweakButton: Rect;
  
 function Start () 
 {
     gearButton = Rect(20,Screen.height/2,Screen.width/6.5,Screen.height/8);
     tweakButton = Rect(Screen.width/6.5+40,Screen.height/2,Screen.width/6.5,Screen.height/8);
 }
 
     function OnGUI() 
     {
     var e = Event.current;
     if (e.type == EventType.MouseUp) {
     
            if(gearButton.Contains(e.mousePosition)) {
              Debug.Log("GEAR");
            }
            if(tweakButton.Contains(e.mousePosition)) {
              Debug.Log("TWEAK");
            }
         }
         GUI.DrawTexture(Rect(0,0,Screen.width, Screen.height),background);
         GUI.DrawTexture(Rect(20,20,Screen.width/3,Screen.height/3),toTheMax);
         GUI.DrawTexture(Rect(20,Screen.height/2,Screen.width/6.5,Screen.height/8),gear);
         GUI.DrawTexture(Rect(Screen.width/6.5+40,Screen.height/2,Screen.width/6.5,Screen.height/8),tweak);
      
     }
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 hardmode2236 · Jun 02, 2013 at 11:51 PM 0
Share

oh ok thx for the help

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

13 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

Related Questions

2 Bugs in my game: mouse control takes some time to load and Menu not working. 0 Answers

Button being pressed but an other button gets the effect. 0 Answers

Overlapping GUI Button priority 3 Answers

How can I stop the middle mouse button from zooming out when I single click it inside the editor? 2 Answers

Rect to RectTransform on overlay Canvas? 1 Answer


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