Created
September 22, 2025 16:30
-
-
Save emptybraces/4fd61c969fb43bcb69f1fa65ff3f06b6 to your computer and use it in GitHub Desktop.
選択したオブジェクトが移動先を向くように回転するカスタムシーンビューオーバーレイ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace Emptybraces.Editor | |
| { | |
| using UnityEditor; | |
| using UnityEditor.Overlays; | |
| using UnityEditor.Toolbars; | |
| using UnityEngine; | |
| using UnityEngine.UIElements; | |
| [EditorToolbarElement(ID, typeof(SceneView))] | |
| public class AutoFaceToggle : EditorToolbarToggle | |
| { | |
| public const string ID = "Custom/AutoFace/AutoFaceToggle"; | |
| public static bool IsEnabled | |
| { | |
| get => EditorPrefs.GetBool(ID, false); | |
| set => EditorPrefs.SetBool(ID, value); | |
| } | |
| public AutoFaceToggle() | |
| { | |
| text = "Face"; | |
| tooltip = "Rotate to face the movement direction when moved"; | |
| onIcon = EditorGUIUtility.IconContent("d_Transform Icon").image as Texture2D; | |
| offIcon = EditorGUIUtility.IconContent("d_Transform Icon").image as Texture2D; | |
| SetValueWithoutNotify(IsEnabled); | |
| this.RegisterValueChangedCallback(ev => | |
| { | |
| IsEnabled = ev.newValue; | |
| }); | |
| } | |
| } | |
| [Overlay(typeof(SceneView), "AutoFace", defaultDisplay = true)] | |
| public class AutoFaceToolbarOverlay : ToolbarOverlay | |
| { | |
| public AutoFaceToolbarOverlay() : base(AutoFaceToggle.ID) { } | |
| } | |
| [InitializeOnLoad] | |
| public static class AutoFaceHandler | |
| { | |
| static Vector3 _lastPosition; | |
| static AutoFaceHandler() | |
| { | |
| SceneView.duringSceneGui += OnSceneGUI; | |
| } | |
| static void OnSceneGUI(SceneView sceneView) | |
| { | |
| if (!AutoFaceToggle.IsEnabled) return; | |
| if (Selection.activeTransform == null) return; | |
| if (Tools.current != Tool.Move) return; | |
| var t = Selection.activeTransform; | |
| Vector3 delta = t.position - _lastPosition; | |
| if (0.0001f < delta.sqrMagnitude) | |
| { | |
| t.rotation = Quaternion.LookRotation(delta.normalized, Vector3.up); | |
| } | |
| _lastPosition = t.position; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment