AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
EditorGUIExtensions.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using System;
5 using System.Globalization;
6 using UnityEditor;
7 using UnityEngine;
8 
9 namespace HoloToolkit.Unity
10 {
14  public class EditorGUIExtensions : MonoBehaviour
15  {
16  public static float Indent
17  {
18  get
19  {
20  return EditorGUI.IndentedRect(new Rect()).x;
21  }
22  }
23 
28  public const float HorizontalSpacing = 6.0f;
29 
30  public static bool Button(Rect position, string text)
31  {
32  return Button(position, new GUIContent(text));
33  }
34 
35  public static bool Button(Rect position, GUIContent content)
36  {
37  float indent = Indent;
38  position.x += indent;
39  position.width -= indent;
40  return GUI.Button(position, content);
41  }
42 
43  public static void Label(Rect position, string text)
44  {
45  float indent = Indent;
46  position.x += indent;
47  position.width -= indent;
48  GUI.Label(position, text);
49  }
50 
51  // Based on logic in Unity source function EditorGUI.GetSinglePropertyHeight().
52  // Doesn't handle serialized types with nested serialized children.
53  public static float GetTypeHeight(bool hasLabel, Type valueType)
54  {
55  if (valueType == typeof(Vector3) || valueType == typeof(Vector2))
56  {
57  return (!hasLabel || EditorGUIUtility.wideMode ? 0f : EditorGUIUtility.singleLineHeight) + EditorGUIUtility.singleLineHeight;
58  }
59 
60  if (valueType == typeof(Rect))
61  {
62  return (!hasLabel || EditorGUIUtility.wideMode ? 0f : EditorGUIUtility.singleLineHeight) + EditorGUIUtility.singleLineHeight * 2;
63  }
64 
65  if (valueType == typeof(Bounds))
66  {
67  return (!hasLabel ? 0f : EditorGUIUtility.singleLineHeight) + EditorGUIUtility.singleLineHeight * 2;
68  }
69 
70  return EditorGUIUtility.singleLineHeight;
71  }
72 
82  public static T ObjectField<T>(Rect position, GUIContent label, T value, bool allowSceneObjects)
83  {
84  object objValue = value;
85 
86  Type valueType = objValue.GetType();
87  if (valueType == typeof(Bounds))
88  {
89  objValue = EditorGUI.BoundsField(position, label, (Bounds)objValue);
90  }
91  else if (valueType == typeof(Color))
92  {
93  objValue = EditorGUI.ColorField(position, label, (Color)objValue);
94  }
95  else if (valueType == typeof(Material))
96  {
97  objValue = EditorGUI.ObjectField(position, (Material)objValue, typeof(Material), allowSceneObjects);
98  }
99  else if (valueType == typeof(AnimationCurve))
100  {
101  objValue = EditorGUI.CurveField(position, label, (AnimationCurve)objValue);
102  }
103  else if (valueType == typeof(float))
104  {
105  objValue = EditorGUI.FloatField(position, label, (float)objValue);
106  }
107  else if (valueType == typeof(int))
108  {
109  objValue = EditorGUI.IntField(position, label, (int)objValue);
110  }
111  else if (valueType == typeof(LayerMask))
112  {
113  objValue = EditorGUI.MaskField(position, label, (LayerMask)objValue, LayerMaskExtensions.LayerMaskNames);
114  }
115  else if (valueType.IsEnum)
116  {
117  if (valueType.GetCustomAttributes(typeof(FlagsAttribute), true).Length > 0)
118  {
119 #if UNITY_2017_3_OR_NEWER
120  objValue = EditorGUI.EnumFlagsField(position, label, (Enum)objValue);
121 #else
122  objValue = EditorGUI.EnumMaskField(position, label, (Enum)objValue);
123 #endif
124  }
125  else
126  {
127  objValue = EditorGUI.EnumPopup(position, label, (Enum)objValue);
128  }
129  }
130  else if (valueType == typeof(Rect))
131  {
132  objValue = EditorGUI.RectField(position, label, (Rect)objValue);
133  }
134  else if (valueType == typeof(string))
135  {
136  objValue = EditorGUI.TextField(position, label, (string)objValue);
137  }
138  else if (valueType == typeof(Vector2))
139  {
140  objValue = EditorGUI.Vector2Field(position, new GUIContent(), (Vector2)objValue);
141  }
142  else if (valueType == typeof(Vector3))
143  {
144  objValue = EditorGUI.Vector3Field(position, new GUIContent(), (Vector3)objValue);
145  }
146  else if (valueType == typeof(Vector4))
147  {
148  if (label.image != null)
149  {
150  throw new ArgumentException("Images not supported for labels of Vector4 fields.", "label");
151  }
152 
153  if (!string.IsNullOrEmpty(label.tooltip))
154  {
155  throw new ArgumentException("Tool-tips not supported for labels of Vector4 fields.", "label");
156  }
157 
158  objValue = EditorGUI.Vector4Field(position, label.text, (Vector4)objValue);
159  }
160  else if (Equals(objValue, typeof(SceneAsset)))
161  {
162  objValue = EditorGUI.ObjectField(position, (SceneAsset)objValue, typeof(SceneAsset), allowSceneObjects);
163  }
164  else if (objValue is UnityEngine.Object)
165  {
166  objValue = EditorGUI.ObjectField(position, label, (UnityEngine.Object)objValue, valueType, allowSceneObjects);
167  }
168  else
169  {
170  throw new ArgumentException(
171  string.Format(
172  CultureInfo.InvariantCulture,
173  "Unimplemented value type: {0}.",
174  valueType),
175  "value");
176  }
177 
178  return (T)objValue;
179  }
180  }
181 }
Extensions for the UnityEngine.LayerMask class.
static bool Button(Rect position, string text)
UnityEngine.UI.Button Button
Extensions for the UnityEnditor.EditorGUI class.
static bool Button(Rect position, GUIContent content)
static float GetTypeHeight(bool hasLabel, Type valueType)
static void Label(Rect position, string text)