1+ #if UNITY_EDITOR
2+ using System . Collections . Generic ;
3+ using UnityEditor ;
4+ using UnityEngine ;
5+
6+ namespace DCFApixels . Notes . Editors
7+ {
8+ [ CustomEditor ( typeof ( NoteArrow ) ) ]
9+ [ InitializeOnLoad ]
10+ public class NoteArrowEditor : Editor
11+ {
12+ private const float arrowHeight = 0.0085f ;
13+ [ DrawGizmo ( GizmoType . Active | GizmoType . NonSelected ) ]
14+ static void DrawGizmo ( NoteArrow obj , GizmoType type )
15+ {
16+ if ( obj . Target == null )
17+ return ;
18+
19+ if ( ! _arrows . Contains ( obj ) )
20+ _arrows . Add ( obj ) ;
21+ }
22+ private static HashSet < NoteArrow > _arrows = new HashSet < NoteArrow > ( ) ;
23+ private static HashSet < NoteArrow > _removedArrows = new HashSet < NoteArrow > ( ) ;
24+ static NoteArrowEditor ( )
25+ {
26+ SceneView . duringSceneGui += SceneView_duringSceneGui ;
27+ }
28+ private static void SceneView_duringSceneGui ( SceneView scene )
29+ {
30+ if ( Event . current . type == EventType . Repaint )
31+ {
32+ Camera camera = scene . camera ;
33+ bool isOrthographic = camera . orthographic ;
34+ _removedArrows . Clear ( ) ;
35+ foreach ( var item in _arrows )
36+ {
37+ if ( item == null || ! item . gameObject . activeInHierarchy )
38+ _removedArrows . Add ( item ) ;
39+ }
40+ _arrows . SymmetricExceptWith ( _removedArrows ) ;
41+ foreach ( var obj in _arrows )
42+ {
43+ if ( obj . Target == null )
44+ return ;
45+
46+ Color color = Color . white ;
47+ if ( obj . TryGetComponent ( out INote inote ) )
48+ color = inote . Color ;
49+
50+ Vector3 startPoint = obj . transform . position ;
51+ Vector3 endPoint = obj . Target . position ;
52+
53+ Color defaultColor = Handles . color ;
54+
55+ Handles . color = color ;
56+
57+ Vector3 toCameraDirection ;
58+ if ( isOrthographic )
59+ {
60+ Plane plane = new Plane ( Vector3 . up , startPoint ) ;
61+ plane . SetNormalAndPosition ( camera . transform . forward , camera . transform . position ) ;
62+ var cp = plane . ClosestPointOnPlane ( startPoint ) ;
63+ float distacne = Vector3 . Distance ( cp , startPoint ) ;
64+ toCameraDirection = - camera . transform . forward * distacne * 2f ;
65+ }
66+ else
67+ {
68+ toCameraDirection = camera . transform . position - startPoint ;
69+ }
70+
71+ float height = arrowHeight * toCameraDirection . magnitude ;
72+
73+ float startOffset = 0.02f * toCameraDirection . magnitude ;
74+ float endOffset = 0.2f ;
75+
76+ Vector3 direction = endPoint - startPoint ;
77+ Quaternion q = Quaternion . LookRotation ( toCameraDirection , direction ) ;
78+
79+ endPoint -= direction . normalized * endOffset ;
80+ startPoint = startPoint + q * ( Vector3 . up * startOffset ) ;
81+
82+ Vector3 startPoint1 = startPoint + q * ( Vector3 . left * height / 2f ) ;
83+ Vector3 startPoint2 = startPoint + q * ( Vector3 . right * height / 2f ) ;
84+
85+ Handles . DrawLine ( startPoint , endPoint ) ;
86+ Handles . DrawLine ( startPoint1 , endPoint ) ;
87+ Handles . DrawLine ( startPoint2 , endPoint ) ;
88+
89+ Handles . color = defaultColor ;
90+ }
91+ }
92+ }
93+
94+ public override void OnInspectorGUI ( )
95+ {
96+ var targetProp = serializedObject . FindProperty ( "_target" ) ;
97+
98+ NoteArrow target = this . target as NoteArrow ;
99+ Color color = Color . white ;
100+ if ( target . TryGetComponent ( out INote inote ) )
101+ color = inote . Color ;
102+
103+ Color defaultBackgroundColor = GUI . backgroundColor ;
104+ GUI . backgroundColor = color ;
105+
106+ Rect rect = new Rect ( 0 , 0 , EditorGUIUtility . currentViewWidth , EditorGUIUtility . singleLineHeight + EditorGUIUtility . standardVerticalSpacing * 2 + 9 ) ;
107+ EditorGUI . DrawRect ( rect , color ) ;
108+
109+ float labelWidth = EditorGUIUtility . labelWidth ;
110+ EditorGUIUtility . labelWidth = 0 ;
111+
112+ EditorGUI . BeginChangeCheck ( ) ;
113+ EditorGUILayout . PropertyField ( targetProp ) ;
114+ if ( EditorGUI . EndChangeCheck ( ) )
115+ serializedObject . ApplyModifiedProperties ( ) ;
116+
117+ GUI . backgroundColor = defaultBackgroundColor ;
118+ }
119+ }
120+ }
121+ #endif
0 commit comments