unity-atoms/Packages/Core/Runtime/References/AtomReference.cs
AdamRamberg 299dc195e2 - Added a top menu bar option to regenerate all existing Atoms. Nifty when developing the library.
- Added the option to use a Constant in Atom References. Related to #58
2019-10-16 18:02:08 +02:00

83 lines
2.1 KiB
C#

using UnityEngine;
namespace UnityAtoms
{
/// <summary>
/// None generic base class for `AtomReference&lt;T, V&gt;`.
/// </summary>
public abstract class AtomReference
{
/// <summary>
/// Enum for how to use the Reference.
/// </summary>
public enum Usage
{
Value = 0,
Constant = 1,
Variable = 2,
}
/// <summary>
/// Should we use the provided value (via inspector), the Constant value or the Variable value?
/// </summary>
[SerializeField]
protected Usage _usage;
}
public abstract class AtomReference<T, V, C> : AtomReference
where V : AtomBaseVariable<T>
where C : AtomBaseVariable<T>
{
/// <summary>
/// Value used if `Usage` is set to `Value`.
/// </summary>
[SerializeField]
private T _value;
/// <summary>
/// Constant used if `Usage` is set to `Constant`.
/// </summary>
public C _constant;
/// <summary>
/// Variable used if `Usage` is set to `Variable`.
/// </summary>
public V _variable;
protected AtomReference()
{
_usage = AtomReference.Usage.Value;
}
protected AtomReference(T value) : this()
{
_usage = AtomReference.Usage.Value;
_value = value;
}
/// <summary>
/// Get the value for the Reference.
/// </summary>
/// <value>The value of type `T`.</value>
public T Value
{
get
{
switch (_usage)
{
case (AtomReference.Usage.Constant): return _constant.Value;
case (AtomReference.Usage.Variable): return _variable.Value;
case (AtomReference.Usage.Value):
default:
return _value;
}
}
}
public static implicit operator T(AtomReference<T, V, C> reference)
{
return reference.Value;
}
}
}