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 3DMagicVR · Jan 20, 2011 at 03:17 AM · positionrect

Rong position using Rect.Contains and GUI.Button

Hi every one, I started a mini project on Unity 3D and encounter this weird situation, when I use the Contains function and the GUI.Button to get the mouse over a button this happens:

The Rect to use with the Contains function is set using this coordinates and size (0, 0, 128, 64), when I test the function the Rect is on the bottom left and the GUI.Button is on the top left, here is the code.

var rect : Rect = Rect(0, 0, 128, 64);

function OnGUI() { if (rect.Contains(Input.mousePosition)) { print("Its over me"); } else { print("Its out"); }

GUI.Button(rect, "Button");

}

So please any one can explain me why is this difference or it's a kind of bug in the program, also when I use the GUI.matrix = Matrix4x4.TRS() to scale the GUI about the screen size, the GUI.Button conserve the position but the rect move away from the button, for your attention thank you very much.

Comment
Add comment · Show 1
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 J3-Gaming · May 03, 2012 at 11:50 PM 0
Share

Then what would the correct variable be for mobile touches? Since Input.touches[0].position; is having the same problem.

1 Reply

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

Answer by Eric5h5 · Jan 20, 2011 at 04:02 AM

Input.mousePosition uses screen space coordinates, which are inverted from GUI coordinates. Don't use anything from Input in OnGUI, use Event.current instead, like Event.current.mousePosition. Then you get data appropriate for OnGUI.

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 3DMagicVR · Jan 20, 2011 at 05:08 AM 0
Share

Thanks for you reply, but my question is about the Rect struct, why if I use the same Rect var to draw a GUI.Button this have another position on the screen when drag the game float window to have a different size (the button keep his position but the Rect dont).

avatar image Eric5h5 · Jan 20, 2011 at 09:53 AM 1
Share

I already answered that. "Input.mousePosition uses screen space coordinates, which are inverted from GUI coordinates."

avatar image 3DMagicVR · Jan 21, 2011 at 09:57 PM 0
Share

Eric5h5, you are right, my mistake, it's just I don't really understand that before like I said before I'm new on the unity stuff, once again thanks to your reply, that helps me a lot.

avatar image Alexis100 · Sep 14, 2011 at 02:19 PM 0
Share

What about do something only if you drag over the area? I try to deter$$anonymous$$e if there is a drag in that area. I wrote this code, but i never get "it is draging!",just the opposite. It works for other events,like click,but I want for drag. Any ideas?, Eric5h5 :D ?

if(swip_area.Contains(Event.current.mousePosition)) if(Event.current.type == EventType.$$anonymous$$ouseDrag) { print("It is draging"); } else{ print("It is not draging!"); }

avatar image Alexis100 · Sep 14, 2011 at 02:39 PM 0
Share

Found! Just invert: if(Event.current.type == EventType.$$anonymous$$ouseDrag){ if(swip_area.Contains(Event.current.mousePosition)) .....//code//.... }

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

2 People are following this question.

avatar image avatar image

Related Questions

Limiting Game Object position by rect 1 Answer

Rect Transform local position to gameobject position 1 Answer

Inspector Rect position 2 Answers

Camera rotation around player while following. 6 Answers

Convert Input.mousePosition to RectTransform pivot position 2 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