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 JaredColl98 · Oct 24, 2012 at 09:07 PM · draw

Help : Need To Draw a Box

I need to know how to draw a box that starts at Input.GetMouseButtonDown(0) and continues to render and change size until Input.GetMouseButtonUp(0). And based on the mouse position. I cant figure out where to start.

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

Answer by phodges · Oct 24, 2012 at 09:47 PM

Here's an example to get you started:

 using UnityEngine;
 
 public class MouseBox : MonoBehaviour {
 
     bool _isValid;    // 
     bool _isDown;
     Vector2 _mouseDownPos;
     Vector2 _mouseLastPos;
     
     // Update is called once per frame
     void Update () {
         // Detect when mouse button is down
         if (Input.GetMouseButtonDown(0)) {
             _mouseDownPos = Input.mousePosition;
             _isDown = true;
             _isValid = true;
         }
 
         // Continue tracking mouse position until the button is raised
         if (_isDown) {
             _mouseLastPos = Input.mousePosition;
             if (Input.GetMouseButtonUp(0)) {
                 _isDown = false;
                 // If you want the box to vanish now, just set 'isValid' to false.
             }
         }
     }
 
     void OnGUI() {
         if (_isValid) {
             // Find the corner of the box
             Vector2 origin;
             origin.x = Mathf.Min(_mouseDownPos.x, _mouseLastPos.x);
             // GUI and mouse coordinates are the opposite way around.
             origin.y = Mathf.Max(_mouseDownPos.y, _mouseLastPos.y);
             origin.y = Screen.height - origin.y;    
             //Compute size of box
             Vector2 size = _mouseDownPos - _mouseLastPos;
             size.x = Mathf.Abs(size.x);
             size.y = Mathf.Abs(size.y);        
             // Draw it!
             Rect r = new Rect(origin.x, origin.y, size.x, size.y);
             GUI.Box(r, "");
         }
     }
 }
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 JaredColl98 · Oct 24, 2012 at 11:47 PM 0
Share

I wrote it in javaScript but all the same it worked Thank you. Here is the code i used

 var isValid : boolean;
 var isDown : boolean;
 
 var mouseLastPos : Vector2;
 var mouseCurrentPos : Vector2;
 
 
 
 function Update () 
 {
 
         if (Input.Get$$anonymous$$ouseButtonDown(0)) 
         {
             mouseLastPos.x = Input.mousePosition.x;
             mouseLastPos.y = Input.mousePosition.y;
             isDown = true;
             isValid = true;
             Debug.Log(mouseLastPos);
         }
         
         if(isDown)
         {
             mouseCurrentPos.x = Input.mousePosition.x;
             mouseCurrentPos.y = Input.mousePosition.y;
             if(Input.Get$$anonymous$$ouseButtonUp(0))
             {
             isDown= false;
             isValid = false;
             }
         
         }
 
 }
 
 function OnGUI()
 {
     if(isValid)
     {
     var boxOrigin : Vector2;
     var boxSize : Vector2;
     
     boxOrigin.x = $$anonymous$$athf.$$anonymous$$in(mouseCurrentPos.x, mouseLastPos.x);
     boxOrigin.y = $$anonymous$$athf.$$anonymous$$ax(mouseCurrentPos.y, mouseLastPos.y);
     boxOrigin.y = Screen.height - boxOrigin.y;
     
     boxSize = mouseLastPos - mouseCurrentPos;
     boxSize.x = $$anonymous$$athf.Abs(boxSize.x);
     boxSize.y = $$anonymous$$athf.Abs(boxSize.y);
     
     GUI.Box(new Rect(boxOrigin.x, boxOrigin.y, boxSize.x, boxSize.y), "This is a box");
     
     }
     
 }
     
avatar image phodges · Oct 25, 2012 at 04:45 AM 0
Share

It looks like an accurate port of the C# I wrote. WIll you accept my answer?

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

10 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

Related Questions

How to draw 3D text from code 4 Answers

Draw to follow 5 Answers

Sudden frame rate drop during rendering 1 Answer

Grass not drawing past 1 metre 0 Answers

Draw a circle on touchscreen 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