Skip to content

Commit 9009654

Browse files
Stephen Hodgsonkeveleigh
authored andcommitted
Updates to EditorGUI Extensions in Utilities folder. (#410)
* Updated EditorGUILayoutExtensions to also handle ObjectFields with Material, SceneAssets, and Generic UnityEngine.Objects. Cleaned up EditorGUIExtionsions and added SceneAsset and Material types to ObjectField * EditorGUILayoutExtensions: Better GUIContent options.
1 parent 05903ba commit 9009654

File tree

2 files changed

+52
-14
lines changed

2 files changed

+52
-14
lines changed

Assets/HoloToolkit/Utilities/Editor/EditorGUIExtensions.cs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@ public static float Indent
2929

3030
public static bool Button(Rect position, string text)
3131
{
32-
return EditorGUIExtensions.Button(position, new GUIContent(text));
32+
return Button(position, new GUIContent(text));
3333
}
3434

3535
public static bool Button(Rect position, GUIContent content)
3636
{
37-
float indent = EditorGUIExtensions.Indent;
37+
float indent = Indent;
3838
position.x += indent;
3939
position.width -= indent;
4040
return GUI.Button(position, content);
4141
}
4242

4343
public static void Label(Rect position, string text)
4444
{
45-
float indent = EditorGUIExtensions.Indent;
45+
float indent = Indent;
4646
position.x += indent;
4747
position.width -= indent;
4848
GUI.Label(position, text);
@@ -52,21 +52,19 @@ public static void Label(Rect position, string text)
5252
// Doesn't handle serialized types with nested serialized children.
5353
public static float GetTypeHeight(bool hasLabel, Type valueType)
5454
{
55-
if (valueType == typeof(Vector3) ||
56-
valueType == typeof(Vector2))
55+
if (valueType == typeof(Vector3) || valueType == typeof(Vector2))
5756
{
58-
return (!hasLabel || EditorGUIUtility.wideMode ? 0f : EditorGUIUtility.singleLineHeight) +
59-
EditorGUIUtility.singleLineHeight;
57+
return (!hasLabel || EditorGUIUtility.wideMode ? 0f : EditorGUIUtility.singleLineHeight) + EditorGUIUtility.singleLineHeight;
6058
}
61-
else if (valueType == typeof(Rect))
59+
60+
if (valueType == typeof(Rect))
6261
{
63-
return (!hasLabel || EditorGUIUtility.wideMode ? 0f : EditorGUIUtility.singleLineHeight) +
64-
EditorGUIUtility.singleLineHeight * 2;
62+
return (!hasLabel || EditorGUIUtility.wideMode ? 0f : EditorGUIUtility.singleLineHeight) + EditorGUIUtility.singleLineHeight * 2;
6563
}
66-
else if (valueType == typeof(Bounds))
64+
65+
if (valueType == typeof(Bounds))
6766
{
68-
return (!hasLabel ? 0f : EditorGUIUtility.singleLineHeight) +
69-
EditorGUIUtility.singleLineHeight * 2;
67+
return (!hasLabel ? 0f : EditorGUIUtility.singleLineHeight) + EditorGUIUtility.singleLineHeight * 2;
7068
}
7169

7270
return EditorGUIUtility.singleLineHeight;
@@ -83,7 +81,7 @@ public static float GetTypeHeight(bool hasLabel, Type valueType)
8381
/// <returns>The new value.</returns>
8482
public static T ObjectField<T>(Rect position, GUIContent label, T value, bool allowSceneObjects)
8583
{
86-
object objValue = (object)value;
84+
object objValue = value;
8785

8886
Type valueType = objValue.GetType();
8987
if (valueType == typeof(Bounds))
@@ -94,6 +92,10 @@ public static T ObjectField<T>(Rect position, GUIContent label, T value, bool al
9492
{
9593
objValue = EditorGUI.ColorField(position, label, (Color)objValue);
9694
}
95+
else if (valueType == typeof(Material))
96+
{
97+
objValue = EditorGUI.ObjectField(position, (Material)objValue, typeof(Material), allowSceneObjects);
98+
}
9799
else if (valueType == typeof(AnimationCurve))
98100
{
99101
objValue = EditorGUI.CurveField(position, label, (AnimationCurve)objValue);
@@ -151,6 +153,10 @@ public static T ObjectField<T>(Rect position, GUIContent label, T value, bool al
151153

152154
objValue = EditorGUI.Vector4Field(position, label.text, (Vector4)objValue);
153155
}
156+
else if (Equals(objValue, typeof(SceneAsset)))
157+
{
158+
objValue = EditorGUI.ObjectField(position, (SceneAsset)objValue, typeof(SceneAsset), allowSceneObjects);
159+
}
154160
else if (objValue is UnityEngine.Object)
155161
{
156162
objValue = EditorGUI.ObjectField(position, label, (UnityEngine.Object)objValue, valueType, allowSceneObjects);

Assets/HoloToolkit/Utilities/Editor/EditorGUILayoutExtensions.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4+
using System;
5+
using System.Globalization;
46
using UnityEditor;
57
using UnityEngine;
68

@@ -37,5 +39,35 @@ public static void Label(string text, GUIStyle style, params GUILayoutOption[] o
3739
GUILayout.Label(text, style, options);
3840
EditorGUILayout.EndHorizontal();
3941
}
42+
43+
public static T ObjectField<T>(GUIContent guiContent, T value, bool allowSceneObjects, GUILayoutOption[] guiLayoutOptions = null)
44+
{
45+
object objValue = value;
46+
47+
Type valueType = objValue.GetType();
48+
if (valueType == typeof(Material))
49+
{
50+
objValue = EditorGUILayout.ObjectField(guiContent, (Material)objValue, typeof(Material), allowSceneObjects, guiLayoutOptions);
51+
}
52+
else if (valueType == typeof(SceneAsset))
53+
{
54+
objValue = EditorGUILayout.ObjectField(guiContent, (SceneAsset)objValue, typeof(SceneAsset), allowSceneObjects, guiLayoutOptions);
55+
}
56+
else if (objValue is UnityEngine.Object)
57+
{
58+
objValue = EditorGUILayout.ObjectField(guiContent, (UnityEngine.Object)objValue, valueType, allowSceneObjects, guiLayoutOptions);
59+
}
60+
else
61+
{
62+
throw new ArgumentException(
63+
string.Format(
64+
CultureInfo.InvariantCulture,
65+
"Unimplemented value type: {0}.",
66+
valueType),
67+
"value");
68+
}
69+
70+
return (T)objValue;
71+
}
4072
}
4173
}

0 commit comments

Comments
 (0)