| | 1 | | using System.Collections.Generic; |
| | 2 | | using UnityEngine; |
| | 3 | |
|
| | 4 | | public class ListVariable_Legacy<T> : ScriptableObject |
| | 5 | | { |
| | 6 | | public delegate void Added(T addedValue); |
| | 7 | |
|
| | 8 | | public delegate void Removed(T removedValue); |
| | 9 | |
|
| | 10 | | private event Added OnAddedElementValue; |
| | 11 | | private event Removed OnRemovedElementValue; |
| | 12 | |
|
| 0 | 13 | | public virtual event Added OnAdded { add => OnAddedElementValue += value; remove => OnAddedElementValue -= value; } |
| | 14 | |
|
| 0 | 15 | | public virtual event Removed OnRemoved { add => OnRemovedElementValue += value; remove => OnRemovedElementValue -= v |
| | 16 | |
|
| 0 | 17 | | [SerializeField] protected List<T> list = new List<T>(); |
| | 18 | |
|
| | 19 | | public void Add(T newValue) |
| | 20 | | { |
| 1 | 21 | | list.Add(newValue); |
| 1 | 22 | | OnAddedElementValue?.Invoke(newValue); |
| 0 | 23 | | } |
| | 24 | |
|
| | 25 | | public void Add(T[] newValues) |
| | 26 | | { |
| 0 | 27 | | int count = newValues.Length; |
| 0 | 28 | | for (int i = 0; i < count; ++i) |
| | 29 | | { |
| 0 | 30 | | Add(newValues[i]); |
| | 31 | | } |
| 0 | 32 | | } |
| | 33 | |
|
| | 34 | | public void Remove(T value) |
| | 35 | | { |
| 0 | 36 | | if (!list.Contains(value)) |
| 0 | 37 | | return; |
| | 38 | |
|
| 0 | 39 | | list.Remove(value); |
| 0 | 40 | | OnRemovedElementValue?.Invoke(value); |
| 0 | 41 | | } |
| | 42 | |
|
| | 43 | | public void Remove(T[] values) |
| | 44 | | { |
| 0 | 45 | | int count = values.Length; |
| 0 | 46 | | for (int i = 0; i < count; ++i) |
| | 47 | | { |
| 0 | 48 | | Remove(values[i]); |
| | 49 | | } |
| 0 | 50 | | } |
| | 51 | |
|
| 0 | 52 | | public T Get(int index) { return list.Count >= index + 1 ? list[index] : default(T); } |
| | 53 | |
|
| 0 | 54 | | public List<T> GetList() { return list; } |
| | 55 | |
|
| | 56 | | public void Clear() |
| | 57 | | { |
| 0 | 58 | | for (int i = 0; i < list.Count; i++) |
| | 59 | | { |
| 0 | 60 | | OnRemovedElementValue?.Invoke(list[i]); |
| | 61 | | } |
| | 62 | |
|
| 0 | 63 | | list.Clear(); |
| 0 | 64 | | } |
| | 65 | |
|
| | 66 | | #if UNITY_EDITOR |
| | 67 | | private void OnEnable() |
| | 68 | | { |
| 0 | 69 | | Application.quitting -= CleanUp; |
| 0 | 70 | | Application.quitting += CleanUp; |
| 0 | 71 | | } |
| | 72 | |
|
| | 73 | | private void CleanUp() |
| | 74 | | { |
| 0 | 75 | | Application.quitting -= CleanUp; |
| 0 | 76 | | if (UnityEditor.AssetDatabase.Contains(this)) //It could happen that the base variable has been created in runti |
| 0 | 77 | | Resources.UnloadAsset(this); |
| 0 | 78 | | } |
| | 79 | | #endif |
| | 80 | | } |