HKI Core
HKIVarGeneric.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using System.Collections.Generic;
3 
4 namespace HKI.Core.Variables
5 {
10  [System.Serializable]
11  public abstract class HKIVarGeneric<T> : HKIVar
12  {
13  // Serialized variables
14  [SerializeField] T defaultValue = default(T);
15  [HideInInspector][SerializeField] T previousValue = default(T);
16  [HideInInspector][SerializeField] T value = default(T);
17 
18  // Getter
19  public T GetDefaultValue { get { return defaultValue; } }
20  public T Value { get { return value; } }
21 
22  // Setter
23  public T SetValue
24  { set
25  {
26  if(!EqualityComparer<T>.Default.Equals(this.value, value))
27  {
28  previousValue = this.value;
29  this.value = value;
30  RaiseOnValueChangedCallback();
31  }
32  }
33  }
34 
35  // OnEnable function
36  void OnEnable()
37  {
38  if (!EqualityComparer<T>.Default.Equals(defaultValue, value))
39  SetValue = defaultValue;
40  else
41  RaiseOnValueChangedCallback();
42  }
43 
44  // Interface functions
45  public void SetValueToDefaultValue()
46  {
47  SetValue = defaultValue;
48  }
49 
51  {
52  SetValue = previousValue;
53  }
54 
56  {
57  defaultValue = previousValue;
58  }
59 
60  public void SetDefaultValueToValue()
61  {
62  defaultValue = value;
63  }
64 
65  public override string ToString()
66  {
67  return value.ToString();
68  }
69 
70  // Helper functions
71  protected virtual void SetWithParameters(object[] parameters)
72  {
73  if(parameters == null || parameters.Length != 1)
74  return;
75 
76  defaultValue = (T)parameters[0];
77  value = defaultValue;
78  }
79  }
80 }
virtual void SetWithParameters(object[] parameters)
This abstract class of a ScriptableObject is provides child classes with on value changed callback ca...
Definition: HKIVar.cs:12
This class adds a generic implementation to HKIVar. This reduces the repetitive implementation of thi...