AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
DropDownGameObjectAttribute.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.Reflection;
6 using UnityEngine;
7 #if UNITY_EDITOR
8 using UnityEditor;
9 #endif
10 
11 namespace HoloToolkit.Unity
12 {
13  // Displays a drop-down menu of GameObjects that are limited to the target object
14  [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
16  {
17  public string CustomLabel { get; private set; }
18 
19  public DropDownGameObjectAttribute(string customLabel = null)
20  {
21  CustomLabel = customLabel;
22  }
23 
24 #if UNITY_EDITOR
25  public override void DrawEditor(UnityEngine.Object target, FieldInfo field, SerializedProperty property)
26  {
27  Transform transform = (target as Component).transform;
28 
29  GameObject fieldValue = field.GetValue(target) as GameObject;
30  fieldValue = DropDownGameObjectField(
31  SplitCamelCase(field.Name),
32  fieldValue,
33  transform);
34  field.SetValue(target, fieldValue);
35  }
36 
37  public override void DrawEditor(UnityEngine.Object target, PropertyInfo prop)
38  {
39  Transform transform = (target as Component).transform;
40 
41  GameObject propValue = prop.GetValue(target, null) as GameObject;
42  propValue = DropDownGameObjectField(
43  SplitCamelCase(prop.Name),
44  propValue,
45  transform);
46  prop.SetValue(target, propValue, null);
47  }
48 
49  private static GameObject DropDownGameObjectField(string label, GameObject obj, Transform transform)
50  {
51  Transform[] optionObjects = transform.GetComponentsInChildren<Transform>(true);
52  int selectedIndex = 0;
53  string[] options = new string[optionObjects.Length + 1];
54  options[0] = "(None)";
55  for (int i = 0; i < optionObjects.Length; i++)
56  {
57  options[i + 1] = optionObjects[i].name;
58  if (obj == optionObjects[i].gameObject)
59  {
60  selectedIndex = i + 1;
61  }
62  }
63 
64  EditorGUILayout.BeginHorizontal();
65  int newIndex = EditorGUILayout.Popup(label, selectedIndex, options);
66  if (newIndex == 0)
67  {
68  // Zero means '(None)'
69  obj = null;
70  }
71  else
72  {
73  obj = optionObjects[newIndex - 1].gameObject;
74  }
75 
76  //draw the object field so people can click it
77  obj = (GameObject)EditorGUILayout.ObjectField(obj, typeof(GameObject), true);
78  EditorGUILayout.EndHorizontal();
79 
80  return obj;
81  }
82 #endif
83 
84  }
85 }