14 [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
17 public string DefaultName {
get;
private set; }
18 public int DefaultValue {
get;
private set; }
19 public int ValueOnZero {
get;
private set; }
20 public bool IgnoreNone {
get;
private set; }
21 public bool IgnoreAll {
get;
private set; }
22 public string CustomLabel {
get;
private set; }
31 CustomLabel = customLabel;
39 IgnoreNone = ignoreNone;
40 IgnoreAll = ignoreAll;
41 CustomLabel = customLabel;
44 public EnumCheckboxAttribute(
string defaultName,
object defaultValue,
object valueOnZero = null,
bool ignoreNone =
true,
bool ignoreAll =
true,
string customLabel = null)
46 DefaultName = defaultName;
47 DefaultValue = Convert.ToInt32(defaultValue);
48 ValueOnZero = Convert.ToInt32(valueOnZero);
49 IgnoreNone = ignoreNone;
50 IgnoreAll = ignoreAll;
51 CustomLabel = customLabel;
55 public override void DrawEditor(
UnityEngine.Object target, FieldInfo field, SerializedProperty property)
57 int value = EnumCheckbox(
58 (
string.IsNullOrEmpty(CustomLabel) ? SplitCamelCase(field.Name) : CustomLabel),
59 field.GetValue(target),
66 field.SetValue(target, value);
69 public override void DrawEditor(
UnityEngine.Object target, PropertyInfo prop)
71 int value = EnumCheckbox(
72 (
string.IsNullOrEmpty(CustomLabel) ? SplitCamelCase(prop.Name) : CustomLabel),
73 prop.GetValue(target, null),
80 prop.SetValue(target, value, null);
83 private static int EnumCheckbox(
string label,
object enumObj,
string defaultName,
object defaultVal,
object valOnZero,
bool ignoreNone =
true,
bool ignoreAll =
true)
85 if (!enumObj.GetType().IsEnum)
87 throw new ArgumentException(
"enumObj must be an enum.");
91 int enumFlags = Convert.ToInt32(enumObj);
93 EditorGUILayout.BeginVertical(EditorStyles.helpBox);
94 EditorGUILayout.LabelField(label, EditorStyles.miniLabel);
96 GUIStyle styleHR =
new GUIStyle(GUI.skin.box);
97 styleHR.stretchWidth =
true;
98 styleHR.fixedHeight = 2;
99 GUILayout.Box(
"", styleHR);
101 System.Array enumVals = Enum.GetValues(enumObj.GetType());
102 int lastvalue = Convert.ToInt32(enumVals.GetValue(enumVals.GetLength(0) - 1));
104 foreach (
object enumVal
in enumVals)
106 int flagVal = Convert.ToInt32(enumVal);
107 if (ignoreNone && flagVal == 0 && enumVal.ToString().ToLower() ==
"none")
111 if (ignoreAll && flagVal == lastvalue && enumVal.ToString().ToLower() ==
"all")
115 bool selected = (flagVal & enumFlags) != 0;
116 selected = EditorGUILayout.Toggle(enumVal.ToString(), selected);
120 enumFlags |= flagVal;
124 enumFlags &= ~flagVal;
127 if (!
string.IsNullOrEmpty(defaultName))
129 if (GUILayout.Button(defaultName, EditorStyles.miniButton))
131 enumFlags = Convert.ToInt32(defaultVal);
134 EditorGUILayout.EndVertical();
138 enumFlags = Convert.ToInt32(valOnZero);