AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
EditablePropAttribute.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 #if UNITY_EDITOR
7 using UnityEditor;
8 #endif
9 
10 namespace HoloToolkit.Unity
11 {
12  // Displays a prop as editable in the inspector
13  [AttributeUsage(AttributeTargets.Property)]
15  {
16  public string CustomLabel { get; private set; }
17 
18  public EditablePropAttribute(string customLabel = null)
19  {
20  CustomLabel = customLabel;
21  }
22 
23 #if UNITY_EDITOR
24  public override void DrawEditor(UnityEngine.Object target, FieldInfo field, SerializedProperty property)
25  {
26  // (safe since this is property-only attribute)
27  throw new NotImplementedException();
28  }
29 
30  public override void DrawEditor(UnityEngine.Object target, PropertyInfo prop)
31  {
32  switch (prop.PropertyType.Name)
33  {
34  case "Boolean":
35  bool boolValue = (bool)prop.GetValue(target, null);
36  boolValue = EditorGUILayout.Toggle(!string.IsNullOrEmpty (CustomLabel) ? CustomLabel : SplitCamelCase(prop.Name), boolValue);
37  prop.SetValue(target, boolValue, null);
38  break;
39 
40  case "Int32":
41  int intValue = (int)prop.GetValue(target, null);
42  intValue = EditorGUILayout.IntField(!string.IsNullOrEmpty(CustomLabel) ? CustomLabel : SplitCamelCase(prop.Name), intValue);
43  prop.SetValue(target, intValue, null);
44  break;
45 
46  case "Single":
47  float singleValue = (float)prop.GetValue(target, null);
48  singleValue = EditorGUILayout.FloatField(!string.IsNullOrEmpty(CustomLabel) ? CustomLabel : SplitCamelCase(prop.Name), singleValue);
49  prop.SetValue(target, singleValue, null);
50  break;
51 
52  default:
53  throw new NotImplementedException("No drawer for type " + prop.PropertyType.Name);
54  }
55  }
56 #endif
57 
58  }
59 }