static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) { if (deletedAssets.Length > 0) { ScriptableEnum.MarkAssetsForReload(); } }
public override int GetBitMask() { int mask = 0; T[] allValues = ScriptableEnum.GetValues <T>(); for (int i = 0; i < allValues.Length; i++) { if (_isEverything || _values.Contains(allValues[i])) { mask |= (1 << i); } } return(mask); }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { SerializedProperty isEverythingProperty = property.FindPropertyRelative("_isEverything"); SerializedProperty listProperty = property.FindPropertyRelative("_values"); ScriptableEnumMask mask = property.GetValue <ScriptableEnumMask>(); Type valueType = mask.GetType().BaseType.GetGenericArguments()[0]; ScriptableEnum[] values = ScriptableEnum.GetValues(valueType); // values.Sort((x, y) => x.name.CompareTo(y.name)); string[] labels = new string[values.Length]; for (int i = 0; i < values.Length; i++) { labels[i] = values[i].name; } EditorGUI.BeginProperty(position, label, property); EditorGUI.BeginChangeCheck(); int newMaskValue = EditorGUI.MaskField(position, label, isEverythingProperty.boolValue ? ~0 : mask.GetBitMask(), labels); if (EditorGUI.EndChangeCheck()) { mask.SetFromBitMask(newMaskValue); ScriptableEnum[] maskValues = mask.GetRawValues(); listProperty.arraySize = maskValues.Length; for (int i = 0; i < maskValues.Length; i++) { listProperty.GetArrayElementAtIndex(i).objectReferenceValue = maskValues[i]; } isEverythingProperty.boolValue = mask.IsEverything; // EditorUtility.SetDirty(property.serializedObject.targetObject); property.serializedObject.ApplyModifiedPropertiesWithoutUndo(); } EditorGUI.EndProperty(); }
static int GetIndex(ScriptableEnum value) { Assert.IsNotNull(value, "ScriptableEnum value is null"); EnsureInstancesAreLoaded(); List <ScriptableEnum> list; if (_assetLists.TryGetValue(value.GetType(), out list)) { for (int i = 0; i < list.Count; i++) { if (list[i] == value) { return(i); } } } throw new KeyNotFoundException("No ScriptableEnum value in asset list for type: " + value.GetType() + ", with value: " + value.name); }
static int SetupValuesAndLabels(Type type, Object currentValue, out List <ScriptableEnum> values, out GUIContent[] labels, bool allowNull) { values = new List <ScriptableEnum>(ScriptableEnum.GetValues(type)); values.Sort((x, y) => x.name.CompareTo(y.name)); if (allowNull) { values.Insert(0, null); } int selectedIndex = 0; if (currentValue != null) { for (int i = 0; i < values.Count; i++) { if (values[i] == currentValue) { selectedIndex = i; break; } } } labels = new GUIContent[values.Count]; for (int i = 0; i < values.Count; i++) { if (values[i] == null) { labels[i] = new GUIContent("NONE"); } else { labels[i] = new GUIContent(values[i].name); } } return(selectedIndex); }
public override void SetFromBitMask(int mask) { _values = new List <T>(); T[] allValues = ScriptableEnum.GetValues <T>(); for (int i = 0; i < allValues.Length; i++) { if (((1 << i) & mask) != 0) { _values.Add(allValues[i]); } } if (_values.Count == allValues.Length) { _isEverything = true; _values.Clear(); } else { _isEverything = false; } }
static void EnsureInstancesAreLoaded() { if (!_assetsAreLoaded) { ScriptableEnum[] assets = Resources.LoadAll <ScriptableEnum>(""); _assetTypes = new List <Type>(); _assetLists = new Dictionary <Type, List <ScriptableEnum> >(); _assetsByName = new Dictionary <Type, Dictionary <string, ScriptableEnum> >(); _assetsByGUID = new Dictionary <string, ScriptableEnum>(); // Add all ScriptableEnum assets in the resources folder to asset lists for (int i = 0; i < assets.Length; i++) { #if UNITY_EDITOR if (string.IsNullOrEmpty(assets[i]._guid)) { assets[i].OnValidate(); } #endif assets[i]._assetIndex = -1; Type type = assets[i].GetType(); List <ScriptableEnum> list; if (_assetLists.TryGetValue(type, out list)) { Assert.IsFalse(list.Contains(assets[i])); list.Add(assets[i]); } else { _assetTypes.Add(type); _assetLists.Add(type, new List <ScriptableEnum> { assets[i] }); } _assetsByGUID.Add(assets[i].GUID, assets[i]); } // Make sure when register any types that have no assets, but do have subtypes with assets Type scriptableEnumType = typeof(ScriptableEnum); List <Type> newAssetTypes = new List <Type>(); for (int i = 0; i < _assetTypes.Count; i++) { Type baseType = _assetTypes[i].BaseType; while (baseType != scriptableEnumType && scriptableEnumType.IsAssignableFrom(baseType)) { if (!newAssetTypes.Contains(baseType) && !_assetTypes.Contains(baseType)) { newAssetTypes.Add(baseType); } baseType = baseType.BaseType; } } for (int i = 0; i < newAssetTypes.Count; i++) { _assetTypes.Add(newAssetTypes[i]); _assetLists.Add(newAssetTypes[i], new List <ScriptableEnum>()); } // Make sure we add all assets to lists of parent types for (int i = 0; i < assets.Length; i++) { Type type = assets[i].GetType(); foreach (KeyValuePair <Type, List <ScriptableEnum> > assetList in _assetLists) { if (type != assetList.Key && assetList.Key.IsAssignableFrom(type)) { assetList.Value.Add(assets[i]); } } } // Populate the name lookup dictionaries foreach (KeyValuePair <Type, List <ScriptableEnum> > assetList in _assetLists) { Dictionary <string, ScriptableEnum> nameDictionary = new Dictionary <string, ScriptableEnum>(); for (int i = 0; i < assetList.Value.Count; i++) { ScriptableEnum asset = assetList.Value[i]; if (!nameDictionary.ContainsKey(asset.name)) { nameDictionary.Add(asset.name, asset); } else { UnityEngine.Debug.LogError("Duplicate ScriptableEnum name: " + asset, asset); } } _assetsByName.Add(assetList.Key, nameDictionary); } _assetsAreLoaded = true; } }
public static CodeDefintion CreateScriptableEnumConstants(Type type) { Assert.IsTrue(typeof(ScriptableEnum).IsAssignableFrom(type)); StringBuilder code = new StringBuilder(); ScriptableEnum[] values = ScriptableEnum.GetValues(type); string typeName = type.Name; bool childType = type.BaseType != typeof(ScriptableEnum); string allFieldName = "__all" + StringUtils.Pluralise(typeName); code.Append("public static "); code.Append(typeName); code.Append("[] "); code.Append("All" + StringUtils.Pluralise(typeName)); code.Append(" { get { if ("); code.Append(allFieldName); code.Append(" == null) "); code.Append(allFieldName); code.Append(" = GetValues<"); code.Append(typeName); code.Append(">(); return "); code.Append(allFieldName); code.Append("; } }"); code.Append(NEW_LINE); for (int i = 0; i < values.Length; i++) { string fieldName = StringUtils.Variableize(values[i].name, true, true); code.Append("public static "); if (childType) { code.Append("new "); } code.Append(typeName); code.Append(" "); code.Append(StringUtils.Santise(values[i].name, false, false)); code.Append(" { get { if (_"); code.Append(fieldName); code.Append(" == null) _"); code.Append(fieldName); code.Append(" = GetValue<"); code.Append(typeName); code.Append(">(\""); code.Append(values[i].name); code.Append("\"); return _"); code.Append(fieldName); code.Append("; } }"); code.Append(NEW_LINE); } code.Append(NEW_LINE); code.Append("protected static "); code.Append(typeName); code.Append("[] "); code.Append(allFieldName); code.Append(";"); code.Append(NEW_LINE); for (int i = 0; i < values.Length; i++) { code.Append("protected static "); if (childType) { code.Append("new "); } code.Append(typeName); code.Append(" _"); code.Append(StringUtils.Variableize(values[i].name, true, true)); code.Append(";"); if (i < values.Length - 1) { code.Append(NEW_LINE); } } return(new CodeDefintion(code.ToString(), Scope.Class)); }
public override ScriptableEnum[] GetRawValues() { return(_isEverything ? ScriptableEnum.GetValues <T>() : _values.ToArray()); }
public T this[int index] => _isEverything?ScriptableEnum.GetValue <T>(index) : _values[index];