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 AtomBaseVariableInstancer : MonoBehaviour, IVariable
where V : AtomBaseVariable
{
///
/// Getter for retrieving the in memory runtime variable.
///
public V Variable { get => _inMemoryCopy; }
///
/// Getter for retrieving the value of the in memory runtime variable.
///
public T Value { get => _inMemoryCopy.Value; set => _inMemoryCopy.Value = value; }
public virtual V Base { get => _base; }
[SerializeField]
[ReadOnly]
protected V _inMemoryCopy = default(V);
///
/// The variable that the in memory copy will be based on when created at runtime.
///
[SerializeField]
protected V _base = null;
///
/// Override to add implementation specific setup on `OnEnable`.
///
protected virtual void ImplSpecificSetup() { }
private void OnEnable()
{
Assert.IsNotNull(Base);
_inMemoryCopy = Instantiate(Base);
ImplSpecificSetup();
}
}
}