Enhance your unity editor with Handles

Overview

Enhance your unity editor with Handles

By using handles within the Unity editor you can make your game editing much easier

This video shows you two ways you can use handles with UI examples and how the code works


Downloads

You can download the the files..
Unity3D package

Show Range


ShowHandles.cs

ShowHandles_UI.cs

Show area


ShowPath.cs

ShowPath_UI.cs


Files

ShowHandles.cs
using UnityEngine;

public class ShowHandles : MonoBehaviour
{
    public float Size = 1;
    public Vector3 LastPos { get; set; } = new Vector3(1, 0, 0);

    public void OnDrawGizmosSelected()
    {
        Gizmos.DrawWireSphere(transform.position, Size);
    }
}


ShowHandles_UI.cs
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(ShowHandles))]
[CanEditMultipleObjects]
public class ShowHandles_UI : Editor
{
    private ShowHandles _node;

    public void OnEnable()
    {
        _node = target.GetComponent();
    }
    public void OnSceneGUI()
    {
        var handlePosition = _node.gameObject.transform.position + _node.LastPos;

        Vector3 newpos = Handles.FreeMoveHandle(
            handlePosition,
            Quaternion.identity, 0.5f, Vector3.zero, Handles.RectangleHandleCap);

        if (newpos != handlePosition)
        {
            _node.LastPos = newpos - _node.gameObject.transform.position;
            _node.Size = _node.LastPos.magnitude;
        }

    }
}




ShowPath.cs
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class ShowPath : MonoBehaviour
{
    public bool EditPath = true;
    public List Nodes = new List();

    public void OnDrawGizmosSelected()
    {
        if (!EditPath)
            return;

        if (!Nodes.Any())
            return;

        Vector3 posLast = Nodes[0];
        
        for (int i = 1; i < Nodes.Count; i++)
        {
            Gizmos.DrawLine(Nodes[i], posLast);
            posLast = Nodes[i];
        }
    }
}


ShowPath_UI.cs
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(ShowPath))]
[CanEditMultipleObjects]
public class ShowPath_UI : Editor
{
    private ShowPath _node;

    public void OnEnable()
    {
        _node = target.GetComponent();
    }

    public void OnSceneGUI()
    {
        if (!_node.EditPath)
            return;

        Vector3 handlePosition = new Vector3(1, 0, 0);

        for (int i = 0; i < _node.Nodes.Count; i++)
        {
            handlePosition = _node.Nodes[i];

            Vector3 newpos = Handles.FreeMoveHandle(
             handlePosition,
             Quaternion.identity, 0.5f, Vector3.zero, Handles.RectangleHandleCap);

            if (newpos != handlePosition)
            {
                newpos.y = 0;
                _node.Nodes[i] = newpos;
            }
        }

        handlePosition.x += 2f;
        Quaternion q = Quaternion.Euler(90, 0, 0);
        if (Handles.Button(handlePosition, q, 0.5f, 0.5f, Handles.CircleHandleCap))
        {
            _node.Nodes.Add(handlePosition);
        }

    }
}