• 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
Question by Redmoth · Mar 31, 2013 at 07:35 PM · c#guirtsselection

C# converting Input.Mouseposition.y to to "top" argument of GUI.Box

Note: There are some syntax errors in the code i wrote as example, this is just pseudo code, I posted my own code from my script under it. This question may seem long, but its actually fairly short, I just wrote alot of stuff to put you guys in context.

Hi guys, I'm coding a box selection system, and I want to display my box on screen by a GUI.Box. However, the arguments for Input.mouseposition start from the bottom left of the screen, E.G:

 Vector2 MousePos = Input.MousePosition;
 If your mouse is at the bottom left of screen it will give:
 MousePos = (0,0)

And the Arguments for GUI.Box are:

 (Left, Top, Width, Height)
 
 So if you enter your MousePos of (0,0) (bottom left) in there:
 
 GUI.Box(new GUI.Box(MousePos.x (0), MousePos.y (0), 100, 100));


This will start the box from the top left instead of the bottom left, since the origin of the box starts from the top of the screen and the origin of input.mouseposition starts from the bottom of the screen!

Add all of this together, if my MousePos.y is 100, the box will not start 100 units from the bottom of the screen, but 100 units from the top of the screen, mirroring the Y position of the mouse on the horizontal axis of the screen!

My question is: How can I manage to get the "Top" argument from my GUI.Box to match the Input.MousePosition.y on my game screen?

Here is my code:

     void Update () {
         
         
         if(Input.GetButton("Fire1")){
             
             if(LeftMouseDown == false){
                 LeftMouseDown = true;
                 mouseOriginalPos = Input.mousePosition;
             }
             mouseCurrentPos = Input.mousePosition;
             SelectionBox = new Rect(Mathf.Min(mouseOriginalPos.x, mouseCurrentPos.x), Mathf.Min(mouseOriginalPos.y, mouseCurrentPos.y),
                                     Mathf.Abs(mouseOriginalPos.x - mouseCurrentPos.x), Mathf.Abs(mouseOriginalPos.y - mouseCurrentPos.y));
             
         }
 
     void OnGUI(){
         
         if(LeftMouseDown == true)
             GUI.Box(new Rect(mouseOriginalPos.x, mouseOriginalPos.y, mouseCurrentPos.x - mouseOriginalPos.x, mouseOriginalPos.y - mouseCurrentPos.y), "");    
         
         Debug.Log(mouseOriginalPos.y);
     }

the SelectionBox Rect variable is in no way affecting my GUI.Box

LeftMouseDown is a bool variable that I reset to false once i release my mouse button. Notice that I directly enter mouseOriginalPos.y as the "Top" argument of the box, resulting in this "mirror" effect of the mouse position and top of the box.

Thank you very much for anyone who can help!

Comment

People who like this

0 Show 0
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
Best Answer

Answer by whydoidoit · Mar 31, 2013 at 07:36 PM

You need to do all of your mouse calculations in the GUI if you want to use it in OnGUI. You can get the mouse presses and the current position of the mouse from Event.current. The position of the mouse in Input is not relevant to the position of t$$anonymous$$ngs in OnGUI - but the Event's mouse positions are.

Comment
atifmahmood29

People who like this

1 Show 3 · 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 Redmoth · Mar 31, 2013 at 07:46 PM 0
Share

Thanks for the quick answer, I accepted this as the answer for my question, however, I would like to ask how you go about writing code that does what you just said?

Do I get a variable, for example: public vector2 GUIMousePos;

and then set it to Event.Current.MousePosition right after my

Onbutton("fire1") in update?

Scratch that, I tried and it always returns 0 as the y position of the mouse

Any Ideas? :)

avatar image whydoidoit · Mar 31, 2013 at 07:53 PM 0
Share

So if Event.current.type == EventType.mouseDown then you need to do your selection box stuff, in OnGUI, with Event.current.mousePosition

avatar image Redmoth · Mar 31, 2013 at 07:56 PM 0
Share

Ahaa, yes, that clears everything up for me, thank you very much!! :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Drawing a GUI box by specifying the two corners 1 Answer

rts building placement 1 Answer

x0,y0 doesn't always point to the bottom left corner 1 Answer

Selection box not working 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