Tanoda
ImplementsTypeNameDropdown.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System;
10using System.Collections;
11using System.Collections.Generic;
12#if UNITY_EDITOR
13using UnityEditor;
14#endif
15using UnityEngine;
16
17namespace Leap.Unity.Attributes {
18
28
29 protected Type _baseType;
30 protected List<Type> _implementingTypes = new List<Type>();
31 protected GUIContent[] _typeOptions;
32
34 _baseType = type;
35
36#if UNITY_EDITOR
37 refreshImplementingTypes();
38 refreshTypeOptions();
39#endif
40 }
41
42#if UNITY_EDITOR
43 public void DrawProperty(Rect rect, SerializedProperty property, GUIContent label) {
44 int curSelectedIdx = getCurSelectedIdx(property);
45
46 int selectedIdx = EditorGUI.Popup(rect, label, curSelectedIdx, _typeOptions);
47 if (selectedIdx != curSelectedIdx) {
48 property.stringValue = _implementingTypes[selectedIdx].FullName;
49 }
50
51 if (curSelectedIdx == -1 && _implementingTypes.Count > 0) {
52 curSelectedIdx = 0;
53 property.stringValue = _implementingTypes[curSelectedIdx].FullName;
54 }
55 }
56
57 private void refreshImplementingTypes() {
58 _implementingTypes.Clear();
59
60 foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) {
61 foreach (var type in assembly.GetTypes()) {
62 if (_baseType.IsAssignableFrom(type) && !type.IsAbstract && !type.IsInterface) {
63 _implementingTypes.Add(type);
64 }
65 }
66 }
67 }
68
69 private void refreshTypeOptions() {
70 _typeOptions = new GUIContent[_implementingTypes.Count];
71
72 for (int i = 0; i < _typeOptions.Length; i++) {
73 _typeOptions[i] = new GUIContent(_implementingTypes[i].Name);
74 }
75 }
76
77 private int getCurSelectedIdx(SerializedProperty property) {
78 return _implementingTypes.FindIndex((t => property.stringValue.Equals(t.FullName)));
79 }
80#endif
81 }
82
83
84}
Place this attribute on a serialized string field to have it render as a dropdown menu that is automa...