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
3
Question by karljj1 · Nov 27, 2013 at 09:07 AM · editorscenehandles

Scene Box/Rectangle Editor. Does one exist?

Hello,

I am trying to create an editor script that lets me resize a 2D box in the environment. Does Unity have a built in box editor I can use such as the one they use on the box colliders? I know it is built internally into the UnityEditor.dll but is it exposed to developer? Here is what I have so far:

 //////////////////////////////////////////////////////////////////
 /// <summary>
 /// Provides a resizable rectangle in the scene for adjusting the height and width.
 /// </summary>
 /// <param name="owner">Transform the rectangle belongs to, needed to apply the rotation and position for the handles.</param>
 /// <param name="size">The size of the rectangle.</param>
 /// <param name="capFunc">Function to use to draw the handles. E.G Handles.ArrowCap</param>
 /// <param name="capCol">Colour of the handles.</param>
 /// <param name="fillCol">Colour of the filled center.</param>
 /// <param name="capSize">Size of the handles.</param>
 /// <param name="snap">Rounds the value val to the closest multiple of snap (snap can only be posiive).</param>
 /// <returns>The new rectangle size.</returns>
 //////////////////////////////////////////////////////////////////
 public static Vector2 ResizeRect( Transform owner, Vector2 size, Handles.DrawCapFunction capFunc, Color capCol, Color fillCol, float capSize, float snap )
 {
     // Calculate the centre for our handles and corners
     Vector2 centre = new Vector2( size.x * 0.5f, size.y * 0.5f );

     //
     // Draw the rect
     //
     Vector3[] corners = { owner.TransformDirection( -centre.x, centre.y, 0 ) + owner.position,   // Top Left
                           owner.TransformDirection( centre.x, centre.y, 0 ) + owner.position,    // Top Right
                           owner.TransformDirection( centre.x, -centre.y, 0 ) + owner.position,    // Bottom Right
                           owner.TransformDirection( -centre.x, -centre.y, 0 ) + owner.position }; // Bottom Left

     //Handles.color = fillCol;
     Handles.DrawSolidRectangleWithOutline( corners, new Color( fillCol.r, fillCol.g, fillCol.b, 0.25f ), capCol );   

     //
     // Draw the handles
     //
     
     // Calculate the 4 control points. Should be the center of each edge.            
     Vector3[] handlePoints = {  Vector3.Lerp( corners[0], corners[3], 0.5f ),   // Left
                                 Vector3.Lerp( corners[1], corners[2], 0.5f ),   // Right
                                 Vector3.Lerp( corners[0], corners[1], 0.5f ),   // Top
                                 Vector3.Lerp( corners[2], corners[3], 0.5f ) }; // Bottom

     // Draw our handles so we can resize the rect
     Handles.color = capCol;
     Vector3 newVec = size;
     newVec += Handles.Slider( handlePoints[0], -owner.right, capSize, capFunc, snap ) - handlePoints[0];
     newVec += Handles.Slider( handlePoints[1], owner.right, capSize, capFunc, snap ) - handlePoints[1];
     newVec += Handles.Slider( handlePoints[2], owner.up, capSize, capFunc, snap ) - handlePoints[2];
     newVec += Handles.Slider( handlePoints[3], -owner.up, capSize, capFunc, snap ) - handlePoints[3];
     
     return newVec;
 }



EDIT:

I now have a working version that I am happy with although I would like to know if Unity does already provide this feature.

 //////////////////////////////////////////////////////////////////
         /// <summary>
         /// Provides a resizable rectangle in the scene for adjusting the height and width.
         /// </summary>
         /// <param name="owner">Transform the rectangle belongs to, needed to apply the rotation and position for the handles.</param>
         /// <param name="size">The size of the rectangle.</param>
         /// <param name="capFunc">Function to use to draw the handles. E.G Handles.ArrowCap</param>
         /// <param name="capCol">Colour of the handles.</param>
         /// <param name="fillCol">Colour of the filled center.</param>
         /// <param name="capSize">Size of the handles.</param>
         /// <param name="snap">Rounds the value val to the closest multiple of snap (snap can only be posiive).</param>
         /// <returns>The new rectangle size.</returns>
         //////////////////////////////////////////////////////////////////
         public static Vector2 ResizeRect( Transform owner, Vector2 size, Handles.DrawCapFunction capFunc, Color capCol, Color fillCol, float capSize, float snap )
         {
             Vector2 centre = new Vector2( size.x * 0.5f, size.y * 0.5f );
 
             Vector3[] corners = { new Vector3( -centre.x, centre.y, 0 ),    // Top Left
                                   new Vector3( centre.x, centre.y, 0 ),     // Top Right
                                   new Vector3( centre.x, -centre.y, 0 ),    // Bottom Right
                                   new Vector3( -centre.x, -centre.y, 0 ) }; // Bottom Left
 
             Handles.matrix = owner.localToWorldMatrix; // Apply the owners matrix so we draw from their position, ori and scale.
             Handles.DrawSolidRectangleWithOutline( corners, new Color( fillCol.r, fillCol.g, fillCol.b, 0.25f ), capCol );
 
 
             // Calculate the 4 control points. Should be the center of each edge.            
             Vector3[] handlePoints =  { new Vector3( -centre.x, 0, 0 ),   // Left
                                         new Vector3( centre.x, 0, 0 ),    // Right
                                         new Vector3( 0, centre.y, 0 ),    // Top
                                         new Vector3( 0, -centre.y, 0 ) }; // Bottom 
             
             Handles.color = capCol;
             Vector3 newVec = size;
             newVec -= Handles.Slider( handlePoints[0], -Vector3.right, capSize, capFunc, snap ) - handlePoints[0]; // Left Handle
             newVec += Handles.Slider( handlePoints[1], Vector3.right, capSize, capFunc, snap ) - handlePoints[1];  // Right Handle
             newVec += Handles.Slider( handlePoints[2], Vector3.up, capSize, capFunc, snap ) - handlePoints[2];     // Top Handle
             newVec -= Handles.Slider( handlePoints[3], -Vector3.up, capSize, capFunc, snap ) - handlePoints[3];    // Bottom Handle
                                    
             // Ensure positive values.
             return Vector3.Max( newVec, Vector3.zero );
         }
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

0 Replies

· Add your reply
  • Sort: 

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

16 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

Related Questions

Rendering a camera to an Editor Window 2 Answers

Issues with Handles not being at a right position 0 Answers

Drawing Handles 1 Answer

Z sorting of Handles 0 Answers

Objects that appear in the editor's game view do not appear in the game build 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