-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStateMachineEditor.cs
More file actions
173 lines (156 loc) · 5.44 KB
/
Copy pathStateMachineEditor.cs
File metadata and controls
173 lines (156 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomEditor( typeof( StateMachine ) )]
[CanEditMultipleObjects]
public class StateMachineEditor : Editor
{
private const float buttonWidth = 20f;
SerializedProperty firstStateProperty;
SerializedProperty statesProperty;
void OnEnable()
{
firstStateProperty = serializedObject.FindProperty( "firstState" );
statesProperty = serializedObject.FindProperty( "states" );
}
public override void OnInspectorGUI()
{
serializedObject.Update();
string firstStateName = firstStateProperty.stringValue;
int numberOfStates = statesProperty.arraySize;
int selectedIndex = 0;
string[] stateNames = new string[ numberOfStates + 1 ];
stateNames[ 0 ] = "None";
for( int i = 0; i < numberOfStates; ++i )
{
SerializedProperty stateProperty = statesProperty.GetArrayElementAtIndex( i );
string stateName = stateProperty.FindPropertyRelative( "name" ).stringValue;
stateNames[ i + 1 ] = stateName;
if( stateName == firstStateName && stateName != "" )
{
selectedIndex = i + 1;
}
}
int newIndex = EditorGUILayout.Popup( "Initial state", selectedIndex, stateNames);
if( newIndex != selectedIndex )
{
if( newIndex == 0 )
{
firstStateProperty.stringValue = "";
}
else
{
firstStateProperty.stringValue = stateNames[ newIndex ];
}
}
EditorGUILayout.Space();
EditorGUILayout.LabelField( "States:" );
++EditorGUI.indentLevel;
float nameWidth = EditorGUIUtility.labelWidth - buttonWidth - 25;
EditorGUILayout.BeginHorizontal();
GUILayout.Space( buttonWidth + 3 );
EditorGUILayout.LabelField( "Name", GUILayout.Width( nameWidth + 5 ) );
EditorGUILayout.LabelField( "Components" );
EditorGUILayout.EndHorizontal();
for( int i = 0; i < numberOfStates; ++i )
{
EditorGUILayout.Space();
EditorGUILayout.BeginHorizontal();
bool removeState = GUILayout.Button( "-", GUILayout.Width( buttonWidth ) );
if( removeState )
{
statesProperty.DeleteArrayElementAtIndex( i );
for( int k = i + 1; k < numberOfStates; ++k )
{
statesProperty.MoveArrayElement( k, k - 1 );
}
--numberOfStates;
statesProperty.arraySize = numberOfStates;
--i;
continue;
}
SerializedProperty stateProperty = statesProperty.GetArrayElementAtIndex( i );
string stateName = stateProperty.FindPropertyRelative( "name" ).stringValue;
string newStateName = EditorGUILayout.TextField( stateName, GUILayout.Width( nameWidth ) );
if( newStateName != stateName )
{
stateProperty.FindPropertyRelative( "name" ).stringValue = newStateName;
if( firstStateProperty.stringValue == stateName && stateName != "" )
{
firstStateProperty.stringValue = newStateName;
}
}
EditorGUILayout.BeginVertical();
SerializedProperty componentsProperty = stateProperty.FindPropertyRelative( ( "components" ) );
int numberOfComponents = componentsProperty.isArray ? componentsProperty.arraySize : 0;
for( int j = 0; j < numberOfComponents; ++j )
{
EditorGUILayout.BeginHorizontal();
Object component = componentsProperty.GetArrayElementAtIndex( j ).objectReferenceValue;
Object updatedComponent = EditorGUILayout.ObjectField( component, typeof( MonoBehaviour ), true );
if( updatedComponent != component )
{
componentsProperty.GetArrayElementAtIndex( j ).objectReferenceValue = updatedComponent;
}
bool removeComponent = GUILayout.Button( "-", GUILayout.Width( buttonWidth ) );
if( removeComponent )
{
componentsProperty.DeleteArrayElementAtIndex( j );
for( int k = j + 1; k < numberOfComponents; ++k )
{
componentsProperty.MoveArrayElement( k, k - 1 );
}
--numberOfComponents;
componentsProperty.arraySize = numberOfComponents;
--j;
}
EditorGUILayout.EndHorizontal();
}
if( numberOfComponents == 0 )
{
EditorGUILayout.BeginHorizontal();
Object updatedComponent = EditorGUILayout.ObjectField( null, typeof( MonoBehaviour ), true );
if( updatedComponent != null )
{
numberOfComponents = 1;
componentsProperty.arraySize = 1;
componentsProperty.GetArrayElementAtIndex( 0 ).objectReferenceValue = updatedComponent;
}
EditorGUILayout.EndHorizontal();
}
if( numberOfComponents > 0 )
{
EditorGUILayout.BeginHorizontal();
EditorGUILayout.Space();
bool addComponent = GUILayout.Button( "+", GUILayout.Width( buttonWidth ) );
if( addComponent )
{
++numberOfComponents;
componentsProperty.arraySize = numberOfComponents;
componentsProperty.GetArrayElementAtIndex( numberOfComponents - 1 ).objectReferenceValue = null;
}
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.EndVertical();
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.Space();
EditorGUILayout.BeginHorizontal();
bool addState = GUILayout.Button( "+", GUILayout.Width( buttonWidth ) );
if( addState )
{
++numberOfStates;
statesProperty.arraySize = numberOfStates;
SerializedProperty stateProperty = statesProperty.GetArrayElementAtIndex( numberOfStates - 1 );
stateProperty.FindPropertyRelative( "name" ).stringValue = "";
stateProperty.FindPropertyRelative( "components" ).arraySize = 0;
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space();
--EditorGUI.indentLevel;
//EditorGUILayout.PropertyField( firstStateProperty );
//EditorGUILayout.PropertyField( statesProperty, true );
serializedObject.ApplyModifiedProperties();
}
}