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
1
Question by sona.viswam · Mar 18, 2013 at 05:30 AM · guiinputjoystickrect

Input.mouse position on GUI Textures

I have 2 gui textures.

According to screen width and height i kept it in gui.

One for joystick and another for shooter.

Now touching on shooter joystick moves to that specific portion.

i used rect.Contains.

 if (rect.Contains(Input.mousePosition)){
 shootBool = true;
 print("shooter gui Inside");
 alert.text="shooter gui Inside";
 }

Not working properly for me. Think space coordinates are different from gui coordinates. How can fix this problem.

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

2 Replies

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

Answer by robertbu · Mar 18, 2013 at 05:35 AM

I'm guessing you are using Input.mousePosition. This variable is in Screen coordinates. GUI lives in GUI coordinates. You can convert between the two by:

 Pos.y = Screen.height - Pos.y;

But a better solution is to use GUI events. See Event.mousePosition.

Comment
Add comment · Show 10 · 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 Eric5h5 · Mar 18, 2013 at 06:07 AM 0
Share

Don't bother with that, just use Event.current.mousePosition in OnGUI. Then it will be correct. Don't use Input.mousePosition in OnGUI.

avatar image sona.viswam · Mar 18, 2013 at 06:28 AM 0
Share

@Eric5h5: i used

 void Start () {
     xx = Screen.width - Screen.width/12;
     yy = Screen.height - Screen.height/8; 
     lb = Screen.width/10;
     rect = new Rect(-xx/2, -yy/2, lb, lb);
     shooter.pixelInset  = rect;        
     shooter.enabled = false;         
 }
 
 void OnGUI(){
     if(characterScript.playbool){
         shooter.enabled = true;
     }
     
     if (rect.Contains(Event.current.mousePosition)){
     shootBool = true;
     print("shoot");
     alert.text="shoot";
     }        
 }

Still its not working. can someone help me to solve this issue.

avatar image robertbu · Mar 18, 2013 at 06:49 AM 0
Share

Do a debug.Log() on your 'rect'. It is way off the screen, so there is no way for the rect.Contains() to return true.

avatar image robertbu · Mar 18, 2013 at 07:06 AM 0
Share

In Start(), just after you assign the 'rect', do:

 Debug.Log(rect);

When I execute your code on my machine I get the following values for the rect:

 (x:-625.50, y:-591.00, width:136.00, height:136.00)

GUI coordinates begin in the upper left corner of the screen at 0,0 and go to screen width and screen height in the lower right corner. So your 'rect' as specified in this code is not visible.

As for your texture being visible, you don't have anything in the code above that displays a texture, so there is no way for me to check how you setup or use your rect in your GUI.DrawTexture() call.

avatar image robertbu · Mar 18, 2013 at 09:17 AM 2
Share

If the code above is what you are talking about for generating for different devices, then it is flawed, and you need to change it. The 'rect' you specify above is off the screen and therefore 1) will not display a texture, and 2) can never be hit by a mouse event. Here is some more code that works. I've specified a rectangle in the lower left of the device.

 public class Bug6 : $$anonymous$$onoBehaviour {
     float xx, yy, lb;
     Rect rect;
     
     public Texture tex;
 
     void Start () {
         xx = Screen.width/12;
         yy = Screen.height - Screen.height/8 - Screen.width/10; 
         lb = Screen.width/10;
         rect = new Rect(xx, yy, lb, lb);
        Debug.Log(rect);   
     }
     
     void OnGUI() {
         GUI.DrawTexture (rect, tex);
         if (rect.Contains(Event.current.mousePosition)) {
             Debug.Log ("Point in Rect");
         }
     }
 }
Show more comments
avatar image
-1

Answer by programmrzinc · Mar 18, 2013 at 11:54 AM

Since it is GUI,like a GUITexture, You can attach a script to the GUITexture and call OnMouseDown, or OnMouseOver.

 var hovertex : Texture2D;
 var normaltex : Texture2D;
 
 function OnMouseDown(){
 
 print("Pressed");
 
 }
 
 function OnMouseOver(){
 
 print("HOvering");
 
 }
Comment
Add comment · Show 2 · 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 sona.viswam · Mar 18, 2013 at 12:09 PM 0
Share

this is not only just button. its joystick and shooter.

avatar image programmrzinc · Mar 18, 2013 at 01:14 PM 0
Share

I see. Thanks for the clarity.

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

12 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

Related Questions

Simultaneous button presses 2 Answers

Map iOS joystick to the input manager? 2 Answers

reading user's keyboard input inside a GUI Input field and saving it into variables for later use? 2 Answers

Multiple joystick input 2 Answers

Making GuiButton trigger on ps3 controller input 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