AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
ButtonIconProfileTexture.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.Collections.Generic;
5 using UnityEngine;
6 
7 #if ENABLE_WINMD_SUPPORT && !UNITY_EDITOR
8 using System.Reflection;
9 #endif
10 
11 namespace HoloToolkit.Unity.Buttons
12 {
17  {
18 #if UNITY_EDITOR
19  private static float textureSize = 50f;
20 #endif
21 
22  [Header("Navigation icons")]
23  public Texture2D GlobalNavButton;
24  public Texture2D ChevronUp;
25  public Texture2D ChevronDown;
26  public Texture2D ChevronLeft;
27  public Texture2D ChevronRight;
28  public Texture2D Forward;
29  public Texture2D Back;
30  public Texture2D PageLeft;
31  public Texture2D PageRight;
32 
33  [Header("Common action icons")]
34  public Texture2D Add;
35  public Texture2D Remove;
36  public Texture2D Clear;
37  public Texture2D Cancel;
38  public Texture2D Zoom;
39  public Texture2D Refresh;
40  public Texture2D Lock;
41  public Texture2D Accept;
42  public Texture2D OpenInNewWindow;
43 
44  [Header("Common notification icons")]
45  public Texture2D Completed;
46  public Texture2D Error;
47 
48  [Header("Common object icons")]
49  public Texture2D Contact;
50  public Texture2D Volume;
51  public Texture2D KeyboardClassic;
52  public Texture2D Camera;
53  public Texture2D Video;
54  public Texture2D Microphone;
55 
56  [Header("Common gesture icons")]
57  public Texture2D Ready;
58  public Texture2D AirTap;
59  public Texture2D PressHold;
60  public Texture2D Drag;
61  public Texture2D TapToPlaceArt;
62  public Texture2D AdjustWithHand;
63  public Texture2D AdjustHologram;
64  public Texture2D RemoveHologram;
65 
67  public Texture2D[] CustomIcons;
68 
69  private bool initialized;
70  private List<string> iconKeys;
71  private Dictionary<string, Texture2D> iconLookup;
72 
73  public override bool GetIcon(string iconName, MeshRenderer targetRenderer, MeshFilter targetMesh, bool useDefaultIfNotFound)
74  {
75  Initialize();
76 
77  Texture2D icon = null;
78  if (useDefaultIfNotFound)
79  {
80  icon = _IconNotFound;
81  }
82 
83  if (!string.IsNullOrEmpty(iconName))
84  {
85  // See if the icon exists
86  if (!iconLookup.TryGetValue(iconName, out icon))
87  if (useDefaultIfNotFound)
88  icon = _IconNotFound;
89 
90  if (icon == null)
91  if (useDefaultIfNotFound)
92  icon = _IconNotFound;
93  }
94 
95  // Set the texture on the material
96  targetMesh.sharedMesh = IconMesh;
97  targetMesh.transform.localScale = Vector3.one;
98  targetRenderer.sharedMaterial.mainTexture = icon;
99  return icon != null;
100  }
101 
105  public override List<string> GetIconKeys()
106  {
107  Initialize();
108 
109  return new List<string>(iconKeys);
110  }
111 
112  private void Initialize()
113  {
114  if (iconLookup != null)
115  return;
116 
117  iconLookup = new Dictionary<string, Texture2D>();
118  iconKeys = new List<string>();
119 
120  // Store all icons in iconLookup via reflection
121 #if ENABLE_WINMD_SUPPORT && !UNITY_EDITOR
122  var fields = GetType().GetTypeInfo().DeclaredFields;
123 #else
124  var fields = this.GetType().GetFields();
125 #endif
126  foreach (var field in fields)
127  {
128  if (field.FieldType == typeof(Texture2D) && !field.Name.StartsWith("_"))
129  {
130  iconLookup.Add(field.Name, (Texture2D)field.GetValue(this));
131  iconKeys.Add(field.Name);
132  }
133  }
134 
135  // These icons will override the common icons if they exist, so do them last
136  foreach (Texture2D icon in CustomIcons)
137  {
138  if (iconLookup.ContainsKey(icon.name))
139  {
140  iconLookup[icon.name] = icon;
141  }
142  else
143  {
144  iconLookup.Add(icon.name, icon);
145  iconKeys.Add(icon.name);
146  }
147  }
148  }
149 
150 #if UNITY_EDITOR
151  public override string DrawIconSelectField(string iconName)
152  {
153  int selectedIconIndex = -1;
154  List<string> iconKeys = GetIconKeys();
155  for (int i = 0; i < iconKeys.Count; i++)
156  {
157  if (iconName == iconKeys[i])
158  {
159  selectedIconIndex = i;
160  break;
161  }
162  }
163  int newIconIndex = UnityEditor.EditorGUILayout.Popup("Icon", selectedIconIndex, iconKeys.ToArray());
164  // This will automatically set the icon in the editor view
165  iconName = (newIconIndex < 0 ? string.Empty : iconKeys[newIconIndex]);
166  return iconName;
167  }
168 
169  [UnityEditor.CustomEditor(typeof(ButtonIconProfileTexture))]
170  public class CustomEditor : ProfileInspector
171  {
172  protected override void DrawCustomFooter()
173  {
175  UnityEditor.EditorGUILayout.LabelField("Custom Icons", UnityEditor.EditorStyles.boldLabel);
176 
177  for (int i = 0; i < iconProfile.CustomIcons.Length; i++)
178  {
179  Texture2D icon = iconProfile.CustomIcons[i];
180  icon = (Texture2D)UnityEditor.EditorGUILayout.ObjectField(icon != null ? icon.name : "(Empty)", icon, typeof(Texture2D), false, GUILayout.MaxHeight(textureSize));
181  iconProfile.CustomIcons[i] = icon;
182  }
183 
184  if (GUILayout.Button("Add custom icon"))
185  {
186  System.Array.Resize<Texture2D>(ref iconProfile.CustomIcons, iconProfile.CustomIcons.Length + 1);
187  }
188  }
189  }
190 #endif
191  }
192 
193 }
The base class for button icon profiles
override bool GetIcon(string iconName, MeshRenderer targetRenderer, MeshFilter targetMesh, bool useDefaultIfNotFound)
Searches for an icon If found, the icon texture is applied to the target renderer&#39;s material and the ...
override List< string > GetIconKeys()
(Icons starting with &#39;_&#39; will not be included in icon list)