5 using System.Collections.Generic;
15 [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
26 public string Property {
get;
private set; }
28 public string MaterialMemberName {
get;
private set; }
29 public bool AllowNone {
get;
private set; }
30 public string DefaultProperty {
get;
private set; }
31 public string CustomLabel {
get;
private set; }
35 PropertyType = propertyType;
36 MaterialMemberName = materialMemberName;
37 AllowNone = allowNone;
38 DefaultProperty = defaultProperty;
39 CustomLabel = customLabel;
43 public override void DrawEditor(
UnityEngine.Object target, FieldInfo field, SerializedProperty property)
45 Material mat = GetMaterial(target);
47 string fieldValue = MaterialPropertyName(
48 (
string)field.GetValue(target),
53 (
string.IsNullOrEmpty(CustomLabel) ? SplitCamelCase(field.Name) : CustomLabel));
55 field.SetValue(target, fieldValue);
58 public override void DrawEditor(
UnityEngine.Object target, PropertyInfo prop)
60 Material mat = GetMaterial(target);
62 string propValue = MaterialPropertyName(
63 (
string)prop.GetValue(target, null),
68 (
string.IsNullOrEmpty(CustomLabel) ? SplitCamelCase(prop.Name) : CustomLabel));
70 prop.SetValue(target, propValue, null);
73 private Material GetMaterial(
object target)
75 MemberInfo[] members = target.GetType().GetMember(MaterialMemberName);
76 if (members.Length == 0)
78 Debug.LogError(
"Couldn't find material member " + MaterialMemberName);
84 switch (members[0].MemberType)
86 case MemberTypes.Field:
87 FieldInfo matField = target.GetType().GetField(MaterialMemberName);
88 mat = matField.GetValue(target) as Material;
91 case MemberTypes.Property:
92 PropertyInfo matProp = target.GetType().GetProperty(MaterialMemberName);
93 mat = matProp.GetValue(target, null) as Material;
97 Debug.LogError(
"Couldn't find material member " + MaterialMemberName);
104 private static string MaterialPropertyName(
string property, Material mat,
PropertyTypeEnum type,
bool allowNone,
string defaultProperty,
string labelName)
106 Color tColor = GUI.color;
108 List<string> props =
new List<string>();
110 int selectedPropIndex = 0;
119 int propertyCount = ShaderUtil.GetPropertyCount(mat.shader);
120 string propName =
string.Empty;
121 for (
int i = 0; i < propertyCount; i++)
123 if (ShaderUtil.GetPropertyType(mat.shader, i).ToString() == type.ToString())
125 propName = ShaderUtil.GetPropertyName(mat.shader, i);
126 if (propName == property)
129 selectedPropIndex = props.Count;
135 if (
string.IsNullOrEmpty(labelName))
137 labelName = type.ToString();
139 int newPropIndex = EditorGUILayout.Popup(labelName, selectedPropIndex, props.ToArray());
142 property = (newPropIndex > 0 ? props[newPropIndex] :
string.Empty);
148 property = props[newPropIndex];
152 property = defaultProperty;
159 GUI.color = Color.Lerp(tColor, Color.gray, 0.5f);
161 EditorGUILayout.Popup(labelName, selectedPropIndex, props.ToArray());