|
| 1 | +using UnityEditor; |
| 2 | +using UnityEditor.SettingsManagement; |
| 3 | +using UnityEngine; |
| 4 | +using UnityEngine.Rendering; |
| 5 | + |
| 6 | +namespace ProBuilder.Debug.Editor |
| 7 | +{ |
| 8 | + static class GuidesSettingsProvider |
| 9 | + { |
| 10 | + const string k_PreferencesPath = "Preferences/Scene Guides"; |
| 11 | + static Settings s_Settings; |
| 12 | + public static Settings settings => s_Settings ?? (s_Settings = new Settings("com.unity.scene-guides")); |
| 13 | + |
| 14 | + [SettingsProvider] |
| 15 | + static SettingsProvider CreateSettingsProvider() |
| 16 | + { |
| 17 | + var provider = new UserSettingsProvider(k_PreferencesPath, |
| 18 | + settings, |
| 19 | + new[] { typeof(GuidesSettingsProvider).Assembly }); |
| 20 | + |
| 21 | + settings.afterSettingsSaved += HandleUtility.Repaint; |
| 22 | + |
| 23 | + return provider; |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + class Pref<T> : UserSetting<T> |
| 28 | + { |
| 29 | + public Pref(string key, T value, SettingsScope scope = SettingsScope.Project) |
| 30 | + : base(GuidesSettingsProvider.settings, key, value, scope) { } |
| 31 | + } |
| 32 | + |
| 33 | + static class Guides |
| 34 | + { |
| 35 | + [UserSetting("World Space", "Origin Axes", "Shows 3 axis guides from the scene view origin.")] |
| 36 | + static Pref<bool> s_SceneOrigin = new Pref<bool>("Guides.s_SceneOrigin", false); |
| 37 | + |
| 38 | + [UserSetting("Selection", "Transform Pivot", "Draw a gizmo at the origin of each selected transform.")] |
| 39 | + static Pref<bool> s_SelectionPivot = new Pref<bool>("Guides.s_SelectionPivot", false); |
| 40 | + |
| 41 | + static Vector3 zero = Vector3.zero, |
| 42 | + up = new Vector3(0f, 1f, 0f), |
| 43 | + right = new Vector3(1f, 0f, 0f), |
| 44 | + forward = new Vector3(0f, 0f, 1f); |
| 45 | + |
| 46 | + [InitializeOnLoadMethod] |
| 47 | + static void Init() |
| 48 | + { |
| 49 | + SceneView.duringSceneGui += view => |
| 50 | + { |
| 51 | + var evt = Event.current; |
| 52 | + |
| 53 | + if (evt.type != EventType.Repaint) |
| 54 | + return; |
| 55 | + |
| 56 | + if (s_SceneOrigin) |
| 57 | + { |
| 58 | + var dist = view.camera.farClipPlane / 10f; |
| 59 | + |
| 60 | + DrawLine(zero, right * dist, Handles.xAxisColor); |
| 61 | + DrawLine(zero, right * -dist, Handles.xAxisColor * .7f); |
| 62 | + |
| 63 | + DrawLine(zero, up * dist, Handles.yAxisColor); |
| 64 | + DrawLine(zero, up * -dist, Handles.yAxisColor * .7f); |
| 65 | + |
| 66 | + DrawLine(zero, forward * dist, Handles.zAxisColor); |
| 67 | + DrawLine(zero, forward * -dist, Handles.zAxisColor * .7f); |
| 68 | + } |
| 69 | + |
| 70 | + if (s_SelectionPivot) |
| 71 | + { |
| 72 | + foreach (var transform in Selection.transforms) |
| 73 | + DrawPivot(transform); |
| 74 | + } |
| 75 | + }; |
| 76 | + } |
| 77 | + |
| 78 | + static void DrawPivot(Transform transform) |
| 79 | + { |
| 80 | + using (new Handles.DrawingScope(transform.localToWorldMatrix)) |
| 81 | + { |
| 82 | + DrawLine(zero, right * .2f, Handles.xAxisColor, 1f, 3f); |
| 83 | + DrawLine(zero, up * .2f, Handles.yAxisColor, 1f, 3f); |
| 84 | + DrawLine(zero, forward * .2f, Handles.zAxisColor, 1f, 3f); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + static void DrawLine(Vector3 from, Vector3 to, Color color, float occludedTint = .7f, float thickness = 0f) |
| 89 | + { |
| 90 | + Handles.color = color; |
| 91 | + Handles.zTest = CompareFunction.LessEqual; |
| 92 | +#if UNITY_2021_1_OR_NEWER |
| 93 | + Handles.DrawLine(from, to, thickness); |
| 94 | +#else |
| 95 | + Handles.DrawLine(from, to); |
| 96 | +#endif |
| 97 | + |
| 98 | + Handles.color = color * occludedTint; |
| 99 | + Handles.zTest = CompareFunction.Greater; |
| 100 | +#if UNITY_2021_1_OR_NEWER |
| 101 | + Handles.DrawLine(from, to, thickness); |
| 102 | +#else |
| 103 | + Handles.DrawLine(from, to); |
| 104 | +#endif |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments