11#if UNITY_EDITOR
2+ using System . IO ;
23using System . Reflection ;
34using UnityEditor ;
5+ using UnityEditorInternal ;
46using UnityEngine ;
57
68namespace DCFApixels . Notes . Editors
@@ -9,40 +11,35 @@ namespace DCFApixels.Notes.Editors
911 internal static class NoteUtility
1012 {
1113 private static string _gizmosPath ;
12- private static GameObject FindRoot ( string name )
13- {
14- GameObject root = GameObject . Find ( name ) ;
15- if ( root == null )
16- {
17- root = new GameObject ( name ) ;
18- root . tag = EDITOR_NAME_TAG ;
19- }
20- return root ;
21- }
14+
2215
2316 #region CreateLazyNote
2417 [ MenuItem ( "GameObject/" + ASSET_SHORT_NAME + "/Create " + nameof ( LazyNote ) + " with arrow" ) ]
2518 public static void CreateLazyNoteWithArrow ( MenuCommand menuCommand )
2619 {
27- GameObject go = CreateLazyNoteInternal ( menuCommand ) ;
28- go . AddComponent < NoteArrow > ( ) ;
29- Undo . RegisterCreatedObjectUndo ( go , "Create " + go . name ) ;
20+ CreateLazyNoteInternal ( menuCommand , true ) ;
3021 }
3122 [ MenuItem ( "GameObject/" + ASSET_SHORT_NAME + "/Create " + nameof ( LazyNote ) ) ]
3223 public static void CreateLazyNote ( MenuCommand menuCommand )
3324 {
34- GameObject go = CreateLazyNoteInternal ( menuCommand ) ;
35- Undo . RegisterCreatedObjectUndo ( go , "Create " + go . name ) ;
25+ CreateLazyNoteInternal ( menuCommand , false ) ;
3626 }
37- private static GameObject CreateLazyNoteInternal ( MenuCommand menuCommand )
27+ private static GameObject CreateLazyNoteInternal ( MenuCommand menuCommand , bool isWithArrow )
3828 {
3929 GameObject go = new GameObject ( nameof ( LazyNote ) ) ;
4030 go . tag = EDITOR_NAME_TAG ;
4131 go . AddComponent < LazyNote > ( ) ;
4232 GameObjectUtility . SetParentAndAlign ( go , menuCommand . context as GameObject ) ;
4333 if ( go . transform . parent == null )
44- go . transform . parent = FindRoot ( NOTES_ROOT_NAME ) . transform ;
34+ {
35+ go . transform . parent = FindRootTransform ( ) . transform ;
36+ }
4537 Selection . activeObject = go ;
38+ if ( isWithArrow )
39+ {
40+ go . AddComponent < NoteArrow > ( ) ;
41+ }
42+ Undo . RegisterCreatedObjectUndo ( go , "Create " + go . name ) ;
4643 return go ;
4744 }
4845 #endregion
@@ -51,29 +48,34 @@ private static GameObject CreateLazyNoteInternal(MenuCommand menuCommand)
5148 [ MenuItem ( "GameObject/" + ASSET_SHORT_NAME + "/Create " + nameof ( Note ) + " with arrow" ) ]
5249 public static void CreateNoteWithArrow ( MenuCommand menuCommand )
5350 {
54- GameObject go = CreateNoteInternal ( menuCommand ) ;
55- go . AddComponent < NoteArrow > ( ) ;
56- Undo . RegisterCreatedObjectUndo ( go , "Create " + go . name ) ;
51+ CreateNoteInternal ( menuCommand , true ) ;
5752 }
5853 [ MenuItem ( "GameObject/" + ASSET_SHORT_NAME + "/Create " + nameof ( Note ) ) ]
5954 public static void CreateNote ( MenuCommand menuCommand )
6055 {
61- GameObject go = CreateNoteInternal ( menuCommand ) ;
62- Undo . RegisterCreatedObjectUndo ( go , "Create " + go . name ) ;
56+ CreateNoteInternal ( menuCommand , false ) ;
6357 }
64- private static GameObject CreateNoteInternal ( MenuCommand menuCommand )
58+ private static GameObject CreateNoteInternal ( MenuCommand menuCommand , bool isWithArrow )
6559 {
6660 GameObject go = new GameObject ( nameof ( Note ) ) ;
6761 go . tag = EDITOR_NAME_TAG ;
6862 go . AddComponent < Note > ( ) ;
6963 GameObjectUtility . SetParentAndAlign ( go , menuCommand . context as GameObject ) ;
7064 if ( go . transform . parent == null )
71- go . transform . parent = FindRoot ( NOTES_ROOT_NAME ) . transform ;
65+ {
66+ go . transform . parent = FindRootTransform ( ) . transform ;
67+ }
7268 Selection . activeObject = go ;
69+ if ( isWithArrow )
70+ {
71+ go . AddComponent < NoteArrow > ( ) ;
72+ }
73+ Undo . RegisterCreatedObjectUndo ( go , "Create " + go . name ) ;
7374 return go ;
7475 }
7576 #endregion
7677
78+ #region Draw
7779 [ DrawGizmo ( GizmoType . Selected | GizmoType . NonSelected | GizmoType . Pickable ) ]
7880 private static void DrawLazyNote ( LazyNote note , GizmoType gizmoType )
7981 {
@@ -97,29 +99,59 @@ private static void DrawNote(Note note, GizmoType gizmoType)
9799 string sceneNote = GetSceneNote ( note . Text , note . DrawIcon ) ;
98100 Handles . Label ( note . transform . position , sceneNote , EditorStyles . whiteBoldLabel ) ;
99101 }
102+ internal static string GetSceneNote ( string fullNote , bool isNeedSpacing )
103+ {
104+ int index = fullNote . IndexOf ( NOTE_SEPARATOR ) ;
105+ if ( index < 0 ) return string . Empty ;
106+ string result = fullNote . Substring ( 0 , index ) ;
107+ return isNeedSpacing ? "\r \n " + result : result ;
108+ }
109+ #endregion
100110
111+ #region Utils
112+ private static Transform FindRootTransform ( string name = NOTES_ROOT_NAME )
113+ {
114+ GameObject root = GameObject . Find ( name ) ;
115+ if ( root == null )
116+ {
117+ root = new GameObject ( name ) ;
118+ root . tag = EDITOR_NAME_TAG ;
119+ root . transform . position = Vector3 . zero ;
120+ root . transform . rotation = Quaternion . identity ;
121+ root . transform . localScale = Vector3 . one ;
122+ }
123+ return root . transform ;
124+ }
101125 internal static string GetGizmosPath ( )
102126 {
103127 if ( string . IsNullOrEmpty ( _gizmosPath ) )
104128 {
105129 var assembly = Assembly . GetExecutingAssembly ( ) ;
106130 string packagePath = null ;
107131 if ( assembly != null )
132+ {
108133 packagePath = UnityEditor . PackageManager . PackageInfo . FindForAssembly ( assembly ) ? . assetPath ;
134+ }
109135 if ( string . IsNullOrEmpty ( packagePath ) )
110- packagePath = "Assets" ;
136+ {
137+ var guids = AssetDatabase . FindAssets ( $ "Notes-Unity t:AssemblyDefinitionAsset") ;
138+ for ( var i = 0 ; i < guids . Length ; i ++ )
139+ {
140+ var guid = guids [ i ] ;
141+ var path = AssetDatabase . GUIDToAssetPath ( guid ) ;
142+ var asmdef = AssetDatabase . LoadAssetAtPath < AssemblyDefinitionAsset > ( path ) ;
143+ if ( asmdef != null && asmdef . name == "Notes-Unity" )
144+ {
145+ packagePath = path . Substring ( 0 , path . LastIndexOf ( "/" ) ) ;
146+ break ;
147+ }
148+ }
149+ }
111150 _gizmosPath = packagePath + "/Gizmos" ;
112-
113151 }
114152 return _gizmosPath ;
115153 }
116- internal static string GetSceneNote ( string fullNote , bool isNeedSpacing )
117- {
118- int index = fullNote . IndexOf ( NOTE_SEPARATOR ) ;
119- if ( index < 0 ) return string . Empty ;
120- string result = fullNote . Substring ( 0 , index ) ;
121- return isNeedSpacing ? "\r \n " + result : result ;
122- }
154+ #endregion
123155 }
124156}
125157#endif
0 commit comments