mirror of
https://github.com/unity-atoms/unity-atoms.git
synced 2025-01-24 00:58:59 -05:00
299dc195e2
- Added the option to use a Constant in Atom References. Related to #58
83 lines
2.1 KiB
C#
83 lines
2.1 KiB
C#
using UnityEngine;
|
|
|
|
namespace UnityAtoms
|
|
{
|
|
/// <summary>
|
|
/// None generic base class for `AtomReference<T, V>`.
|
|
/// </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;
|
|
}
|
|
}
|
|
}
|