8 using NaughtyAttributes.Editor;
11 using System.Reflection;
12 using System.Collections;
34 1, 2, 3, 4, 5, 6, 7, 8,
35 9, 10, 11, 12, 13, 14, 15, 16,
36 17, 18, 19, 20, 21, 22, 23, 24,
37 25, 26, 27, 28, 29, 30, 31
43 public static int[]
OneTo32 =>
new int[32]
45 1, 2, 3, 4, 5, 6, 7, 8,
46 9, 10, 11, 12, 13, 14, 15, 16,
47 17, 18, 19, 20, 21, 22, 23, 24,
48 25, 26, 27, 28, 29, 30, 31,
82 public static T[]
To<T>(
int _flags, T[] _options)
87 Debug.LogWarning(
"A value less than 0 is being used as the flag variable in a MultiselectAttribute.To<T>() call! The value is " + _flags);
91 int[] outIndexes =
new int[32];
94 for (
int i = 0; i < 32; i++)
97 if ((_flags & (1 << i)) != 0)
106 .Select(i => _options[i])
118 public static int[]
ToInt(
int _flags)
129 [CustomPropertyDrawer(typeof(MultiselectAttribute))]
130 public class MultiselectDrawer : PropertyDrawer
134 public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
143 public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
146 int mask =
property.intValue;
147 string dataName = ((MultiselectAttribute)attribute).OptionsName;
149 object options = GetValues(property, dataName);
150 string[] optionNames;
152 EditorGUILayout.BeginHorizontal();
153 EditorGUILayout.PrefixLabel(label);
162 if (options is IList list)
166 optionNames =
new string[list.Count];
169 for (
int i = 0; i < list.Count; i++)
170 optionNames[i] = list[i].ToString();
173 mask = EditorGUILayout.MaskField(mask, optionNames);
178 EditorGUILayout.LabelField($
"{dataName} has size 0!");
181 else if (options is Array array)
183 if (array.Length > 0)
185 optionNames =
new string[array.Length];
188 for (
int i = 0; i < array.Length; i++)
189 optionNames[i] = array.GetValue(i).ToString();
192 mask = EditorGUILayout.MaskField(mask, optionNames);
197 EditorGUILayout.LabelField($
"{dataName} has size 0!");
203 EditorGUILayout.LabelField($
"{dataName} is not a List or an Array!");
207 EditorGUILayout.LabelField($
"Invalid collection at {dataName}! Cannot display multiselect!");
209 EditorGUILayout.EndHorizontal();
212 property.intValue = mask;
218 private object GetValues(SerializedProperty property,
string valuesName)
220 object target = PropertyUtility.GetTargetObjectWithProperty(property);
222 FieldInfo valuesFieldInfo = ReflectionUtility.GetField(target, valuesName);
223 if (valuesFieldInfo !=
null)
225 return valuesFieldInfo.GetValue(target);
228 PropertyInfo valuesPropertyInfo = ReflectionUtility.GetProperty(target, valuesName);
229 if (valuesPropertyInfo !=
null)
231 return valuesPropertyInfo.GetValue(target);
234 MethodInfo methodValuesInfo = ReflectionUtility.GetMethod(target, valuesName);
235 if (methodValuesInfo !=
null &&
236 methodValuesInfo.ReturnType != typeof(
void) &&
237 methodValuesInfo.GetParameters().Length == 0)
239 return methodValuesInfo.Invoke(target,
null);
static int[] ZeroTo31
A filler array with numbers 0 - 31, used when converting from a flag int to a list subset.
static int[] ToInt(int _flags)
Shorthand for MultiselectAttribute.To<int>(_flags, _options). Useful for converting a multiselect to ...
string OptionsName
Name of the variable this attribute uses for the options list.
MultiselectAttribute(string _optionsName)
Initialises the attribute.
static T[] To< T >(int _flags, T[] _options)
Converts an int with the Multiselect attribute to a subset of a given list according to the int flags...
static int[] OneTo32
A filler array with numbers 1 - 32. Not yet used, but theoretically helpful for indexing from the end...