- Home /
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
Your answer
Follow this Question
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