14 [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
17 public bool AutoFill {
get;
private set; }
18 public bool ShowComponentNames {
get;
private set; }
19 public string CustomLabel {
get;
private set; }
23 ShowComponentNames = showComponentNames;
24 CustomLabel = customLabel;
29 public override void DrawEditor(
UnityEngine.Object target, FieldInfo field, SerializedProperty property)
31 Transform transform = (target as Component).transform;
33 Component componentValue = field.GetValue(target) as Component;
35 Type targetType = field.FieldType;
36 if (targetType == typeof(MonoBehaviour))
37 targetType = typeof(Component);
39 if (componentValue == null && AutoFill)
41 componentValue = transform.GetComponentInChildren(targetType) as Component;
44 componentValue = DropDownComponentField(
45 SplitCamelCase(field.Name),
50 field.SetValue(target, componentValue);
53 public override void DrawEditor(
UnityEngine.Object target, PropertyInfo prop)
55 Transform transform = (target as Component).transform;
57 Component componentValue = prop.GetValue(target, null) as Component;
59 Type targetType = prop.PropertyType;
60 if (targetType == typeof(MonoBehaviour))
61 targetType = typeof(Component);
63 if (componentValue == null && AutoFill)
65 componentValue = transform.GetComponentInChildren(targetType) as Component;
68 componentValue = DropDownComponentField(
69 SplitCamelCase(prop.Name),
74 prop.SetValue(target, componentValue, null);
77 private static Component DropDownComponentField(
string label, Component obj, Type componentType, Transform transform,
bool showComponentName =
false)
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++)
85 if (showComponentName)
87 options[i + 1] = optionObjects[i].GetType().Name +
" (" + optionObjects[i].name +
")";
91 options[i + 1] = optionObjects[i].name;
93 if (obj == optionObjects[i])
95 selectedIndex = i + 1;
99 EditorGUILayout.BeginHorizontal();
100 int newIndex = EditorGUILayout.Popup(label, selectedIndex, options);
108 obj = optionObjects[newIndex - 1];
112 obj = (Component)EditorGUILayout.ObjectField(obj, componentType,
true);
113 EditorGUILayout.EndHorizontal();