1+ using UnityEditor ;
2+ using UnityEngine ;
3+ using System . Collections . Generic ;
4+ using UnityEditor . IMGUI . Controls ;
5+ using System ;
6+
7+ using Object = UnityEngine . Object ;
8+ using System . Reflection ;
9+
10+ namespace GTVariable . Editor
11+ {
12+ public class SideBar
13+ {
14+ public SerializedProperty Array { get ; set ; }
15+ public Type ElementType { get ; set ; }
16+ public SerializedObject SerializedObject { get { return Array . serializedObject ; } }
17+ public int Selected { get ; private set ; }
18+ public Object CurrentSelected { get { return Selected == - 1 || Selected >= Array . arraySize ? null : Array . GetArrayElementAtIndex ( Selected ) . objectReferenceValue ; } }
19+ public bool CanDuplicated { get ; set ; } = true ;
20+ public bool CanDelete { get ; set ; } = true ;
21+ public bool CanCreate { get ; set ; } = true ;
22+
23+ public event Action < Object > OnElementDelete ;
24+
25+ private string searchText ;
26+ private SearchField searchField ;
27+ private ComponenetCreator componenetCreator = new ComponenetCreator ( ) ;
28+
29+ private Vector2 scrollVector ;
30+
31+ public void OnGUI ( )
32+ {
33+ if ( searchField == null ) searchField = new SearchField ( ) ;
34+
35+
36+ //Draw header
37+ using ( var scope = new EditorGUILayout . HorizontalScope ( GTGUIStyles . sideBarHeader ) )
38+ {
39+ EditorGUILayout . LabelField ( Array . displayName , EditorStyles . boldLabel ) ;
40+ if ( GUILayout . Button ( GTGUIStyles . PlusIcon , GTGUIStyles . plusButtonStyle , GUILayout . MaxWidth ( 20 ) ) && CanCreate )
41+ {
42+ componenetCreator . UpdateComponentsTypesList ( ElementType ) ;
43+ componenetCreator . ShowObjectMenu ( OnTypeSelected ) ;
44+ }
45+ }
46+
47+ //Draw content
48+ EditorGUILayout . BeginVertical ( GTGUIStyles . sideBarContent ) ;
49+ scrollVector = EditorGUILayout . BeginScrollView ( scrollVector ) ;
50+ if ( Array . arraySize == 0 )
51+ {
52+ EditorGUILayout . LabelField ( "List is Empty" , GTGUIStyles . labelCenter ) ;
53+ }
54+
55+ for ( int i = 0 ; i < Array . arraySize ; i ++ )
56+ {
57+ var current = Array . GetArrayElementAtIndex ( i ) ;
58+
59+
60+
61+ using ( var scope = new EditorGUILayout . HorizontalScope ( Selected == i ? GTGUIStyles . rowSelected : GTGUIStyles . row ) )
62+ {
63+
64+ EditorGUILayout . LabelField ( GetElementName ( i ) ) ;
65+
66+ GUI . Label ( scope . rect , GUIContent . none ) ;
67+ Rect rect = scope . rect ;
68+ if ( rect . Contains ( Event . current . mousePosition ) )
69+ {
70+ if ( Event . current . type == EventType . MouseDown )
71+ {
72+ if ( Event . current . button == 0 )
73+ {
74+ GUI . FocusControl ( "" ) ;
75+ Selected = i ;
76+ Event . current . Use ( ) ;
77+ }
78+ else if ( Event . current . button == 1 )
79+ {
80+ var menu = new GenericMenu ( ) ;
81+ if ( CanDelete ) menu . AddItem ( new GUIContent ( "Delete" ) , false , ( ) => OnDelete ( current ) ) ;
82+ if ( CanDuplicated ) menu . AddItem ( new GUIContent ( "Duplicated" ) , false , ( ) => OnDuplicated ( current ) ) ;
83+ menu . ShowAsContext ( ) ;
84+ }
85+ }
86+ }
87+ }
88+
89+ var lineRect = GUILayoutUtility . GetRect ( GUIContent . none , GTGUIStyles . sideBarElementLine ) ;
90+ GUI . Box ( lineRect , GUIContent . none ) ;
91+ }
92+ EditorGUILayout . EndScrollView ( ) ;
93+ EditorGUILayout . EndVertical ( ) ;
94+
95+ Selected = Array . arraySize == 0 ? - 1 : Mathf . Clamp ( Selected , 0 , Array . arraySize ) ;
96+ }
97+
98+ protected virtual void OnDelete ( SerializedProperty target )
99+ {
100+ var objRef = target . objectReferenceValue ;
101+ if ( objRef == null || EditorUtility . DisplayDialog ( "Delete Selected Asset?" , "You cannot undo this action." , "Delete" , "Cancel" ) )
102+ {
103+ SerializedObject . Update ( ) ;
104+
105+
106+ var index = GTGUIUtilites . FindIndexInProperty ( target . objectReferenceValue , Array ) ;
107+ var deleteTwice = target . objectReferenceValue != null ;
108+ Array . DeleteArrayElementAtIndex ( index ) ;
109+ if ( deleteTwice ) Array . DeleteArrayElementAtIndex ( index ) ;
110+ SerializedObject . ApplyModifiedProperties ( ) ;
111+ OnElementDelete . Invoke ( objRef ) ;
112+ Selected = Array . arraySize == 0 ? - 1 : Mathf . Clamp ( Selected , 0 , Array . arraySize ) ;
113+ }
114+ }
115+
116+ protected virtual void OnDuplicated ( SerializedProperty target )
117+ {
118+ SerializedObject . Update ( ) ;
119+ var index = GTGUIUtilites . FindIndexInProperty ( target . objectReferenceValue , Array ) ;
120+ Array . InsertArrayElementAtIndex ( index ) ;
121+ Array . GetArrayElementAtIndex ( index ) . objectReferenceValue = target . objectReferenceValue ;
122+ SerializedObject . ApplyModifiedProperties ( ) ;
123+ Selected = Array . arraySize == 0 ? - 1 : Mathf . Clamp ( Selected , 0 , Array . arraySize ) ;
124+ }
125+
126+ public virtual string GetElementName ( int i )
127+ {
128+ if ( Array == null ) return string . Empty ;
129+
130+ var objRef = Array . GetArrayElementAtIndex ( i ) . objectReferenceValue ;
131+
132+ return objRef != null ? objRef . name : string . Empty ;
133+ }
134+
135+
136+ private void OnTypeSelected ( object type )
137+ {
138+ SerializedObject . Update ( ) ;
139+
140+ Array . InsertArrayElementAtIndex ( Array . arraySize ) ;
141+
142+ var newObject = ScriptableObject . CreateInstance ( ( Type ) type ) as Object ;
143+
144+ var nameable = newObject as INameable ;
145+
146+ if ( nameable != null )
147+ {
148+ nameable . Name = newObject . name = $ "New{ newObject . GetType ( ) . Name } ";
149+ }
150+ else
151+ {
152+ newObject . name = $ "New{ newObject . GetType ( ) . Name } ";
153+ }
154+
155+ AssetDatabase . AddObjectToAsset ( newObject , SerializedObject . targetObject ) ;
156+ AssetDatabase . SaveAssets ( ) ;
157+ AssetDatabase . Refresh ( ) ;
158+
159+ Array . GetArrayElementAtIndex ( Array . arraySize - 1 ) . objectReferenceValue = newObject ;
160+
161+ SerializedObject . ApplyModifiedProperties ( ) ;
162+
163+ }
164+
165+ }
166+ }
0 commit comments