From 397b9b9845ed761144c970ed5952145995686618 Mon Sep 17 00:00:00 2001 From: Eric Heine <erheine@nps.edu> Date: Tue, 26 Oct 2021 09:49:56 -0700 Subject: [PATCH 1/2] Added a new type of scriptable variable that handles lists of values. The list itself is not accessible externally and any function that modifies it fires an event that the value has been changed. --- .../Base Types/BooleanListVariable.cs | 21 ++ .../Base Types/BooleanListVariable.cs.meta | 13 + .../Variables/Base Types/FloatListVariable.cs | 21 ++ .../Base Types/FloatListVariable.cs.meta | 13 + .../Base Types/GameObjectListVariable.cs | 21 ++ .../Base Types/GameObjectListVariable.cs.meta | 13 + .../Variables/Base Types/IntListVariable.cs | 21 ++ .../Base Types/IntListVariable.cs.meta | 13 + .../Base Types/ListScriptableVariable.cs | 291 ++++++++++++++++++ .../Base Types/ListScriptableVariable.cs.meta | 13 + .../Base Types/StringListVariable.cs | 21 ++ .../Base Types/StringListVariable.cs.meta | 13 + .../Base Types/TransformListVariable.cs | 21 ++ .../Base Types/TransformListVariable.cs.meta | 13 + .../Base Types/Vector2ListVariable.cs | 21 ++ .../Base Types/Vector2ListVariable.cs.meta | 13 + .../Base Types/Vector3ListVariable.cs | 21 ++ .../Base Types/Vector3ListVariable.cs.meta | 13 + .../Base Types/Vector4ListVariable.cs | 21 ++ .../Base Types/Vector4ListVariable.cs.meta | 13 + 20 files changed, 610 insertions(+) create mode 100644 Scriptable Variables/Variables/Base Types/BooleanListVariable.cs create mode 100644 Scriptable Variables/Variables/Base Types/BooleanListVariable.cs.meta create mode 100644 Scriptable Variables/Variables/Base Types/FloatListVariable.cs create mode 100644 Scriptable Variables/Variables/Base Types/FloatListVariable.cs.meta create mode 100644 Scriptable Variables/Variables/Base Types/GameObjectListVariable.cs create mode 100644 Scriptable Variables/Variables/Base Types/GameObjectListVariable.cs.meta create mode 100644 Scriptable Variables/Variables/Base Types/IntListVariable.cs create mode 100644 Scriptable Variables/Variables/Base Types/IntListVariable.cs.meta create mode 100644 Scriptable Variables/Variables/Base Types/ListScriptableVariable.cs create mode 100644 Scriptable Variables/Variables/Base Types/ListScriptableVariable.cs.meta create mode 100644 Scriptable Variables/Variables/Base Types/StringListVariable.cs create mode 100644 Scriptable Variables/Variables/Base Types/StringListVariable.cs.meta create mode 100644 Scriptable Variables/Variables/Base Types/TransformListVariable.cs create mode 100644 Scriptable Variables/Variables/Base Types/TransformListVariable.cs.meta create mode 100644 Scriptable Variables/Variables/Base Types/Vector2ListVariable.cs create mode 100644 Scriptable Variables/Variables/Base Types/Vector2ListVariable.cs.meta create mode 100644 Scriptable Variables/Variables/Base Types/Vector3ListVariable.cs create mode 100644 Scriptable Variables/Variables/Base Types/Vector3ListVariable.cs.meta create mode 100644 Scriptable Variables/Variables/Base Types/Vector4ListVariable.cs create mode 100644 Scriptable Variables/Variables/Base Types/Vector4ListVariable.cs.meta diff --git a/Scriptable Variables/Variables/Base Types/BooleanListVariable.cs b/Scriptable Variables/Variables/Base Types/BooleanListVariable.cs new file mode 100644 index 0000000..3eddd56 --- /dev/null +++ b/Scriptable Variables/Variables/Base Types/BooleanListVariable.cs @@ -0,0 +1,21 @@ +using UnityEngine; + +namespace Shared.ScriptableVariables { + // A list of bool values to share across components, scenes, and prefabs + [CreateAssetMenu(menuName = "Scriptable Objects/Variables/Boolean List")] + public class BooleanListVariable : ListScriptableVariable<bool> { + //--------------------------------------------------------------------------- + [ContextMenu("Reset To Default Value")] + public void ContextMenuReset() { + Reset(); + } + } + +#if UNITY_EDITOR + //----------------------------------------------------------------------------- + [UnityEditor.CustomEditor(typeof(BooleanListVariable))] + [UnityEditor.CanEditMultipleObjects] + public class BooleanListScriptableVariableEditor : BooleanListVariable.BaseListScriptableVariableEditor { + } +#endif +} diff --git a/Scriptable Variables/Variables/Base Types/BooleanListVariable.cs.meta b/Scriptable Variables/Variables/Base Types/BooleanListVariable.cs.meta new file mode 100644 index 0000000..daea140 --- /dev/null +++ b/Scriptable Variables/Variables/Base Types/BooleanListVariable.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: 15f3837928781de49893cf8afbf3cf74 +timeCreated: 1510599427 +licenseType: Pro +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scriptable Variables/Variables/Base Types/FloatListVariable.cs b/Scriptable Variables/Variables/Base Types/FloatListVariable.cs new file mode 100644 index 0000000..a11c7b2 --- /dev/null +++ b/Scriptable Variables/Variables/Base Types/FloatListVariable.cs @@ -0,0 +1,21 @@ +using UnityEngine; + +namespace Shared.ScriptableVariables { + // A list of float values to share across components, scenes, and prefabs + [CreateAssetMenu(menuName = "Scriptable Objects/Variables/Float List")] + public class FloatListVariable : ListScriptableVariable<float> { + //--------------------------------------------------------------------------- + [ContextMenu("Reset To Default Value")] + public void ContextMenuReset() { + Reset(); + } + } + +#if UNITY_EDITOR + //----------------------------------------------------------------------------- + [UnityEditor.CustomEditor(typeof(FloatListVariable))] + [UnityEditor.CanEditMultipleObjects] + public class FloatListScriptableVariableEditor : FloatListVariable.BaseListScriptableVariableEditor { + } +#endif +} diff --git a/Scriptable Variables/Variables/Base Types/FloatListVariable.cs.meta b/Scriptable Variables/Variables/Base Types/FloatListVariable.cs.meta new file mode 100644 index 0000000..406e1ac --- /dev/null +++ b/Scriptable Variables/Variables/Base Types/FloatListVariable.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: b5347f0cb64d988408fcfe1328b71147 +timeCreated: 1510599427 +licenseType: Pro +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scriptable Variables/Variables/Base Types/GameObjectListVariable.cs b/Scriptable Variables/Variables/Base Types/GameObjectListVariable.cs new file mode 100644 index 0000000..0b06da3 --- /dev/null +++ b/Scriptable Variables/Variables/Base Types/GameObjectListVariable.cs @@ -0,0 +1,21 @@ +using UnityEngine; + +namespace Shared.ScriptableVariables { + // A list GameObject values to share across components, scenes, and prefabs + [CreateAssetMenu(menuName = "Scriptable Objects/Variables/GameObject List")] + public class GameObjectListVariable : ListScriptableVariable<GameObject> { + //--------------------------------------------------------------------------- + [ContextMenu("Reset To Default Value")] + public void ContextMenuReset() { + Reset(); + } + } + +#if UNITY_EDITOR + //----------------------------------------------------------------------------- + [UnityEditor.CustomEditor(typeof(GameObjectListVariable))] + [UnityEditor.CanEditMultipleObjects] + public class GameObjectListScriptableVariableEditor : GameObjectListVariable.BaseListScriptableVariableEditor { + } +#endif +} diff --git a/Scriptable Variables/Variables/Base Types/GameObjectListVariable.cs.meta b/Scriptable Variables/Variables/Base Types/GameObjectListVariable.cs.meta new file mode 100644 index 0000000..eb8f0de --- /dev/null +++ b/Scriptable Variables/Variables/Base Types/GameObjectListVariable.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: a5d3621783bd7b44586a6798709e9fb6 +timeCreated: 1510599427 +licenseType: Pro +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scriptable Variables/Variables/Base Types/IntListVariable.cs b/Scriptable Variables/Variables/Base Types/IntListVariable.cs new file mode 100644 index 0000000..2948656 --- /dev/null +++ b/Scriptable Variables/Variables/Base Types/IntListVariable.cs @@ -0,0 +1,21 @@ +using UnityEngine; + +namespace Shared.ScriptableVariables { + // A list of int values to share across components, scenes, and prefabs + [CreateAssetMenu(menuName = "Scriptable Objects/Variables/Int List")] + public class IntListVariable : ListScriptableVariable<int> { + //--------------------------------------------------------------------------- + [ContextMenu("Reset To Default Value")] + public void ContextMenuReset() { + Reset(); + } + } + +#if UNITY_EDITOR + //----------------------------------------------------------------------------- + [UnityEditor.CustomEditor(typeof(IntListVariable))] + [UnityEditor.CanEditMultipleObjects] + public class IntListScriptableVariableEditor : IntListVariable.BaseListScriptableVariableEditor { + } +#endif +} diff --git a/Scriptable Variables/Variables/Base Types/IntListVariable.cs.meta b/Scriptable Variables/Variables/Base Types/IntListVariable.cs.meta new file mode 100644 index 0000000..9bb9eb3 --- /dev/null +++ b/Scriptable Variables/Variables/Base Types/IntListVariable.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: 95da63b0a149f85498e00f302dced403 +timeCreated: 1510599427 +licenseType: Pro +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scriptable Variables/Variables/Base Types/ListScriptableVariable.cs b/Scriptable Variables/Variables/Base Types/ListScriptableVariable.cs new file mode 100644 index 0000000..8e2889a --- /dev/null +++ b/Scriptable Variables/Variables/Base Types/ListScriptableVariable.cs @@ -0,0 +1,291 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using UnityEngine; + +namespace Shared.ScriptableVariables { + //--------------------------------------------------------------------------- + // Base class for Scriptable Objects to share across components, scenes, and prefabs + public abstract class ListScriptableVariable<T> : ScriptableVariable, Resettable { + [Tooltip("The value of the variable")] + [SerializeField] + private List<T> list = new List<T>(); + + [Tooltip("The default value of the variable to go back to on Reset")] + public List<T> defaultList = new List<T>(); + + public delegate void ValueChangeHandler(); + public event ValueChangeHandler OnValueChanged; + + //////////////////////////////// LIST MODIFIERS ///////////////////////////// + //--------------------------------------------------------------------------- + public void Reset() { + list = new List<T>(defaultList); + ValueChanged(); + } + + //--------------------------------------------------------------------------- + public void Set(List<T> other) { + list.Clear(); + list.AddRange(other); + ValueChanged(); + } + + //--------------------------------------------------------------------------- + public void Clear() { + list.Clear(); + ValueChanged(); + } + + //--------------------------------------------------------------------------- + public void Add(T value) { + list.Add(value); + ValueChanged(); + } + + //--------------------------------------------------------------------------- + public void AddRange(List<T> otherList) { + list.AddRange(otherList); + ValueChanged(); + } + + //--------------------------------------------------------------------------- + public void Insert(int index, T item) { + list.Insert(index, item); + ValueChanged(); + } + + //--------------------------------------------------------------------------- + public void InsertRange(int index, IEnumerable<T> collection) { + list.InsertRange(index, collection); + ValueChanged(); + } + + //--------------------------------------------------------------------------- + public void Remove(T value) { + list.Remove(value); + ValueChanged(); + } + + //--------------------------------------------------------------------------- + public int RemoveAll(Predicate<T> match) { + var removedCount = list.RemoveAll(match); + ValueChanged(); + return removedCount; + } + + //--------------------------------------------------------------------------- + public void RemoveAt(int index) { + list.RemoveAt(index); + ValueChanged(); + } + + //--------------------------------------------------------------------------- + public void RemoveRange(int index, int count) { + list.RemoveRange(index, count); + ValueChanged(); + } + + //--------------------------------------------------------------------------- + public void Reverse(int index, int count) { + list.Reverse(index, count); + ValueChanged(); + } + + //--------------------------------------------------------------------------- + public void Reverse() { + list.Reverse(); + ValueChanged(); + } + + //--------------------------------------------------------------------------- + public void Sort(Comparison<T> comparison) { + list.Sort(comparison); + ValueChanged(); + } + + //--------------------------------------------------------------------------- + public void Sort(int index, int count, IComparer<T> comparer) { + list.Sort(index, count, comparer); + ValueChanged(); + } + + //--------------------------------------------------------------------------- + public void Sort() { + list.Sort(); + ValueChanged(); + } + + //--------------------------------------------------------------------------- + public void Sort(IComparer<T> comparer) { + list.Sort(comparer); + ValueChanged(); + } + + //--------------------------------------------------------------------------- + public void TrimExcess() { + list.TrimExcess(); + ValueChanged(); + } + + //////////////////////////////// LIST ACCESSORS ///////////////////////////// + //--------------------------------------------------------------------------- + public ReadOnlyCollection<T> Value { + get { return list.AsReadOnly(); } + } + + //--------------------------------------------------------------------------- + public int BinarySearch(T item) { + return list.BinarySearch(item); + } + + //--------------------------------------------------------------------------- + public int BinarySearch(T item, IComparer<T> comparer) { + return list.BinarySearch(item, comparer); + } + + //--------------------------------------------------------------------------- + public int BinarySearch(int index, int count, T item, IComparer<T> comparer) { + return list.BinarySearch(index, count, item, comparer); + } + + //--------------------------------------------------------------------------- + public bool Contains(T item) { + return list.Contains(item); + } + + //--------------------------------------------------------------------------- + public List<TOutput> ConvertAll<TOutput>(Converter<T, TOutput> converter) { + return list.ConvertAll(converter); + } + + //--------------------------------------------------------------------------- + public void CopyTo(int index, T[] array, int arrayIndex, int count) { + list.CopyTo(index, array, arrayIndex, count); + } + + //--------------------------------------------------------------------------- + public void CopyTo(T[] array, int arrayIndex) { + list.CopyTo(array, arrayIndex); + } + + //--------------------------------------------------------------------------- + public void CopyTo(T[] array) { + list.CopyTo(array); + } + + //--------------------------------------------------------------------------- + public bool Exists(Predicate<T> match) { + return list.Exists(match); + } + + //--------------------------------------------------------------------------- + public T Find(Predicate<T> match) { + return list.Find(match); + } + + //--------------------------------------------------------------------------- + public List<T> FindAll(Predicate<T> match) { + return list.FindAll(match); + } + + //--------------------------------------------------------------------------- + public int FindIndex(int startIndex, int count, Predicate<T> match) { + return list.FindIndex(startIndex, count, match); + } + + //--------------------------------------------------------------------------- + public int FindIndex(int startIndex, Predicate<T> match) { + return list.FindIndex(startIndex, match); + } + + //--------------------------------------------------------------------------- + public int FindIndex(Predicate<T> match) { + return list.FindIndex(match); + } + + //--------------------------------------------------------------------------- + public T FindLast(Predicate<T> match) { + return list.FindLast(match); + } + + //--------------------------------------------------------------------------- + public int FindLastIndex(int startIndex, int count, Predicate<T> match) { + return list.FindLastIndex(startIndex, count, match); + } + + //--------------------------------------------------------------------------- + public int FindLastIndex(int startIndex, Predicate<T> match) { + return list.FindLastIndex(startIndex, match); + } + + //--------------------------------------------------------------------------- + public int FindLastIndex(Predicate<T> match) { + return list.FindLastIndex(match); + } + + //--------------------------------------------------------------------------- + public void ForEach(Action<T> action) { + list.ForEach(action); + } + + //--------------------------------------------------------------------------- + public int IndexOf(T item, int index, int count) { + return list.IndexOf(item, index, count); + } + + //--------------------------------------------------------------------------- + public int IndexOf(T item, int index) { + return list.IndexOf(item, index); + } + + //--------------------------------------------------------------------------- + public int IndexOf(T item) { + return list.IndexOf(item); + } + + //--------------------------------------------------------------------------- + public int LastIndexOf(T item) { + return list.LastIndexOf(item); + } + + //--------------------------------------------------------------------------- + public int LastIndexOf(T item, int index) { + return list.LastIndexOf(item, index); + } + + //--------------------------------------------------------------------------- + public int LastIndexOf(T item, int index, int count) { + return list.LastIndexOf(item, index, count); + } + + //--------------------------------------------------------------------------- + public bool TrueForAll(Predicate<T> match) { + return list.TrueForAll(match); + } + + //--------------------------------------------------------------------------- + protected void ValueChanged() { + if (OnValueChanged != null) { + OnValueChanged.Invoke(); + } + } + +#if UNITY_EDITOR + //----------------------------------------------------------------------------- + //Base class for custom editors which expose the Property of ScriptableVariables + public class BaseListScriptableVariableEditor : UnityEditor.Editor { + public override void OnInspectorGUI() { + UnityEditor.EditorGUI.BeginChangeCheck(); + base.OnInspectorGUI(); + if (UnityEditor.EditorGUI.EndChangeCheck()) { + var scriptableVariable = target as ListScriptableVariable<T>; + if (scriptableVariable.OnValueChanged != null) { + scriptableVariable.OnValueChanged.Invoke(); + } + } + } + } +#endif + } +} diff --git a/Scriptable Variables/Variables/Base Types/ListScriptableVariable.cs.meta b/Scriptable Variables/Variables/Base Types/ListScriptableVariable.cs.meta new file mode 100644 index 0000000..2f5fc0c --- /dev/null +++ b/Scriptable Variables/Variables/Base Types/ListScriptableVariable.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: 2c46b23b3dbebc24b87594a6e5512b91 +timeCreated: 1510599427 +licenseType: Pro +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scriptable Variables/Variables/Base Types/StringListVariable.cs b/Scriptable Variables/Variables/Base Types/StringListVariable.cs new file mode 100644 index 0000000..c606390 --- /dev/null +++ b/Scriptable Variables/Variables/Base Types/StringListVariable.cs @@ -0,0 +1,21 @@ +using UnityEngine; + +namespace Shared.ScriptableVariables { + // A list of string values to share across components, scenes, and prefabs + [CreateAssetMenu(menuName = "Scriptable Objects/Variables/String List")] + public class StringListVariable : ListScriptableVariable<string> { + //--------------------------------------------------------------------------- + [ContextMenu("Reset To Default Value")] + public void ContextMenuReset() { + Reset(); + } + } + +#if UNITY_EDITOR + //----------------------------------------------------------------------------- + [UnityEditor.CustomEditor(typeof(StringListVariable))] + [UnityEditor.CanEditMultipleObjects] + public class StringListScriptableVariableEditor : StringListVariable.BaseListScriptableVariableEditor { + } +#endif +} diff --git a/Scriptable Variables/Variables/Base Types/StringListVariable.cs.meta b/Scriptable Variables/Variables/Base Types/StringListVariable.cs.meta new file mode 100644 index 0000000..212de3c --- /dev/null +++ b/Scriptable Variables/Variables/Base Types/StringListVariable.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: ac8c63c807dbd474ab89665284db53a2 +timeCreated: 1510599427 +licenseType: Pro +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scriptable Variables/Variables/Base Types/TransformListVariable.cs b/Scriptable Variables/Variables/Base Types/TransformListVariable.cs new file mode 100644 index 0000000..f519f10 --- /dev/null +++ b/Scriptable Variables/Variables/Base Types/TransformListVariable.cs @@ -0,0 +1,21 @@ +using UnityEngine; + +namespace Shared.ScriptableVariables { + // A list of Transform values to share across components, scenes, and prefabs + [CreateAssetMenu(menuName = "Scriptable Objects/Variables/Transform List")] + public class TransformListVariable : ListScriptableVariable<Transform> { + //--------------------------------------------------------------------------- + [ContextMenu("Reset To Default Value")] + public void ContextMenuReset() { + Reset(); + } + } + +#if UNITY_EDITOR + //----------------------------------------------------------------------------- + [UnityEditor.CustomEditor(typeof(TransformListVariable))] + [UnityEditor.CanEditMultipleObjects] + public class TransformListScriptableVariableEditor : TransformListVariable.BaseListScriptableVariableEditor { + } +#endif +} diff --git a/Scriptable Variables/Variables/Base Types/TransformListVariable.cs.meta b/Scriptable Variables/Variables/Base Types/TransformListVariable.cs.meta new file mode 100644 index 0000000..f2b613f --- /dev/null +++ b/Scriptable Variables/Variables/Base Types/TransformListVariable.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: cb949882da42d66418ff2ad4b175bf68 +timeCreated: 1510599427 +licenseType: Pro +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scriptable Variables/Variables/Base Types/Vector2ListVariable.cs b/Scriptable Variables/Variables/Base Types/Vector2ListVariable.cs new file mode 100644 index 0000000..220b5a7 --- /dev/null +++ b/Scriptable Variables/Variables/Base Types/Vector2ListVariable.cs @@ -0,0 +1,21 @@ +using UnityEngine; + +namespace Shared.ScriptableVariables { + // A list of Vector2 values to share across components, scenes, and prefabs + [CreateAssetMenu(menuName = "Scriptable Objects/Variables/Vector2 List")] + public class Vector2ListVariable : ListScriptableVariable<Vector2> { + //--------------------------------------------------------------------------- + [ContextMenu("Reset To Default Value")] + public void ContextMenuReset() { + Reset(); + } + } + +#if UNITY_EDITOR + //----------------------------------------------------------------------------- + [UnityEditor.CustomEditor(typeof(Vector2ListVariable))] + [UnityEditor.CanEditMultipleObjects] + public class Vector2ListScriptableVariableEditor : Vector2ListVariable.BaseListScriptableVariableEditor { + } +#endif +} diff --git a/Scriptable Variables/Variables/Base Types/Vector2ListVariable.cs.meta b/Scriptable Variables/Variables/Base Types/Vector2ListVariable.cs.meta new file mode 100644 index 0000000..22b859e --- /dev/null +++ b/Scriptable Variables/Variables/Base Types/Vector2ListVariable.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: 48bb1201e64b01348a9c2430cdb2e2b0 +timeCreated: 1510599427 +licenseType: Pro +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scriptable Variables/Variables/Base Types/Vector3ListVariable.cs b/Scriptable Variables/Variables/Base Types/Vector3ListVariable.cs new file mode 100644 index 0000000..541a77f --- /dev/null +++ b/Scriptable Variables/Variables/Base Types/Vector3ListVariable.cs @@ -0,0 +1,21 @@ +using UnityEngine; + +namespace Shared.ScriptableVariables { + // A list of Vector3 values to share across components, scenes, and prefabs + [CreateAssetMenu(menuName = "Scriptable Objects/Variables/Vector3 List")] + public class Vector3ListVariable : ListScriptableVariable<Vector3> { + //--------------------------------------------------------------------------- + [ContextMenu("Reset To Default Value")] + public void ContextMenuReset() { + Reset(); + } + } + +#if UNITY_EDITOR + //----------------------------------------------------------------------------- + [UnityEditor.CustomEditor(typeof(Vector3ListVariable))] + [UnityEditor.CanEditMultipleObjects] + public class Vector3ListScriptableVariableEditor : Vector3ListVariable.BaseListScriptableVariableEditor { + } +#endif +} diff --git a/Scriptable Variables/Variables/Base Types/Vector3ListVariable.cs.meta b/Scriptable Variables/Variables/Base Types/Vector3ListVariable.cs.meta new file mode 100644 index 0000000..c4c5905 --- /dev/null +++ b/Scriptable Variables/Variables/Base Types/Vector3ListVariable.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: c1dc4fd277987534faa8414e8bc6637f +timeCreated: 1510599427 +licenseType: Pro +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scriptable Variables/Variables/Base Types/Vector4ListVariable.cs b/Scriptable Variables/Variables/Base Types/Vector4ListVariable.cs new file mode 100644 index 0000000..d7fd61f --- /dev/null +++ b/Scriptable Variables/Variables/Base Types/Vector4ListVariable.cs @@ -0,0 +1,21 @@ +using UnityEngine; + +namespace Shared.ScriptableVariables { + // A list of Vector4 values to share across components, scenes, and prefabs + [CreateAssetMenu(menuName = "Scriptable Objects/Variables/Vector4 List")] + public class Vector4ListVariable : ListScriptableVariable<Vector4> { + //--------------------------------------------------------------------------- + [ContextMenu("Reset To Default Value")] + public void ContextMenuReset() { + Reset(); + } + } + +#if UNITY_EDITOR + //----------------------------------------------------------------------------- + [UnityEditor.CustomEditor(typeof(Vector4ListVariable))] + [UnityEditor.CanEditMultipleObjects] + public class Vector4ListScriptableVariableEditor : Vector4ListVariable.BaseListScriptableVariableEditor { + } +#endif +} diff --git a/Scriptable Variables/Variables/Base Types/Vector4ListVariable.cs.meta b/Scriptable Variables/Variables/Base Types/Vector4ListVariable.cs.meta new file mode 100644 index 0000000..496434d --- /dev/null +++ b/Scriptable Variables/Variables/Base Types/Vector4ListVariable.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: c163b272645f15349ab7c12571922835 +timeCreated: 1510599427 +licenseType: Pro +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: -- GitLab From 23e1d193e2ef04159b71a2132001ca44b74238b2 Mon Sep 17 00:00:00 2001 From: Eric Heine <erheine@nps.edu> Date: Tue, 26 Oct 2021 10:43:13 -0700 Subject: [PATCH 2/2] Added an indexer override and Count function. Renamed the list changed event and added another one for when a list item changes. --- .../Base Types/ListScriptableVariable.cs | 88 +++++++++++++------ 1 file changed, 61 insertions(+), 27 deletions(-) diff --git a/Scriptable Variables/Variables/Base Types/ListScriptableVariable.cs b/Scriptable Variables/Variables/Base Types/ListScriptableVariable.cs index 8e2889a..cacbd07 100644 --- a/Scriptable Variables/Variables/Base Types/ListScriptableVariable.cs +++ b/Scriptable Variables/Variables/Base Types/ListScriptableVariable.cs @@ -14,118 +14,129 @@ namespace Shared.ScriptableVariables { [Tooltip("The default value of the variable to go back to on Reset")] public List<T> defaultList = new List<T>(); - public delegate void ValueChangeHandler(); - public event ValueChangeHandler OnValueChanged; + public delegate void ListChangeHandler(); + public event ListChangeHandler OnListChanged; + + public delegate void ListItemChangeHandler(int index, T item); + public event ListItemChangeHandler OnListItemChanged; //////////////////////////////// LIST MODIFIERS ///////////////////////////// + public T this[int index] { + get { return list[index]; } + set { + list[index] = value; + ListItemChanged(index, value); + } + } + //--------------------------------------------------------------------------- public void Reset() { list = new List<T>(defaultList); - ValueChanged(); + ListChanged(); } //--------------------------------------------------------------------------- public void Set(List<T> other) { list.Clear(); list.AddRange(other); - ValueChanged(); + ListChanged(); } //--------------------------------------------------------------------------- public void Clear() { list.Clear(); - ValueChanged(); + ListChanged(); } //--------------------------------------------------------------------------- public void Add(T value) { list.Add(value); - ValueChanged(); + ListChanged(); } //--------------------------------------------------------------------------- public void AddRange(List<T> otherList) { list.AddRange(otherList); - ValueChanged(); + ListChanged(); } //--------------------------------------------------------------------------- public void Insert(int index, T item) { list.Insert(index, item); - ValueChanged(); + ListChanged(); } //--------------------------------------------------------------------------- public void InsertRange(int index, IEnumerable<T> collection) { list.InsertRange(index, collection); - ValueChanged(); + ListChanged(); } //--------------------------------------------------------------------------- public void Remove(T value) { list.Remove(value); - ValueChanged(); + ListChanged(); } //--------------------------------------------------------------------------- public int RemoveAll(Predicate<T> match) { var removedCount = list.RemoveAll(match); - ValueChanged(); + ListChanged(); return removedCount; } //--------------------------------------------------------------------------- public void RemoveAt(int index) { list.RemoveAt(index); - ValueChanged(); + ListChanged(); } //--------------------------------------------------------------------------- public void RemoveRange(int index, int count) { list.RemoveRange(index, count); - ValueChanged(); + ListChanged(); } //--------------------------------------------------------------------------- public void Reverse(int index, int count) { list.Reverse(index, count); - ValueChanged(); + ListChanged(); } //--------------------------------------------------------------------------- public void Reverse() { list.Reverse(); - ValueChanged(); + ListChanged(); } //--------------------------------------------------------------------------- public void Sort(Comparison<T> comparison) { list.Sort(comparison); - ValueChanged(); + ListChanged(); } //--------------------------------------------------------------------------- public void Sort(int index, int count, IComparer<T> comparer) { list.Sort(index, count, comparer); - ValueChanged(); + ListChanged(); } //--------------------------------------------------------------------------- public void Sort() { list.Sort(); - ValueChanged(); + ListChanged(); } //--------------------------------------------------------------------------- public void Sort(IComparer<T> comparer) { list.Sort(comparer); - ValueChanged(); + ListChanged(); } //--------------------------------------------------------------------------- public void TrimExcess() { list.TrimExcess(); - ValueChanged(); + ListChanged(); } //////////////////////////////// LIST ACCESSORS ///////////////////////////// @@ -134,6 +145,9 @@ namespace Shared.ScriptableVariables { get { return list.AsReadOnly(); } } + //--------------------------------------------------------------------------- + public int Count { get { return list.Count; } } + //--------------------------------------------------------------------------- public int BinarySearch(T item) { return list.BinarySearch(item); @@ -265,10 +279,32 @@ namespace Shared.ScriptableVariables { } //--------------------------------------------------------------------------- - protected void ValueChanged() { - if (OnValueChanged != null) { - OnValueChanged.Invoke(); - } + // This function is public so that users can fire it in the specific cases + // where the ListScriptableVariable has no idea that aspects of an item in the + // list has changed + // + // For example, if the item type is a reference type, then when a field/property + // of an item changes, the list can't know. + // + // list[index].field = value; + // + // So the user of the ListScriptableVariable needs to call this function so + // the anyone who cares can be notified: + // + // list[index].field = value; + // list.ListItemChanged(index, list[index]); + // + // If a user sets the item directly with the index, this function automatically + // gets called + // + // list[index] = value; + public void ListItemChanged(int index, T item) { + OnListItemChanged?.Invoke(index, item); + } + + //--------------------------------------------------------------------------- + protected void ListChanged() { + OnListChanged?.Invoke(); } #if UNITY_EDITOR @@ -280,9 +316,7 @@ namespace Shared.ScriptableVariables { base.OnInspectorGUI(); if (UnityEditor.EditorGUI.EndChangeCheck()) { var scriptableVariable = target as ListScriptableVariable<T>; - if (scriptableVariable.OnValueChanged != null) { - scriptableVariable.OnValueChanged.Invoke(); - } + scriptableVariable.ListChanged(); } } } -- GitLab