using System;
using UnityEngine;
namespace Chronos.Reflection
{
[Serializable]
public class AnimatorParameter
{
[SerializeField]
private Animator _target;
///
/// The animator containing the member.
///
public Animator target
{
get { return _target; }
set { _target = value; isLinked = false; }
}
[SerializeField]
private string _name;
///
/// The name of the parameter.
///
public string name
{
get { return _name; }
set { _name = value; isLinked = false; }
}
///
/// The underlying animator controller parameter.
///
public AnimatorControllerParameter parameterInfo { get; private set; }
///
/// Indicates whether the parameter has been found and analyzed.
///
public bool isLinked { get; private set; }
///
/// Indicates whether the animator parameter has been properly assigned.
///
public bool isAssigned
{
get
{
return target != null && !string.IsNullOrEmpty(name);
}
}
public AnimatorParameter() { }
public AnimatorParameter(string name)
{
this.name = name;
}
public AnimatorParameter(string name, Animator target)
{
this.name = name;
this.target = target;
Link();
}
///
/// Fetches and caches the parameter.
///
public void Link()
{
if (target == null)
{
throw new UnityException("Target has not been defined.");
}
foreach (AnimatorControllerParameter parameter in target.parameters)
{
if (parameter.name == name)
{
parameterInfo = parameter;
return;
}
}
throw new UnityException(string.Format("Animator parameter not found: '{0}'.", name));
}
///
/// Fetches and caches the parameter if it is not already present.
///
protected void EnsureLinked()
{
if (!isLinked)
{
Link();
}
}
///
/// Retrieves the value of the parameter.
///
public object Get()
{
EnsureLinked();
switch (parameterInfo.type)
{
case AnimatorControllerParameterType.Float: return target.GetFloat(parameterInfo.nameHash);
case AnimatorControllerParameterType.Int: return target.GetInteger(parameterInfo.nameHash);
case AnimatorControllerParameterType.Bool: return target.GetBool(parameterInfo.nameHash);
case AnimatorControllerParameterType.Trigger: throw new UnityException("Cannot get the value of a trigger parameter.");
default: throw new NotImplementedException();
}
}
///
/// Retrieves the value of the parameter casted to the specified type.
///
public T Get() where T : struct
{
return (T)Get();
}
///
/// Assigns a new value to the parameter.
///
public void Set(object value)
{
EnsureLinked();
switch (parameterInfo.type)
{
case AnimatorControllerParameterType.Float: target.SetFloat(parameterInfo.nameHash, (float)value); break;
case AnimatorControllerParameterType.Int: target.SetInteger(parameterInfo.nameHash, (int)value); break;
case AnimatorControllerParameterType.Bool: target.SetBool(parameterInfo.nameHash, (bool)value); break;
case AnimatorControllerParameterType.Trigger: throw new UnityException("Cannot set the value of a trigger parameter.");
default: throw new NotImplementedException();
}
}
///
/// Triggers the parameter.
///
public void SetTrigger()
{
EnsureLinked();
if (parameterInfo.type != AnimatorControllerParameterType.Trigger)
{
throw new UnityException("Parameter is not a trigger.");
}
target.SetTrigger(parameterInfo.nameHash);
}
///
/// Resets the trigger on the parameter.
///
public void ResetTrigger()
{
EnsureLinked();
if (parameterInfo.type != AnimatorControllerParameterType.Trigger)
{
throw new UnityException("Parameter is not a trigger.");
}
target.ResetTrigger(parameterInfo.nameHash);
}
///
/// The type of the parameter, or null if it is a trigger.
///
public Type type
{
get
{
switch (parameterInfo.type)
{
case AnimatorControllerParameterType.Float: return typeof(float);
case AnimatorControllerParameterType.Int: return typeof(int);
case AnimatorControllerParameterType.Bool: return typeof(bool);
case AnimatorControllerParameterType.Trigger: return null;
default: throw new NotImplementedException();
}
}
}
public bool Corresponds(AnimatorParameter other)
{
return
(other != null || !this.isAssigned) &&
this.target == other.target &&
this.name == other.name;
}
}
}