HKI Core
GenericContainer.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using System.Collections.Generic;
3 
4 namespace HKI.Core.Containers
5 {
10  public abstract class GenericContainer<T> : ScriptableObject
11  {
12  // Public variables
13  public event System.Action OnValueChanged;
14 
15  // Private variables
16  List<T> items = new List<T>();
17 
18  // Getter
19  public List<T> Items { get { return items; } }
20  public int Count { get { return items.Count; } }
21 
22  // Interface function
23  public void Add(T item)
24  {
25  if(item != null && !items.Contains(item))
26  {
27  items.Add(item);
28  if(OnValueChanged != null)
29  OnValueChanged();
30  }
31  }
32 
33  public void Remove(T item)
34  {
35  if(item != null)
36  {
37  items.Remove(item);
38  if(OnValueChanged != null)
39  OnValueChanged();
40  }
41  }
42 
43  public void RemoveByIndex(int index)
44  {
45  if(index >= 0 && index < items.Count)
46  {
47  items.RemoveAt(index);
48  if (OnValueChanged != null)
49  OnValueChanged();
50  }
51  }
52  }
53 }
Generic container to store data of a type T in a ScriptableObject