using UnityEditor.EditorTools; using UnityEngine; namespace UnityEditor.Tilemaps { /// /// An `EditorTool` for handling Rotation for a `GridSelection`. /// public class GridSelectionRotateTool : GridSelectionTool { private static class Styles { public static readonly GUIContent toolbarIcon = EditorGUIUtility.TrTextContentWithIcon("Rotate", "Shows a Gizmo in the Scene view for changing the rotation for the Grid Selection", "RotateTool"); } /// /// Toolbar icon for the `GridSelectionRotateTool`. /// public override GUIContent toolbarIcon => Styles.toolbarIcon; private Quaternion before = Quaternion.identity; /// /// Handles the gizmo for managing Rotation for the `GridSelectionRotateTool`. /// /// Position of the `GridSelection` gizmo. /// Rotation of the `GridSelection` gizmo. /// Scale of the `GridSelection` gizmo. public override void HandleTool(ref Vector3 position, ref Quaternion rotation, ref Vector3 scale) { // Outside of an active drag, align the gizmo with the tile's current rotation // so a rotated tile doesn't snap the handle back to world-aligned each frame. if (GUIUtility.hotControl == 0) before = rotation; EditorGUI.BeginChangeCheck(); var after = Handles.RotationHandle(before, position); if (EditorGUI.EndChangeCheck()) { rotation *= Quaternion.Inverse(before) * after; } before = after; } } }