using UnityEngine; using UnityEngine.Events; using UnityAtoms.Utils; using UnityEngine.Serialization; namespace UnityAtoms { public abstract class CreateVariableOnAwake : MonoBehaviour where V : ScriptableObjectVariable where E1 : GameEvent where E2 : GameEvent where L1 : GameEventListener where L2 : GameEventListener where GA1 : GameAction where GA2 : GameAction where GA3 : GameAction where GA4 : GameAction where UER1 : UnityEvent where UER2 : UnityEvent { [FormerlySerializedAs("CreateChangedEvent")] [SerializeField] private bool _createChangedEvent = true; [FormerlySerializedAs("CreateChangedWithHistoryEvent")] [SerializeField] private bool _createChangedWithHistoryEvent = false; [FormerlySerializedAs("Listener")] [SerializeField] private L1 _listener = null; [FormerlySerializedAs("ListenerWithHistory")] [SerializeField] private L2 _listenerWithHistory = null; [FormerlySerializedAs("OnVariableCreate")] [SerializeField] private GA3 _onVariableCreate = null; [FormerlySerializedAs("OnVariableCreateWithGO")] [SerializeField] private GA4 _onVariableCreateWithGO = null; private void Awake() { var variable = DynamicAtoms.CreateVariable( initialValue: default(T), changed: _createChangedEvent ? ScriptableObject.CreateInstance() : null, changedWithHistory: _createChangedWithHistoryEvent ? ScriptableObject.CreateInstance() : null ); if (variable.Changed != null) { if (_listener != null) { _listener.GameEvent = variable.Changed; _listener.GameEvent.RegisterListener(_listener); } } if (variable.ChangedWithHistory != null) { if (_listenerWithHistory != null) { _listenerWithHistory.GameEvent = variable.ChangedWithHistory; _listenerWithHistory.GameEvent.RegisterListener(_listenerWithHistory); } } if (_onVariableCreate != null) { _onVariableCreate.Do(variable); } if (_onVariableCreateWithGO != null) { _onVariableCreateWithGO.Do(variable, gameObject); } } } }