AnimatorParameter.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System;
  2. using UnityEngine;
  3. namespace Chronos.Reflection
  4. {
  5. [Serializable]
  6. public class AnimatorParameter
  7. {
  8. [SerializeField]
  9. private Animator _target;
  10. /// <summary>
  11. /// The animator containing the member.
  12. /// </summary>
  13. public Animator target
  14. {
  15. get { return _target; }
  16. set { _target = value; isLinked = false; }
  17. }
  18. [SerializeField]
  19. private string _name;
  20. /// <summary>
  21. /// The name of the parameter.
  22. /// </summary>
  23. public string name
  24. {
  25. get { return _name; }
  26. set { _name = value; isLinked = false; }
  27. }
  28. /// <summary>
  29. /// The underlying animator controller parameter.
  30. /// </summary>
  31. public AnimatorControllerParameter parameterInfo { get; private set; }
  32. /// <summary>
  33. /// Indicates whether the parameter has been found and analyzed.
  34. /// </summary>
  35. public bool isLinked { get; private set; }
  36. /// <summary>
  37. /// Indicates whether the animator parameter has been properly assigned.
  38. /// </summary>
  39. public bool isAssigned
  40. {
  41. get
  42. {
  43. return target != null && !string.IsNullOrEmpty(name);
  44. }
  45. }
  46. public AnimatorParameter() { }
  47. public AnimatorParameter(string name)
  48. {
  49. this.name = name;
  50. }
  51. public AnimatorParameter(string name, Animator target)
  52. {
  53. this.name = name;
  54. this.target = target;
  55. Link();
  56. }
  57. /// <summary>
  58. /// Fetches and caches the parameter.
  59. /// </summary>
  60. public void Link()
  61. {
  62. if (target == null)
  63. {
  64. throw new UnityException("Target has not been defined.");
  65. }
  66. foreach (AnimatorControllerParameter parameter in target.parameters)
  67. {
  68. if (parameter.name == name)
  69. {
  70. parameterInfo = parameter;
  71. return;
  72. }
  73. }
  74. throw new UnityException(string.Format("Animator parameter not found: '{0}'.", name));
  75. }
  76. /// <summary>
  77. /// Fetches and caches the parameter if it is not already present.
  78. /// </summary>
  79. protected void EnsureLinked()
  80. {
  81. if (!isLinked)
  82. {
  83. Link();
  84. }
  85. }
  86. /// <summary>
  87. /// Retrieves the value of the parameter.
  88. /// </summary>
  89. public object Get()
  90. {
  91. EnsureLinked();
  92. switch (parameterInfo.type)
  93. {
  94. case AnimatorControllerParameterType.Float: return target.GetFloat(parameterInfo.nameHash);
  95. case AnimatorControllerParameterType.Int: return target.GetInteger(parameterInfo.nameHash);
  96. case AnimatorControllerParameterType.Bool: return target.GetBool(parameterInfo.nameHash);
  97. case AnimatorControllerParameterType.Trigger: throw new UnityException("Cannot get the value of a trigger parameter.");
  98. default: throw new NotImplementedException();
  99. }
  100. }
  101. /// <summary>
  102. /// Retrieves the value of the parameter casted to the specified type.
  103. /// </summary>
  104. public T Get<T>() where T : struct
  105. {
  106. return (T)Get();
  107. }
  108. /// <summary>
  109. /// Assigns a new value to the parameter.
  110. /// </summary>
  111. public void Set(object value)
  112. {
  113. EnsureLinked();
  114. switch (parameterInfo.type)
  115. {
  116. case AnimatorControllerParameterType.Float: target.SetFloat(parameterInfo.nameHash, (float)value); break;
  117. case AnimatorControllerParameterType.Int: target.SetInteger(parameterInfo.nameHash, (int)value); break;
  118. case AnimatorControllerParameterType.Bool: target.SetBool(parameterInfo.nameHash, (bool)value); break;
  119. case AnimatorControllerParameterType.Trigger: throw new UnityException("Cannot set the value of a trigger parameter.");
  120. default: throw new NotImplementedException();
  121. }
  122. }
  123. /// <summary>
  124. /// Triggers the parameter.
  125. /// </summary>
  126. public void SetTrigger()
  127. {
  128. EnsureLinked();
  129. if (parameterInfo.type != AnimatorControllerParameterType.Trigger)
  130. {
  131. throw new UnityException("Parameter is not a trigger.");
  132. }
  133. target.SetTrigger(parameterInfo.nameHash);
  134. }
  135. /// <summary>
  136. /// Resets the trigger on the parameter.
  137. /// </summary>
  138. public void ResetTrigger()
  139. {
  140. EnsureLinked();
  141. if (parameterInfo.type != AnimatorControllerParameterType.Trigger)
  142. {
  143. throw new UnityException("Parameter is not a trigger.");
  144. }
  145. target.ResetTrigger(parameterInfo.nameHash);
  146. }
  147. /// <summary>
  148. /// The type of the parameter, or null if it is a trigger.
  149. /// </summary>
  150. public Type type
  151. {
  152. get
  153. {
  154. switch (parameterInfo.type)
  155. {
  156. case AnimatorControllerParameterType.Float: return typeof(float);
  157. case AnimatorControllerParameterType.Int: return typeof(int);
  158. case AnimatorControllerParameterType.Bool: return typeof(bool);
  159. case AnimatorControllerParameterType.Trigger: return null;
  160. default: throw new NotImplementedException();
  161. }
  162. }
  163. }
  164. public bool Corresponds(AnimatorParameter other)
  165. {
  166. return
  167. (other != null || !this.isAssigned) &&
  168. this.target == other.target &&
  169. this.name == other.name;
  170. }
  171. }
  172. }