using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using UnityEngine.Serialization; namespace UnityAtoms { public abstract class CreateEventOnAwake : MonoBehaviour where E1 : GameEvent where E2 : GameEvent where L1 : GameEventListener where L2 : GameEventListener where GA1 : GameAction where GA2 : GameAction where UER1 : UnityEvent where UER2 : UnityEvent where MH : MonoHook { [FormerlySerializedAs("CreateEvent")] [SerializeField] private bool _createEvent = true; [FormerlySerializedAs("CreateEventWithGameObject")] [SerializeField] private bool _createEventWithGameObject = true; [FormerlySerializedAs("MonoHooks")] [SerializeField] private List _monoHooks = null; [FormerlySerializedAs("Listener")] [SerializeField] private L1 _listener = null; [FormerlySerializedAs("ListenerWithGameObject")] [SerializeField] private L2 _listenerWithGameObject = null; private void Awake() { var e1 = _createEvent ? ScriptableObject.CreateInstance() : null; var e2 = _createEventWithGameObject ? ScriptableObject.CreateInstance() : null; if (e1 != null) { for (int i = 0; _monoHooks != null && i < _monoHooks.Count; ++i) { _monoHooks[i].Event = e1; } if (_listener != null) { _listener.GameEvent = e1; e1.RegisterListener(_listener); } } if (e2 != null) { for (int i = 0; _monoHooks != null && i < _monoHooks.Count; ++i) { _monoHooks[i].EventWithGameObjectReference = e2; } if (_listenerWithGameObject != null) { _listenerWithGameObject.GameEvent = e2; e2.RegisterListener(_listenerWithGameObject); } } } } }