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 associated 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, IGetOrCreateEvent where V : AtomVariable where P : struct, IPair where E1 : AtomEvent where E2 : AtomEvent

where F : AtomFunction { ///

/// 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); } // Manually trigger initial events since base class has already instantiated Variable // and the Variable's OnEnable hook has therefore already been executed. _inMemoryCopy.TriggerInitialEvents(); } /// /// Get event by type. /// /// /// The event. public E GetEvent() where E : AtomEventBase { return _inMemoryCopy.GetEvent(); } /// /// Set event by type. /// /// The new event value. /// public void SetEvent(E e) where E : AtomEventBase { _inMemoryCopy.SetEvent(e); } /// /// Get event by type. Creates it if it doesn't exist. /// /// /// The event. public E GetOrCreateEvent() where E : AtomEventBase { return _inMemoryCopy.GetOrCreateEvent(); } } }