AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
DropDownComponentAttribute.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 Component objects that are limited to the target object
14  [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
16  {
17  public bool AutoFill { get; private set; }
18  public bool ShowComponentNames { get; private set; }
19  public string CustomLabel { get; private set; }
20 
21  public DropDownComponentAttribute(bool showComponentNames = false, bool autoFill = false, string customLabel = null)
22  {
23  ShowComponentNames = showComponentNames;
24  CustomLabel = customLabel;
25  AutoFill = autoFill;
26  }
27 
28 #if UNITY_EDITOR
29  public override void DrawEditor(UnityEngine.Object target, FieldInfo field, SerializedProperty property)
30  {
31  Transform transform = (target as Component).transform;
32 
33  Component componentValue = field.GetValue(target) as Component;
34 
35  Type targetType = field.FieldType;
36  if (targetType == typeof(MonoBehaviour))
37  targetType = typeof(Component);
38 
39  if (componentValue == null && AutoFill)
40  {
41  componentValue = transform.GetComponentInChildren(targetType) as Component;
42  }
43 
44  componentValue = DropDownComponentField(
45  SplitCamelCase(field.Name),
46  componentValue,
47  targetType,
48  transform,
49  ShowComponentNames);
50  field.SetValue(target, componentValue);
51  }
52 
53  public override void DrawEditor(UnityEngine.Object target, PropertyInfo prop)
54  {
55  Transform transform = (target as Component).transform;
56 
57  Component componentValue = prop.GetValue(target, null) as Component;
58 
59  Type targetType = prop.PropertyType;
60  if (targetType == typeof(MonoBehaviour))
61  targetType = typeof(Component);
62 
63  if (componentValue == null && AutoFill)
64  {
65  componentValue = transform.GetComponentInChildren(targetType) as Component;
66  }
67 
68  componentValue = DropDownComponentField(
69  SplitCamelCase(prop.Name),
70  componentValue,
71  targetType,
72  transform,
73  ShowComponentNames);
74  prop.SetValue(target, componentValue, null);
75  }
76 
77  private static Component DropDownComponentField(string label, Component obj, Type componentType, Transform transform, bool showComponentName = false)
78  {
79  Component[] optionObjects = transform.GetComponentsInChildren(componentType, true);
80  int selectedIndex = 0;
81  string[] options = new string[optionObjects.Length + 1];
82  options[0] = "(None)";
83  for (int i = 0; i < optionObjects.Length; i++)
84  {
85  if (showComponentName)
86  {
87  options[i + 1] = optionObjects[i].GetType().Name + " (" + optionObjects[i].name + ")";
88  }
89  else
90  {
91  options[i + 1] = optionObjects[i].name;
92  }
93  if (obj == optionObjects[i])
94  {
95  selectedIndex = i + 1;
96  }
97  }
98 
99  EditorGUILayout.BeginHorizontal();
100  int newIndex = EditorGUILayout.Popup(label, selectedIndex, options);
101  if (newIndex == 0)
102  {
103  // Zero means '(None)'
104  obj = null;
105  }
106  else
107  {
108  obj = optionObjects[newIndex - 1];
109  }
110 
111  //draw the object field so people can click it
112  obj = (Component)EditorGUILayout.ObjectField(obj, componentType, true);
113  EditorGUILayout.EndHorizontal();
114 
115  return obj;
116  }
117 #endif
118  }
119 }
DropDownComponentAttribute(bool showComponentNames=false, bool autoFill=false, string customLabel=null)