Skip to content

Instantly share code, notes, and snippets.

@luispedrofonseca
Created October 8, 2016 19:07
Show Gist options
  • Save luispedrofonseca/c81c26f6a30108893387292965d524b2 to your computer and use it in GitHub Desktop.
Save luispedrofonseca/c81c26f6a30108893387292965d524b2 to your computer and use it in GitHub Desktop.
Unity Custom Rect Editor
using UnityEngine;
using UnityEditor;
public class RectUtils
{
public static Rect ResizeRect(Rect rect, Handles.DrawCapFunction capFunc, Color capCol, Color fillCol, float capSize, float snap)
{
Vector2 halfRectSize = new Vector2(rect.size.x * 0.5f, rect.size.y * 0.5f);
Vector3[] rectangleCorners =
{
new Vector3(rect.position.x - halfRectSize.x, rect.position.y - halfRectSize.y, 0), // Bottom Left
new Vector3(rect.position.x + halfRectSize.x, rect.position.y - halfRectSize.y, 0), // Bottom Right
new Vector3(rect.position.x + halfRectSize.x, rect.position.y + halfRectSize.y, 0), // Top Right
new Vector3(rect.position.x - halfRectSize.x, rect.position.y + halfRectSize.y, 0) // Top Left
};
Handles.color = fillCol;
Handles.DrawSolidRectangleWithOutline(rectangleCorners, new Color(fillCol.r, fillCol.g, fillCol.b, 0.25f), capCol);
Vector3[] handlePoints =
{
new Vector3(rect.position.x - halfRectSize.x, rect.position.y, 0), // Left
new Vector3(rect.position.x + halfRectSize.x, rect.position.y, 0), // Right
new Vector3(rect.position.x, rect.position.y + halfRectSize.y, 0), // Top
new Vector3(rect.position.x, rect.position.y - halfRectSize.y, 0) // Bottom
};
Handles.color = capCol;
var newSize = rect.size;
var newPosition = rect.position;
var leftHandle = Handles.Slider(handlePoints[0], -Vector3.right, capSize, capFunc, snap).x - handlePoints[0].x;
var rightHandle = Handles.Slider(handlePoints[1], Vector3.right, capSize, capFunc, snap).x - handlePoints[1].x;
var topHandle = Handles.Slider(handlePoints[2], Vector3.up, capSize, capFunc, snap).y - handlePoints[2].y;
var bottomHandle = Handles.Slider(handlePoints[3], -Vector3.up, capSize, capFunc, snap).y - handlePoints[3].y;
newSize = new Vector2(
Mathf.Max(.1f, newSize.x - leftHandle + rightHandle),
Mathf.Max(.1f, newSize.y + topHandle - bottomHandle));
newPosition = new Vector2(
newPosition.x + leftHandle * .5f + rightHandle * .5f,
newPosition.y + topHandle * .5f + bottomHandle * .5f);
return new Rect(newPosition.x, newPosition.y, newSize.x, newSize.y);
}
}
@groud
Copy link

groud commented Nov 14, 2016

Hello !

Thank you, this has been relly usefull. I however had to modify it a little bit, as it seemed to return a Rect wich had double the actual size on the screen. (I also condensed the code) Here it is :

using UnityEngine;
using UnityEditor;

public class RectUtils {
	public static Rect ResizeRect (Rect rect, Handles.DrawCapFunction capFunc, Color capCol, Color fillCol, float capSize, float snap) {

		Vector3[] rectangleCorners = { 
			new Vector3(rect.min.x, rect.min.y, 0),   // Bottom Left
			new Vector3(rect.max.x, rect.min.y, 0),   // Bottom Right
			new Vector3(rect.max.x, rect.max.y, 0),   // Top Right
			new Vector3(rect.min.x, rect.max.y, 0)    // Top Left
		}; 

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

		Vector3[] handlePoints = { 
			new Vector3(rect.min.x, rect.center.y, 0),   // Left
			new Vector3(rect.max.x, rect.center.y, 0),   // Right
			new Vector3(rect.center.x, rect.min.y, 0),   // Bottom 
			new Vector3(rect.center.x, rect.max.y, 0)    // Top
		}; 

		Handles.color = capCol;

		Vector3 leftHandle =   Handles.Slider(handlePoints[0], Vector3.right, capSize, capFunc, snap);
		leftHandle.x   = Mathf.Min (leftHandle.x,  handlePoints[1].x-0.1f);
		Vector3 rightHandle =  Handles.Slider(handlePoints[1], Vector3.right, capSize, capFunc, snap);
		rightHandle.x  = Mathf.Max (rightHandle.x, handlePoints[0].x+0.1f);
		Vector3 bottomHandle = Handles.Slider(handlePoints[2], Vector3.up,    capSize, capFunc, snap);
		bottomHandle.y = Mathf.Min (bottomHandle.y, handlePoints[3].y-0.1f);
		Vector3 topHandle =    Handles.Slider(handlePoints[3], Vector3.up,    capSize, capFunc, snap);
		topHandle.y    = Mathf.Max (topHandle.y, handlePoints[2].y+0.1f);

		return Rect.MinMaxRect(leftHandle.x, bottomHandle.y, rightHandle.x, topHandle.y); 
	}
}

Thanks again,

GR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment