Unity Custom Rect Editor

One of the main goals of ProCamera2D’s new extension - Rooms - was to make it as user-friendly as possible.

Each room is basically a rectangle with some extra data to allow custom transitions. To make it easy to edit the rooms the user should be able to interact with them on the scene. However, Unity does not have a built-in rectangle (Rect) editor tool.

Gladly, Unity is very flexible and easy to extend, so I created a simple one. Here you go:

And then, to use it, all you have to do in your custom editor is something like:

using UnityEngine;
using System.Collections;
using UnityEditor;

public class RectExample : MonoBehaviour
{
    public Rect MyRect;
}

[CustomEditor(typeof(RectExample))]
public class RectExampleEditor : Editor
{
    void OnSceneGUI()
    {
        var rectExample = (RectExample)target;

        var rect = RectUtils.ResizeRect(
            rectExample.MyRect,
            Handles.CubeCap,
            Color.green,
            Color.yellow,
            HandleUtility.GetHandleSize(Vector3.zero) * .1f,
            .1f);

        rectExample.MyRect = rect;
    }
}

In the end you get something like this:

Instead of the boring default editor:

Hope you enjoy this little tool and feel free to improve on it!

comments powered by Disqus