66using TarasK8 . UI . Animations . AnimatedProperties ;
77using TarasK8 . UI . Editor . Utils ;
88using UnityEditor ;
9+ using UnityEditor . IMGUI . Controls ;
910using UnityEngine ;
1011
1112namespace TarasK8 . UI . Editor . Animations
@@ -16,14 +17,18 @@ public class StateMachineEditor : UnityEditor.Editor
1617 {
1718 private static string [ ] _propertiesTypesOptions ;
1819 private static List < Type > _propertiesTypes ;
20+ private static AnimatedPropertiesDropdown _addPropertyDropdown ;
1921 private int _selectedTypeOption ;
2022 private string _selectedStateName ;
2123 private SerializedProperty _ignoreTimeScale ;
2224 private SerializedProperty _fullyComplete ;
2325 private SerializedProperty _defaultState ;
2426 private SerializedProperty _animatedProperties ;
25- private StateMachine _target ;
2627 private SerializedProperty _states ;
28+ private static bool _showProperties = true ;
29+ private static bool _showStates = true ;
30+
31+ private StateMachine _target ;
2732
2833 private void OnEnable ( )
2934 {
@@ -43,27 +48,46 @@ private void OnEnable()
4348 for ( int i = 0 ; i < _propertiesTypes . Count ; i ++ )
4449 {
4550 TransitionMenuNameAttribute attribute = ( TransitionMenuNameAttribute ) Attribute . GetCustomAttribute ( _propertiesTypes [ i ] , typeof ( TransitionMenuNameAttribute ) ) ;
46- _propertiesTypesOptions [ i ] = attribute ? . MenuName ?? _propertiesTypes [ i ] . Name ;
51+ string option = attribute ? . MenuName ?? _propertiesTypes [ i ] . Name ;
52+ _propertiesTypesOptions [ i ] = option ;
4753 }
54+ var state = new AdvancedDropdownState ( ) ;
55+ _addPropertyDropdown = new AnimatedPropertiesDropdown ( state , _propertiesTypesOptions ) ;
4856 }
57+ _showProperties = EditorPrefs . GetBool ( nameof ( _showProperties ) , _showProperties ) ;
58+ }
59+
60+ private void OnDisable ( )
61+ {
62+ EditorPrefs . SetBool ( nameof ( _showProperties ) , _showProperties ) ;
4963 }
5064
5165 public override void OnInspectorGUI ( )
5266 {
5367 serializedObject . Update ( ) ;
5468
5569 DrawOptions ( ) ;
56- EditorGUILayout . Space ( 15f ) ;
5770
58- DrawLabel ( ) ;
59- _selectedTypeOption = EditorGUILayout . Popup ( _selectedTypeOption , _propertiesTypesOptions ) ;
60- DrawAllAnimatedProperties ( ) ;
61- DrawAddPropertyButton ( ) ;
62- EditorGUILayout . Space ( 15f ) ;
63- StateListDrawer . Draw ( _states , _target . States ) ;
64- DrawAddStateButton ( ) ;
71+ EditorGUILayout . Space ( ) ;
72+
73+ _showProperties = EditorGUILayout . BeginFoldoutHeaderGroup ( _showProperties ,
74+ $ "Animated Properties ({ _animatedProperties . arraySize } )") ;
75+ if ( _showProperties )
76+ {
77+ DrawAllAnimatedProperties ( ) ;
78+ DrawAddPropertyButton ( ) ;
79+ }
80+ EditorGUILayout . EndFoldoutHeaderGroup ( ) ;
6581
66- //DrawAllStates();
82+ EditorGUILayout . Space ( ) ;
83+
84+ _showStates = EditorGUILayout . BeginFoldoutHeaderGroup ( _showStates , $ "States ({ _target . States . Count } )") ;
85+ if ( _showStates )
86+ {
87+ StateListDrawer . Draw ( _states ) ;
88+ DrawAddStateButton ( ) ;
89+ }
90+ EditorGUILayout . EndFoldoutHeaderGroup ( ) ;
6791
6892 serializedObject . ApplyModifiedProperties ( ) ;
6993 }
@@ -82,50 +106,65 @@ private void DrawAllAnimatedProperties()
82106 EditorGUILayout . BeginVertical ( GUI . skin . box ) ;
83107
84108 var animatedProperty = _animatedProperties . GetArrayElementAtIndex ( i ) ;
109+ var childs = animatedProperty . GetChildProperties ( ) . ToArray ( ) ;
110+
85111 EditorGUILayout . BeginHorizontal ( ) ;
112+ EditorGUILayout . PropertyField ( childs [ 0 ] , GUIContent . none , GUILayout . Width ( 16f ) ) ;
86113 EditorGUILayout . LabelField ( animatedProperty . managedReferenceValue . GetType ( ) . Name , EditorStyles . boldLabel ) ;
87114 if ( MyGuiUtility . DrawRemoveButton ( ) )
88115 {
89- _target . RemoveAnimatedProperty ( i ) ;
90- EditorUtility . SetDirty ( target ) ;
116+ foreach ( var target in targets )
117+ {
118+ var stateMachine = target as StateMachine ;
119+ stateMachine . RemoveAnimatedProperty ( i ) ;
120+ EditorUtility . SetDirty ( stateMachine ) ;
121+ }
91122 }
92- EditorGUILayout . EndHorizontal ( ) ;
93123
94- var childs = animatedProperty . GetChildProperties ( ) ;
95- foreach ( var element in childs )
124+ EditorGUILayout . EndHorizontal ( ) ;
125+ for ( int j = 1 ; j < childs . Length ; j ++ )
96126 {
97-
127+ var element = childs [ j ] ;
98128 EditorGUILayout . PropertyField ( element ) ;
99129 }
130+
100131 EditorGUILayout . EndVertical ( ) ;
101132 }
102133 }
103-
104- private void DrawLabel ( )
105- {
106- EditorGUILayout . BeginHorizontal ( ) ;
107- EditorGUILayout . LabelField ( $ "Animated Properties ({ _animatedProperties . arraySize } )", EditorStyles . boldLabel ) ;
108- EditorGUILayout . EndHorizontal ( ) ;
109- }
110134
111- public void DrawAddPropertyButton ( )
135+ private void DrawAddPropertyButton ( )
112136 {
137+ var lastRect = GUILayoutUtility . GetLastRect ( ) ;
113138 if ( MyGuiUtility . DrawAddButton ( "Add Animated Property" ) )
114139 {
115- var type = _propertiesTypes [ _selectedTypeOption ] ;
116- AnimatedProperty animatedProperty = ( AnimatedProperty ) Activator . CreateInstance ( type ) ;
117- _target . AddAnimatedProperty ( animatedProperty ) ;
118- EditorUtility . SetDirty ( target ) ;
140+ _addPropertyDropdown . OnItemSelected = AddProperty ;
141+ _addPropertyDropdown . Show ( CalculateDropdownRect ( lastRect ) ) ;
119142 }
120143 }
121144
122- public void DrawAddStateButton ( )
145+ private void DrawAddStateButton ( )
123146 {
124147 if ( MyGuiUtility . DrawAddButton ( "Add State" ) )
125148 {
126- var name = StateListDrawer . GetUniqueName ( _target . States ) ;
127- _target . AddState ( name ) ;
128- EditorUtility . SetDirty ( serializedObject . targetObject ) ;
149+ foreach ( var target in targets )
150+ {
151+ var stateMachine = target as StateMachine ;
152+ var name = StateListDrawer . GetUniqueName ( stateMachine . States ) ;
153+ stateMachine . AddState ( name ) ;
154+ EditorUtility . SetDirty ( stateMachine ) ;
155+ }
156+ }
157+ }
158+
159+ private void AddProperty ( int index )
160+ {
161+ var type = _propertiesTypes [ index ] ;
162+ AnimatedProperty animatedProperty = ( AnimatedProperty ) Activator . CreateInstance ( type ) ;
163+ foreach ( var target in targets )
164+ {
165+ var stateMachine = target as StateMachine ;
166+ stateMachine . AddAnimatedProperty ( animatedProperty ) ;
167+ EditorUtility . SetDirty ( stateMachine ) ;
129168 }
130169 }
131170
@@ -142,5 +181,18 @@ private List<Type> GetAnimatedPropertyTypes()
142181 }
143182 return derivedTypes ;
144183 }
184+
185+ private Rect CalculateDropdownRect ( Rect lastRect )
186+ {
187+ const float width = 230f ;
188+ const float buttonSpacing = 27f ;
189+ const float xOffset = 18f ;
190+
191+ float x = ( lastRect . width - width ) * 0.5f + xOffset ;
192+ float y = lastRect . y + buttonSpacing ;
193+ var rect = new Rect ( x , y , width , lastRect . height ) ;
194+
195+ return rect ;
196+ }
145197 }
146198}
0 commit comments