• 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 alexkaskasoli · Dec 21, 2013 at 07:01 PM · guitexturemousepositioncoordinatesrectangle

Drawing a box with mouse dragged on screen

When a user clicks mouse0 I set a

 Vector2 orgBoxPos = Input.mousePosition;

While he is holding down mouse0, I constantly update

 Vector2 endBoxPos = Input.mousePosition;

I then try this to draw this box on the screen (ie: imagine an RTS style selection box):

 GUI.DrawTexture(new Rect(orgBoxPos.x, Screen.height - orgBoxPos.y, endBoxPos.x - orgBoxPos.x, Screen.height - endBoxPos.y - orgBoxPos.y), selectTexture);

All of the Rect coords are correct except the last one (Remember unity GUI y coords are inverted, for some funky reason).

For some reason I can't seem to figure out how to correctly calculate the end height of my rectangle.

Any help would be greatly appreciated!

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

Answer by alexkaskasoli · Dec 21, 2013 at 07:57 PM

Found it in case anyone needs it:

How to draw a RTS rectangle using Mouse Coordinates

 private Vector2 orgBoxPos = Vector2.zero;
 private Vector2 endBoxPos = Vector2.zero;

....

 if (Input.GetKeyDown(KeyCode.Mouse0)) {
         if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit)) {
 
             orgBoxPos = Input.mousePosition;
         }
         else if (Input.GetKey(KeyCode.Mouse0)) {
             endBoxPos = Input.mousePosition;
         }
 }
 else {
    orgBoxPos = Vector2.zero;
    endBoxPos = Vector2.zero;
 }


...

 void OnGUI() {
  if (orgBoxPos != Vector2.zero && endBoxPos != Vector2.zero) {
     GUI.DrawTexture(new Rect(orgBoxPos.x, Screen.height - orgBoxPos.y, endBoxPos.x - orgBoxPos.x, -1 * ((Screen.height - orgBoxPos.y) - (Screen.height - endBoxPos.y))), selectTexture); // -
  }
 }
Comment
Add comment · 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
1

Answer by df424 · Apr 24, 2016 at 07:36 AM

Not sure if alexkaskasoli's answer works or not, but I think they outer if should be the Input.GetKey() which is called while the key is down. So my solution is.

 /// <summary>
 /// Handles the case where the user draws a rectangle to select some units.
 /// </summary>
 void Update ()
 {
     // Called while the user is holding the mouse down.
     if(Input.GetKey(KeyCode.Mouse0))
     {
         // Called on the first update where the user has pressed the mouse button.
         if (Input.GetKeyDown(KeyCode.Mouse0))
             _box_start_pos = Input.mousePosition;
         else  // Else we must be in "drag" mode.
             _box_end_pos = Input.mousePosition;    
     }
     else
     {
         // Handle the case where the player had been drawing a box but has now released.
         if(_box_end_pos != Vector2.zero && _box_start_pos != Vector2.zero)
             HandleUnitSelection();

         // Reset box positions.
         _box_end_pos = _box_start_pos = Vector2.zero;
     }
 }

 /// <summary>
 /// Draws the selection rectangle if the user is holding the mouse down.
 /// </summary>
 void OnGUI()
 {
     // If we are in the middle of a selection draw the texture.
     if(_box_start_pos != Vector2.zero && _box_end_pos != Vector2.zero)
     {
         // Create a rectangle object out of the start and end position while transforming it
         // to the screen's cordinates.
         var rect = new Rect(_box_start_pos.x, Screen.height - _box_start_pos.y,
                             _box_end_pos.x - _box_start_pos.x,
                             -1 * (_box_end_pos.y - _box_start_pos.y));
         // Draw the texture.
         GUI.DrawTexture(rect, SelectionTexture);
     }
 }
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 jonathan84clark · Dec 21, 2016 at 01:25 PM 0
Share

This worked for me

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

21 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 avatar image avatar image

Related Questions

How to get pixel coordinates from mouse position? 0 Answers

How to make a GUITexture follow your mouse? 1 Answer

Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer

Collision between GUI Texture and Object 1 Answer

Finding the mouse co-ordinate on a texture 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