using System; using System.Collections.Generic; using UnityEngine; namespace UnityAtoms { public abstract class AtomEvent : ScriptableObject, ISerializationCallbackReceiver, IAtomEventIcon { public event Action OnEvent; public void Raise(T item) { OnEvent?.Invoke(item); } public void Register(Action del) { OnEvent += del; } public void Unregister(Action del) { OnEvent -= del; } public void RegisterListener(IAtomListener listener) { OnEvent += listener.OnEventRaised; } public void UnregisterListener(IAtomListener listener) { OnEvent -= listener.OnEventRaised; } #region Observable public IObservable Observe() { return new ObservableEvent(Register, Unregister); } #endregion // Observable public void OnBeforeSerialize() { } public void OnAfterDeserialize() { // Clear all delegates when exiting play mode if (OnEvent != null) { foreach (var d in OnEvent.GetInvocationList()) { OnEvent -= (Action)d; } } } } public abstract class AtomEvent : ScriptableObject, ISerializationCallbackReceiver, IAtomEventIcon { public event Action OnEvent; public void Raise(T1 item1, T2 item2) { OnEvent?.Invoke(item1, item2); } public void Register(Action del) { OnEvent += del; } public void Unregister(Action del) { OnEvent -= del; } public void RegisterListener(IAtomListener listener) { OnEvent += listener.OnEventRaised; } public void UnregisterListener(IAtomListener listener) { OnEvent -= listener.OnEventRaised; } #region Observable public IObservable Observe(Func createCombinedModel) { return new ObservableEvent(Register, Unregister, createCombinedModel); } #endregion // Observable public void OnBeforeSerialize() { } public void OnAfterDeserialize() { // Clear all delegates when exiting play mode if (OnEvent != null) foreach (var d in OnEvent.GetInvocationList()) { OnEvent -= (Action)d; } } } }