Luís Pedro Fonseca

Unity Custom Rect Editor

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!

Unity Custom Rect Editor
Prev post

New site engine — Hugo

Next post

Unite Los Angeles 16

Unity Custom Rect Editor

Get in touch

Feel free to reach me on Twitter or use the form below. I'll usually reply within 48 hours.