Search code examples
c#unity-game-enginecolliderunity3d-editorsceneview

Showing standard gizmos in editor mode


I'm writing an extension to Unity editor and was wondering if there's a way to show multiplbe gizmos for rescaling/moving colliders in the scene view while in editing mode? I don't want to re-invent the wheel and create custome gizmos (already started my ugly version!) Thank you!


Solution

  • I think you are speaking about Handles.

    In the docs are examples on how t use them e.g. Handles.PositionHandle, Handles.RotationHandle and Handles.ScaleHandle.

    Usually you should use them in OnSceneGUI.

    Vector3 newPosition = Handles.PositionHandle(currentPosition, Quaternion.identity);
    Quaternion newRotation = RotationHandle(currentRotation, position);
    Vector3 newScale = ScaleHandle(currentScale, position, rotation, handleSize);
    

    However I would always recommend to rather use proper SerializedProperty to avoid problems with Undo/Redo and marking things dirty + saving etc.

    Just an example (this won't override the Transform's default handles so it can get confusing)

    using UnityEditor;
    using UnityEngine;
    
    [CustomEditor(typeof(Transform), true, isFallback = false)]
    public class TransformEditor : Editor
    {
        private SerializedProperty position;
        private SerializedProperty rotation;
        private SerializedProperty scale;
    
        private void OnEnable()
        {
            position = serializedObject.FindProperty("m_LocalPosition");
            rotation = serializedObject.FindProperty("m_LocalRotation");
            scale = serializedObject.FindProperty("m_LocalScale");
        }
    
        private void OnSceneGUI()
        {
            serializedObject.Update();
    
            position.vector3Value = Handles.PositionHandle(position.vector3Value, Quaternion.identity);
            rotation.quaternionValue = Handles.RotationHandle(rotation.quaternionValue, position.vector3Value);
            scale.vector3Value = Handles.ScaleHandle(scale.vector3Value, position.vector3Value, rotation.quaternionValue, 1);
    
            serializedObject.ApplyModifiedProperties();
        }
    }