using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using UnityEngine.Serialization; namespace UnityAtoms { public abstract class AtomListener : MonoBehaviour, IAtomListener, IAtomListenerIcon where GA : AtomAction where E : AtomEvent where UER : UnityEvent { [FormerlySerializedAs("Event")] [SerializeField] private E _event = null; public E GameEvent { get { return _event; } set { _event = value; } } [FormerlySerializedAs("UnityEventResponse")] [SerializeField] private UER _unityEventResponse = null; [FormerlySerializedAs("GameActionResponses")] [SerializeField] private List _actionResponses = new List(); private void OnEnable() { if (GameEvent == null) return; GameEvent.RegisterListener(this); } private void OnDisable() { if (GameEvent == null) return; GameEvent.UnregisterListener(this); } public void OnEventRaised(T item) { if (_unityEventResponse != null) { _unityEventResponse.Invoke(item); } for (int i = 0; _actionResponses != null && i < _actionResponses.Count; ++i) { _actionResponses[i].Do(item); } } } public abstract class AtomListener : MonoBehaviour, IAtomListener, IAtomListenerIcon where GA : AtomAction where E : AtomEvent where UER : UnityEvent { [FormerlySerializedAs("Event")] [SerializeField] private E _event; public E GameEvent { get { return _event; } set { _event = value; } } [FormerlySerializedAs("UnityEventResponse")] [SerializeField] private UER _unityEventResponse; [FormerlySerializedAs("GameActionResponses")] [SerializeField] private List _actionResponses = new List(); private void OnEnable() { if (_event == null) return; GameEvent.RegisterListener(this); } private void OnDisable() { if (_event == null) return; GameEvent.UnregisterListener(this); } public void OnEventRaised(T1 first, T2 second) { if (_unityEventResponse != null) { _unityEventResponse.Invoke(first, second); } for (int i = 0; _actionResponses != null && i < _actionResponses.Count; ++i) { _actionResponses[i].Do(first, second); } } } }