using System; using UnityEngine; using UnityEngine.Assertions; namespace UnityAtoms { /// /// A Variable Instancer is a MonoBehaviour that takes a variable as a base and creates an in memory copy of it OnEnable. /// This is handy when you want to use atoms for prefabs that are instantiated at runtime. Use together with AtomCollection to /// react accordingly when a prefab with an assoicated atom is added or deleted to the scene. /// /// Variable of type T. /// IPair of type `T`. /// The value type. /// Event of type T. /// Event x 2 of type T. /// Function of type T => T [EditorIcon("atom-icon-hotpink")] [DefaultExecutionOrder(Runtime.ExecutionOrder.VARIABLE_INSTANCER)] public abstract class AtomVariableInstancer : AtomBaseVariableInstancer, IGetEvent, ISetEvent where V : AtomVariable where P : struct, IPair where E1 : AtomEvent where E2 : AtomEvent

where F : AtomFunction where CO : IGetValue where L : IGetValue { ///

/// Override to add implementation specific setup on `OnEnable`. /// protected override void ImplSpecificSetup() { if (Base.Changed != null) { _inMemoryCopy.Changed = Instantiate(Base.Changed); } if (Base.ChangedWithHistory != null) { _inMemoryCopy.ChangedWithHistory = Instantiate(Base.ChangedWithHistory); } } /// /// Get event by type. /// /// /// The event. public E GetEvent() where E : AtomEventBase { if (typeof(E) == typeof(E1)) return (_inMemoryCopy.Changed as E); if (typeof(E) == typeof(E2)) return (_inMemoryCopy.ChangedWithHistory as E); throw new Exception($"Event type {typeof(E)} not supported! Use {typeof(E1)} or {typeof(E2)}."); } /// /// Set event by type. /// /// The new event value. /// public void SetEvent(E e) where E : AtomEventBase { if (typeof(E) == typeof(E1)) { _inMemoryCopy.Changed = (e as E1); return; } if (typeof(E) == typeof(E2)) { _inMemoryCopy.ChangedWithHistory = (e as E2); return; } throw new Exception($"Event type {typeof(E)} not supported! Use {typeof(E1)} or {typeof(E2)}."); } } }