-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExposeProperties.cs
More file actions
217 lines (157 loc) · 5.79 KB
/
ExposeProperties.cs
File metadata and controls
217 lines (157 loc) · 5.79 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
* cesar: copied from http://unifycommunity.com/wiki/index.php?title=Expose_properties_in_inspector
*/
using UnityEditor;
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
public static class ExposeProperties
{
public static void Expose( PropertyField[] properties )
{
GUILayoutOption[] emptyOptions = new GUILayoutOption[0];
EditorGUILayout.BeginVertical( emptyOptions );
foreach ( PropertyField field in properties )
{
EditorGUILayout.BeginHorizontal( emptyOptions );
switch ( field.Type )
{
case SerializedPropertyType.Integer:
field.SetValue( EditorGUILayout.IntField( field.Name, (int)field.GetValue(), emptyOptions ) );
break;
case SerializedPropertyType.Float:
field.SetValue( EditorGUILayout.FloatField( field.Name, (float)field.GetValue(), emptyOptions ) );
break;
case SerializedPropertyType.Boolean:
field.SetValue( EditorGUILayout.Toggle( field.Name, (bool)field.GetValue(), emptyOptions ) );
break;
case SerializedPropertyType.String:
field.SetValue( EditorGUILayout.TextField( field.Name, (String)field.GetValue(), emptyOptions ) );
break;
case SerializedPropertyType.Vector2:
field.SetValue( EditorGUILayout.Vector2Field( field.Name, (Vector2)field.GetValue(), emptyOptions ) );
break;
case SerializedPropertyType.Vector3:
field.SetValue( EditorGUILayout.Vector3Field( field.Name, (Vector3)field.GetValue(), emptyOptions ) );
break;
case SerializedPropertyType.Enum:
field.SetValue(EditorGUILayout.EnumPopup(field.Name, (Enum)field.GetValue(), emptyOptions));
break;
default:
break;
}
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.EndVertical();
}
public static PropertyField[] GetProperties( System.Object obj )
{
List< PropertyField > fields = new List<PropertyField>();
PropertyInfo[] infos = obj.GetType().GetProperties( BindingFlags.Public | BindingFlags.Instance );
foreach ( PropertyInfo info in infos )
{
if ( ! (info.CanRead && info.CanWrite) )
continue;
object[] attributes = info.GetCustomAttributes( true );
bool isExposed = false;
foreach( object o in attributes )
{
if ( o.GetType() == typeof( ExposePropertyAttribute ) )
{
isExposed = true;
break;
}
}
if ( !isExposed )
continue;
SerializedPropertyType type = SerializedPropertyType.Integer;
if( PropertyField.GetPropertyType( info, out type ) )
{
PropertyField field = new PropertyField( obj, info, type );
fields.Add( field );
}
}
return fields.ToArray();
}
}
public class PropertyField
{
System.Object m_Instance;
PropertyInfo m_Info;
SerializedPropertyType m_Type;
MethodInfo m_Getter;
MethodInfo m_Setter;
public SerializedPropertyType Type
{
get
{
return m_Type;
}
}
public String Name
{
get
{
return ObjectNames.NicifyVariableName( m_Info.Name );
}
}
public PropertyField( System.Object instance, PropertyInfo info, SerializedPropertyType type )
{
m_Instance = instance;
m_Info = info;
m_Type = type;
m_Getter = m_Info.GetGetMethod();
m_Setter = m_Info.GetSetMethod();
}
public System.Object GetValue()
{
return m_Getter.Invoke( m_Instance, null );
}
public void SetValue( System.Object value )
{
m_Setter.Invoke( m_Instance, new System.Object[] { value } );
}
public static bool GetPropertyType( PropertyInfo info, out SerializedPropertyType propertyType )
{
propertyType = SerializedPropertyType.Generic;
Type type = info.PropertyType;
if ( type == typeof( int ) )
{
propertyType = SerializedPropertyType.Integer;
return true;
}
if ( type == typeof( float ) )
{
propertyType = SerializedPropertyType.Float;
return true;
}
if ( type == typeof( bool ) )
{
propertyType = SerializedPropertyType.Boolean;
return true;
}
if ( type == typeof( string ) )
{
propertyType = SerializedPropertyType.String;
return true;
}
if ( type == typeof( Vector2 ) )
{
propertyType = SerializedPropertyType.Vector2;
return true;
}
if ( type == typeof( Vector3 ) )
{
propertyType = SerializedPropertyType.Vector3;
return true;
}
if ( type.IsEnum )
{
propertyType = SerializedPropertyType.Enum;
return true;
}
return false;
}
}