PlayerAnimatorController.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using XRTool.Util;
  5. namespace ShadowStudio.Model
  6. {
  7. /// <summary>
  8. /// 用户角色控制器
  9. /// </summary>
  10. public class PlayerAnimatorController : MonoBehaviour
  11. {
  12. private Animator playerAni;
  13. public Transform playerHead;
  14. private Transform t_renMAN;
  15. private Transform leftHand;
  16. private Transform rightHand;
  17. private Transform handLeft;
  18. private Transform handRight;
  19. private Animator leftHandAnim;
  20. private Animator rightHandAnim;
  21. private int leftState;
  22. private int rightState;
  23. public void SetHands(Transform leftHand, Transform rightHand)
  24. {
  25. this.leftHand = leftHand;
  26. this.rightHand = rightHand;
  27. }
  28. private void Start()
  29. {
  30. if (HandLeft)
  31. {
  32. HandLeft.gameObject.SetActive(false);
  33. }
  34. if (HandRight)
  35. {
  36. HandRight.gameObject.SetActive(false);
  37. }
  38. if(CScript.App.AppManager.Instance.isRunHolographic)
  39. {
  40. HandLeft.gameObject.SetActive(false);
  41. HandRight.gameObject.SetActive(false);
  42. T_renMAN.gameObject.SetActive(false);
  43. }
  44. }
  45. public Animator PlayerAni
  46. {
  47. get
  48. {
  49. if (!playerAni)
  50. {
  51. playerAni = GetComponent<Animator>();
  52. }
  53. return playerAni;
  54. }
  55. }
  56. public Transform T_renMAN
  57. {
  58. get
  59. {
  60. if (!t_renMAN)
  61. {
  62. t_renMAN = UnityUtil.GetBreadthChild<Transform>(transform, "T_renMAN");
  63. }
  64. return T_renMAN;
  65. }
  66. }
  67. public Transform HandLeft
  68. {
  69. get
  70. {
  71. if (!handLeft)
  72. {
  73. handLeft = UnityUtil.GetBreadthChild<Transform>(transform, "HandLeft");
  74. }
  75. return handLeft;
  76. }
  77. }
  78. public Transform HandRight
  79. {
  80. get
  81. {
  82. if (!handRight)
  83. {
  84. handRight = UnityUtil.GetBreadthChild<Transform>(transform, "HandRight");
  85. }
  86. return handRight;
  87. }
  88. }
  89. public Animator LeftHandAnim
  90. {
  91. get
  92. {
  93. if (!leftHandAnim && HandLeft)
  94. {
  95. leftHandAnim = HandLeft.GetComponent<Animator>();
  96. }
  97. return leftHandAnim;
  98. }
  99. }
  100. public Animator RightHandAnim
  101. {
  102. get
  103. {
  104. if (!rightHandAnim && HandRight)
  105. {
  106. rightHandAnim = HandRight.GetComponent<Animator>();
  107. }
  108. return rightHandAnim;
  109. }
  110. }
  111. /// <summary>
  112. /// 角色IK动画设置
  113. /// </summary>
  114. /// <param name="layerIndex"></param>
  115. //private void OnAnimatorIK(int layerIndex)
  116. //{
  117. // if (PlayerAni)
  118. // {
  119. // if (leftHand)
  120. // {
  121. // SetPlayerIKWeight(AvatarIKGoal.LeftHand);
  122. // SetPlayerIK(AvatarIKGoal.LeftHand, leftHand);
  123. // }
  124. // if (rightHand)
  125. // {
  126. // SetPlayerIKWeight(AvatarIKGoal.RightHand);
  127. // SetPlayerIK(AvatarIKGoal.RightHand, rightHand);
  128. // }
  129. // }
  130. //}
  131. public void SetPlayerIK(AvatarIKGoal goal, Vector3 pos, Quaternion rotation)
  132. {
  133. PlayerAni.SetIKPosition(goal, pos);
  134. PlayerAni.SetIKRotation(goal, rotation);
  135. }
  136. public void SetPlayerIK(AvatarIKGoal goal, Transform target)
  137. {
  138. SetPlayerIK(goal, target.position, target.rotation);
  139. }
  140. /// <summary>
  141. /// 设置头部信息
  142. /// </summary>
  143. /// <param name="goal"></param>
  144. public void SetPlayerIKWeight(AvatarIKGoal goal)
  145. {
  146. PlayerAni.SetIKPositionWeight(goal, 1f);
  147. PlayerAni.SetIKRotationWeight(goal, 1f);
  148. }
  149. public void SetAnimInt(Animator animator, string stateName, int state)
  150. {
  151. if (animator)
  152. {
  153. animator.SetInteger(stateName, state);
  154. }
  155. }
  156. public int LeftHandState
  157. {
  158. get
  159. {
  160. return leftState;
  161. }
  162. set
  163. {
  164. if (LeftHandAnim)
  165. {
  166. SetAnimInt(LeftHandAnim, "State", leftState = value);
  167. }
  168. }
  169. }
  170. public int RightHandState
  171. {
  172. get
  173. {
  174. return rightState;
  175. }
  176. set
  177. {
  178. if (RightHandAnim)
  179. {
  180. SetAnimInt(RightHandAnim, "State", rightState = value);
  181. }
  182. }
  183. }
  184. }
  185. }