-
-
Notifications
You must be signed in to change notification settings - Fork 181
Expand file tree
/
Copy pathPropertyEditor.cs
More file actions
244 lines (221 loc) · 10.4 KB
/
PropertyEditor.cs
File metadata and controls
244 lines (221 loc) · 10.4 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using System;
using UnityEditor;
using UnityEngine;
using System.Reflection;
using System.Collections.Generic;
namespace LLMUnity
{
public class PropertyEditor : Editor
{
public static int buttonWidth = 150;
public virtual void AddScript(SerializedObject llmScriptSO)
{
var scriptProp = llmScriptSO.FindProperty("m_Script");
EditorGUILayout.PropertyField(scriptProp);
}
public virtual bool ToggleButton(string text, bool activated)
{
GUIStyle style = new GUIStyle("Button");
if (activated) style.normal = new GUIStyleState() { background = Texture2D.grayTexture };
return GUILayout.Button(text, style, GUILayout.Width(buttonWidth));
}
public virtual void AddSetupSettings(SerializedObject llmScriptSO)
{
List<Type> attributeClasses = new List<Type>(){typeof(LocalRemoteAttribute)};
SerializedProperty remoteProperty = llmScriptSO.FindProperty("_remote");
if (remoteProperty != null) attributeClasses.Add(remoteProperty.boolValue ? typeof(RemoteAttribute) : typeof(LocalAttribute));
attributeClasses.Add(typeof(LLMAttribute));
if (llmScriptSO.FindProperty("advancedOptions").boolValue)
{
attributeClasses.Add(typeof(LLMAdvancedAttribute));
}
ShowPropertiesOfClass("Setup Settings", llmScriptSO, attributeClasses, true);
}
public virtual void AddModelSettings(SerializedObject llmScriptSO)
{
List<Type> attributeClasses = new List<Type>(){typeof(ModelAttribute)};
if (llmScriptSO.FindProperty("advancedOptions").boolValue)
{
attributeClasses.Add(typeof(ModelAdvancedAttribute));
}
ShowPropertiesOfClass("Model Settings", llmScriptSO, attributeClasses, true);
}
public virtual void AddChatSettings(SerializedObject llmScriptSO)
{
List<Type> attributeClasses = new List<Type>(){typeof(ChatAttribute)};
if (llmScriptSO.FindProperty("advancedOptions").boolValue)
{
attributeClasses.Add(typeof(ChatAdvancedAttribute));
}
ShowPropertiesOfClass("Chat Settings", llmScriptSO, attributeClasses, true);
}
public void AddDebugModeToggle()
{
LLMUnitySetup.SetDebugMode((LLMUnitySetup.DebugModeType)EditorGUILayout.EnumPopup("Log Level", LLMUnitySetup.DebugMode));
}
public void AddAdvancedOptionsToggle(SerializedObject llmScriptSO)
{
List<SerializedProperty> properties = GetPropertiesOfClass(llmScriptSO, new List<Type>(){typeof(AdvancedAttribute)});
if (properties.Count == 0) return;
SerializedProperty advancedOptionsProp = llmScriptSO.FindProperty("advancedOptions");
string toggleText = (advancedOptionsProp.boolValue ? "Hide" : "Show") + " Advanced Options";
if (ToggleButton(toggleText, advancedOptionsProp.boolValue)) advancedOptionsProp.boolValue = !advancedOptionsProp.boolValue;
}
public virtual void AddOptionsToggles(SerializedObject llmScriptSO)
{
AddAdvancedOptionsToggle(llmScriptSO);
Space();
}
public virtual void Space()
{
EditorGUILayout.Space((int)EditorGUIUtility.singleLineHeight / 2);
}
protected virtual Type[] GetPropertyTypes()
{
List<Type> types = new List<Type>();
Type currentType = target.GetType();
while (currentType != null)
{
types.Add(currentType);
currentType = currentType.BaseType;
if (currentType == typeof(MonoBehaviour)) break;
}
types.Reverse();
return types.ToArray();
}
public List<SerializedProperty> GetPropertiesOfClass(SerializedObject so, List<Type> attributeClasses)
{
// display a property if it belongs to a certain class and/or has a specific attribute class
List<SerializedProperty> properties = new List<SerializedProperty>();
foreach (Type attrClass in attributeClasses)
{
if (attrClass == null) continue;
foreach (Type targetClass in GetPropertyTypes())
{
SerializedProperty prop = so.GetIterator();
if (prop.NextVisible(true))
{
do
{
if (PropertyInClass(prop, targetClass, attrClass))
properties.Add(so.FindProperty(prop.propertyPath));
}
while (prop.NextVisible(false));
}
}
}
return properties;
}
public bool ShowPropertiesOfClass(string title, SerializedObject so, List<Type> attributeClasses, bool addSpace = true, List<Type> excludeAttributeClasses = null)
{
// display a property if it belongs to a certain class and/or has a specific attribute class
List<SerializedProperty> properties = GetPropertiesOfClass(so, attributeClasses);
if (excludeAttributeClasses != null)
{
List<SerializedProperty> excludeProperties = GetPropertiesOfClass(so, excludeAttributeClasses);
List<SerializedProperty> removeProperties = new List<SerializedProperty>();
foreach (SerializedProperty excprop in excludeProperties)
{
foreach (SerializedProperty prop in properties)
{
if (prop.displayName == excprop.displayName) removeProperties.Add(prop);
}
}
foreach (SerializedProperty prop in removeProperties) properties.Remove(prop);
}
if (properties.Count == 0) return false;
if (title != "") EditorGUILayout.LabelField(title, EditorStyles.boldLabel);
foreach (SerializedProperty prop in properties)
{
Attribute floatAttr = GetPropertyAttribute(prop, typeof(FloatAttribute));
Attribute intAttr = GetPropertyAttribute(prop, typeof(IntAttribute));
Attribute dynamicRangeAttr = GetPropertyAttribute(prop, typeof(DynamicRangeAttribute));
if (floatAttr != null)
{
FloatAttribute attr = (FloatAttribute)floatAttr;
EditorGUILayout.Slider(prop, attr.Min, attr.Max, new GUIContent(prop.displayName));
}
else if (intAttr != null)
{
IntAttribute attr = (IntAttribute)intAttr;
EditorGUILayout.IntSlider(prop, attr.Min, attr.Max, new GUIContent(prop.displayName));
}
else if (dynamicRangeAttr != null)
{
DynamicRangeAttribute attr = (DynamicRangeAttribute)dynamicRangeAttr;
if (attr.intOrFloat)
{
float minValue = so.FindProperty(attr.minVariable).floatValue;
float maxValue = so.FindProperty(attr.maxVariable).floatValue;
EditorGUILayout.Slider(prop, minValue, maxValue, new GUIContent(prop.displayName));
}
else
{
int minValue = so.FindProperty(attr.minVariable).intValue;
int maxValue = so.FindProperty(attr.maxVariable).intValue;
EditorGUILayout.IntSlider(prop, minValue, maxValue, new GUIContent(prop.displayName));
}
}
else
{
EditorGUILayout.PropertyField(prop);
}
}
if (addSpace) Space();
return true;
}
public bool PropertyInClass(SerializedProperty prop, Type targetClass, Type attributeClass = null)
{
// check if a property belongs to a certain class and/or has a specific attribute class
FieldInfo field = prop.serializedObject.targetObject.GetType().GetField(prop.name,
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
return field != null && field.DeclaringType == targetClass && (attributeClass == null || GetPropertyAttribute(prop, attributeClass) != null);
}
public Attribute GetPropertyAttribute(SerializedProperty prop, Type attributeClass)
{
// check if a property has a specific attribute class
foreach (var pathSegment in prop.propertyPath.Split('.'))
{
var targetType = prop.serializedObject.targetObject.GetType();
while (targetType != null)
{
var fieldInfo = targetType.GetField(pathSegment, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
if (fieldInfo != null)
{
foreach (Attribute attr in fieldInfo.GetCustomAttributes(attributeClass, true))
{
if (attributeClass.IsAssignableFrom(attr.GetType()))
return attr;
}
}
targetType = targetType.BaseType;
}
}
return null;
}
public virtual void OnInspectorGUIStart(SerializedObject scriptSO)
{
scriptSO.Update();
GUI.enabled = false;
AddScript(scriptSO);
GUI.enabled = true;
EditorGUI.BeginChangeCheck();
}
public virtual void OnInspectorGUIEnd(SerializedObject scriptSO)
{
if (EditorGUI.EndChangeCheck())
Repaint();
scriptSO.ApplyModifiedProperties();
}
public override void OnInspectorGUI()
{
SerializedObject llmScriptSO = new SerializedObject(target);
OnInspectorGUIStart(llmScriptSO);
AddOptionsToggles(llmScriptSO);
AddSetupSettings(llmScriptSO);
AddChatSettings(llmScriptSO);
AddModelSettings(llmScriptSO);
OnInspectorGUIEnd(llmScriptSO);
}
}
}