generated from Dabblesoft-Dan/unity-package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomVectorsCustomEditorExamples.cs
306 lines (221 loc) · 13.4 KB
/
CustomVectorsCustomEditorExamples.cs
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
using UnityEditor;
using UnityEngine;
using EditorGUICV = Dabblesoft.Unity.Editor.CustomVectors.EditorGUI;
using EditorGUILayoutCV = Dabblesoft.Unity.Editor.CustomVectors.EditorGUILayout;
[CanEditMultipleObjects]
[CustomEditor(typeof(CustomVectorsCustomEditorExamples))]
public class CustomVectorsCustomEditorExamplesEditor : Editor
{
SerializedProperty customV2;
SerializedProperty customV2Layout;
SerializedProperty customVector2Expanded;
SerializedProperty customV3;
SerializedProperty customV3Layout;
SerializedProperty customV4;
SerializedProperty customV4Layout;
SerializedProperty customVector4Expanded;
SerializedProperty customV4Stacked;
SerializedProperty customV4StackedLayout;
SerializedProperty customVector4StackedExpanded;
SerializedProperty customMulti;
SerializedProperty customMultiLayout;
SerializedProperty customMultiExpanded;
void OnEnable()
{
customV2 = serializedObject.FindProperty("customV2");
customV2Layout = serializedObject.FindProperty("customV2Layout");
customVector2Expanded = serializedObject.FindProperty("customVector2Expanded");
customV3 = serializedObject.FindProperty("customV3");
customV3Layout = serializedObject.FindProperty("customV3Layout");
customV4 = serializedObject.FindProperty("customV4");
customV4Layout = serializedObject.FindProperty("customV4Layout");
customVector4Expanded = serializedObject.FindProperty("customVector4Expanded");
customV4Stacked = serializedObject.FindProperty("customV4Stacked");
customV4StackedLayout = serializedObject.FindProperty("customV4StackedLayout");
customVector4StackedExpanded = serializedObject.FindProperty("customVector4StackedExpanded");
customMulti = serializedObject.FindProperty("customMulti");
customMultiLayout = serializedObject.FindProperty("customMultiLayout");
customMultiExpanded = serializedObject.FindProperty("customMultiExpanded");
}
public override void OnInspectorGUI()
{
serializedObject.Update();
EditorGUILayout.LabelField("Vector2 Examples", EditorStyles.boldLabel);
// Create a Custom Vector2 or Vector2Int field.
float v2Height = EditorGUI.GetPropertyHeight(SerializedPropertyType.Vector2, new GUIContent());
if (!EditorGUIUtility.wideMode) v2Height += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
Rect v2Rect = EditorGUILayout.GetControlRect(true, v2Height, EditorStyles.numberField);
customV2.vector2Value = EditorGUICV.Vector2Field(v2Rect, "CustomV2", customV2.vector2Value, "xLabel", "yLabel");
customV2Layout.vector2Value = EditorGUILayoutCV.Vector2Field("CustomV2Layout", customV2Layout.vector2Value, xLabel: "xLabel");
customVector2Expanded.vector2Value = EditorGUILayoutCV.Vector2Field("CustomV2Layout Expanded", customVector2Expanded.vector2Value, xLabel: "xLabel", expandWidth: true);
// Can also be used with Vector2Int.
//customV2.vector2IntValue = EditorGUICV.Vector2IntField(v2Rect, "CustomV2", customV2.vector2IntValue, "xLabel", "yLabel");
//customV2Layout.vector2IntValue = EditorGUILayoutCV.Vector2IntField("CustomV2Layout", customV2Layout.vector2IntValue, xLabel: "xLabel");
//customVector2Expanded.vector2IntValue = EditorGUILayoutCV.Vector2IntField("CustomV2Layout Expanded", customVector2Expanded.vector2IntValue, xLabel: "xLabel", expandWidth: true);
EditorGUILayout.Space();
EditorGUILayout.LabelField("Vector3 Examples", EditorStyles.boldLabel);
// Create a Custom Vector3 or Vector3Int field.
float v3Height = EditorGUI.GetPropertyHeight(SerializedPropertyType.Vector3, new GUIContent());
if (!EditorGUIUtility.wideMode) v3Height += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
Rect v3Rect = EditorGUILayout.GetControlRect(true, v3Height, EditorStyles.numberField);
customV3.vector3Value = EditorGUICV.Vector3Field(v3Rect, "CustomV3", customV3.vector3Value, "xLabel", "yLabel", "zLabel");
customV3Layout.vector3Value = EditorGUILayoutCV.Vector3Field("CustomV3Layout", customV3Layout.vector3Value, xLabel: "xLabel", zLabel: "zLabel");
// Can also be used with Vector3Int.
//customV3.vector3IntValue = EditorGUICV.Vector3IntField(v3Rect, "CustomV3", customV3.vector3IntValue, "xLabel", "yLabel", "zLabel");
//customV3Layout.vector3IntValue = EditorGUILayoutCV.Vector3IntField("CustomV3Layout", customV3Layout.vector3IntValue, xLabel: "xLabel", zLabel: "zLabel");
EditorGUILayout.Space();
EditorGUILayout.LabelField("Vector4 Examples", EditorStyles.boldLabel);
// Create a Custom Vector4 field.
float v4Height = EditorGUI.GetPropertyHeight(SerializedPropertyType.Vector4, new GUIContent());
if (!EditorGUIUtility.wideMode) v4Height += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
Rect v4Rect = EditorGUILayout.GetControlRect(true, v4Height, EditorStyles.numberField);
customV4.vector4Value = EditorGUICV.Vector4Field(v4Rect, "CustomV4", customV4.vector4Value, "xLabel", "yLabel", "zLabel", "wLabel", false);
customV4Layout.vector4Value = EditorGUILayoutCV.Vector4Field("CustomV4Layout", customV4Layout.vector4Value, xLabel: "xLabel", zLabel: "zLabel", expandWidth: false);
customVector4Expanded.vector4Value = EditorGUILayoutCV.Vector4Field("CustomV4Layout Expanded", customVector4Expanded.vector4Value, xLabel: "xLabel", zLabel: "zLabel");
EditorGUILayout.Space();
EditorGUILayout.LabelField("Vector4Stacked Examples", EditorStyles.boldLabel);
// Create a Custom Vector4 field that displays as stacked fields.
float v4StackedHeight = EditorGUI.GetPropertyHeight(SerializedPropertyType.Vector4, new GUIContent()) * 2;
v4StackedHeight = EditorGUIUtility.wideMode ?
v4StackedHeight + EditorGUIUtility.standardVerticalSpacing :
v4StackedHeight + EditorGUIUtility.singleLineHeight + (EditorGUIUtility.standardVerticalSpacing * 2);
Rect v4StackedRect = EditorGUILayout.GetControlRect(true, v4StackedHeight, EditorStyles.numberField);
customV4Stacked.vector4Value = EditorGUICV.Vector4Field(v4StackedRect, "CustomV4Stacked", customV4Stacked.vector4Value, "xLabel", "yLabel", "zLabel", "wLabel", false, true);
customV4StackedLayout.vector4Value = EditorGUILayoutCV.Vector4Field("CustomV4StackedLayout", customV4StackedLayout.vector4Value, xLabel: "xLabel", zLabel: "zLabel", expandWidth: false, stackFields: true);
customVector4StackedExpanded.vector4Value = EditorGUILayoutCV.Vector4Field("CustomV4StackedLayout Expanded", customVector4StackedExpanded.vector4Value, xLabel: "xLabel", zLabel: "zLabel", stackFields: true);
EditorGUILayout.Space();
EditorGUILayout.LabelField("Multi Examples", EditorStyles.boldLabel);
// Create a Custom MultiFloat or MultiInt field.
var subLabels = new string[]
{
"label1", "label2", "label3", "label4", "label5",
"label6", "label7"
};
var subLabelsLayout = new string[]
{
"label1", "label2", "label3", "label4", "label5",
"label6", "label7"
};
int columns = 5;
int rows = 2;
float mfHeight = EditorGUI.GetPropertyHeight(SerializedPropertyType.Vector3, new GUIContent()) * rows;
mfHeight = EditorGUIUtility.wideMode ?
mfHeight + (EditorGUIUtility.standardVerticalSpacing * (rows - 1)) :
mfHeight + EditorGUIUtility.singleLineHeight + (EditorGUIUtility.standardVerticalSpacing * rows);
Rect mfRect = EditorGUILayout.GetControlRect(true, mfHeight, EditorStyles.numberField);
var multiValues = new float[customMulti.arraySize];
for (int i = 0; i < customMulti.arraySize; i++)
{
multiValues[i] = customMulti.GetArrayElementAtIndex(i).floatValue;
}
EditorGUICV.MultiFloatField(mfRect, "CustomMultiFloat", subLabels, multiValues, columns);
for (int i = 0; i < multiValues.Length; i++)
{
customMulti.GetArrayElementAtIndex(i).floatValue = multiValues[i];
}
var multiLayoutValues = new float[customMultiLayout.arraySize];
for (int i = 0; i < customMultiLayout.arraySize; i++)
{
multiLayoutValues[i] = customMultiLayout.GetArrayElementAtIndex(i).floatValue;
}
EditorGUILayoutCV.MultiFloatField("CustomMultiFloatLayout", subLabelsLayout, multiLayoutValues, columns);
for (int i = 0; i < multiLayoutValues.Length; i++)
{
customMultiLayout.GetArrayElementAtIndex(i).floatValue = multiLayoutValues[i];
}
var multiExpandedValues = new float[customMultiExpanded.arraySize];
for (int i = 0; i < customMultiExpanded.arraySize; i++)
{
multiExpandedValues[i] = customMultiExpanded.GetArrayElementAtIndex(i).floatValue;
}
EditorGUILayoutCV.MultiFloatField("CustomMultiFloatLayout Expanded", subLabelsLayout, multiExpandedValues, columns, true);
for (int i = 0; i < multiExpandedValues.Length; i++)
{
customMultiExpanded.GetArrayElementAtIndex(i).floatValue = multiExpandedValues[i];
}
// Can also be used with ints.
/*var multiValues = new int[customMulti.arraySize];
for (int i = 0; i < customMulti.arraySize; i++)
{
multiValues[i] = customMulti.GetArrayElementAtIndex(i).intValue;
}
EditorGUICV.MultiIntField(mfRect, "CustomVectors MultiInt", subLabels, multiValues, columns);
for (int i = 0; i < multiValues.Length; i++)
{
customMulti.GetArrayElementAtIndex(i).intValue = multiValues[i];
}
var multiLayoutValues = new int[customMultiLayout.arraySize];
for (int i = 0; i < customMultiLayout.arraySize; i++)
{
multiLayoutValues[i] = customMultiLayout.GetArrayElementAtIndex(i).intValue;
}
EditorGUILayoutCV.MultiIntField("CustomVectors MultiIntLayout", subLabelsLayout, multiLayoutValues, columns);
for (int i = 0; i < multiLayoutValues.Length; i++)
{
customMultiLayout.GetArrayElementAtIndex(i).intValue = multiLayoutValues[i];
}
var multiExpandedValues = new int[customMultiExpanded.arraySize];
for (int i = 0; i < customMultiExpanded.arraySize; i++)
{
multiExpandedValues[i] = customMultiExpanded.GetArrayElementAtIndex(i).intValue;
}
EditorGUILayoutCV.MultiIntField("CustomVectors MultiIntLayout Expanded", subLabelsLayout, multiExpandedValues, columns, true);
for (int i = 0; i < multiExpandedValues.Length; i++)
{
customMultiExpanded.GetArrayElementAtIndex(i).intValue = multiExpandedValues[i];
}*/
serializedObject.ApplyModifiedProperties();
}
}
[ExecuteAlways]
public class CustomVectorsCustomEditorExamples : MonoBehaviour
{
public Vector2 customV2 = Vector2.one;
public Vector2 customV2Layout = Vector2.one;
public Vector2 customVector2Expanded = Vector2.one;
// Can also be used with Vector2Int.
//public Vector2Int customV2 = Vector2Int.one;
//public Vector2Int customV2Layout = Vector2Int.one;
//public Vector2Int customVector2Expanded = Vector2Int.one;
public Vector3 customV3 = Vector3.one;
public Vector3 customV3Layout = Vector3.one;
// Can also be used with Vector3Int.
//public Vector3Int customV3 = Vector3Int.one;
//public Vector3Int customV3Layout = Vector3Int.one;
public Vector4 customV4 = Vector4.one;
public Vector4 customV4Layout = Vector4.one;
public Vector4 customVector4 = Vector4.one;
public Vector4 customVector4Expanded = Vector4.one;
public Vector4 customV4Stacked = Vector4.one;
public Vector4 customV4StackedLayout = Vector4.one;
public Vector4 customVector4StackedExpanded = Vector4.one;
public float[] customMulti = new float[] { 1, 2, 3, 4, 5, 6, 7 };
public float[] customMultiLayout = new float[] { 1, 2, 3, 4, 5, 6, 7 };
public float[] customMultiExpanded = new float[] { 1, 2, 3, 4, 5, 6, 7 };
// Can also be used with Int[].
//public int[] customMulti = new int[] { 1, 2, 3, 4, 5, 6, 7 };
//public int[] customMultiLayout = new int[] { 1, 2, 3, 4, 5, 6, 7 };
//public int[] customMultiExpanded = new int[] { 1, 2, 3, 4, 5, 6, 7 };
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//Debug.Log("customV2: " + customV2);
//Debug.Log("customV2Layout: " + customV2Layout);
//Debug.Log("customVector2Expanded: " + customVector2Expanded);
//Debug.Log("customV3: " + customV3);
//Debug.Log("customV3Layout: " + customV3Layout);
//Debug.Log("customVector4: " + customVector4);
//Debug.Log("customV4Layout: " + customV4Layout);
//Debug.Log("customVector4Expanded: " + customVector4Expanded);
//Debug.Log("customV4Stacked: " + customV4Stacked);
//Debug.Log("customV4StackedLayout: " + customV4StackedLayout);
//Debug.Log("customVector4StackedExpanded: " + customVector4StackedExpanded);
//Debug.Log("customMulti: " + customMulti);
//Debug.Log("customMultiLayout: " + customMultiLayout);
//Debug.Log("customMultiExpanded: " + customMultiExpanded);
}
}