CubismMotionController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /**
  2. * Copyright(c) Live2D Inc. All rights reserved.
  3. *
  4. * Use of this source code is governed by the Live2D Open Software license
  5. * that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
  6. */
  7. using Live2D.Cubism.Core;
  8. using Live2D.Cubism.Framework.MotionFade;
  9. using System;
  10. using UnityEngine;
  11. using UnityEngine.Animations;
  12. using UnityEngine.Playables;
  13. namespace Live2D.Cubism.Framework.Motion
  14. {
  15. /// <summary>
  16. /// Cubism motion controller.
  17. /// </summary>
  18. [RequireComponent(typeof(CubismFadeController))]
  19. public class CubismMotionController : MonoBehaviour
  20. {
  21. #region Action
  22. /// <summary>
  23. /// Action animation start handler.
  24. /// </summary>
  25. [SerializeField]
  26. public Action<int> AnimationBeginHandler;
  27. /// <summary>
  28. /// Action animation end handler.
  29. /// </summary>
  30. [SerializeField]
  31. public Action<int> AnimationEndHandler;
  32. /// <summary>
  33. /// Action OnAnimationEnd.
  34. /// </summary>
  35. private void OnAnimationBegin(int layerIndex, int instanceId)
  36. {
  37. if (AnimationBeginHandler != null)
  38. {
  39. AnimationBeginHandler(instanceId);
  40. }
  41. }
  42. /// <summary>
  43. /// Action OnAnimationEnd.
  44. /// </summary>
  45. private void OnAnimationEnd(int layerIndex, int instanceId)
  46. {
  47. _motionPriorities[layerIndex] = CubismMotionPriority.PriorityNone;
  48. if (AnimationEndHandler != null)
  49. {
  50. AnimationEndHandler(instanceId);
  51. }
  52. }
  53. #endregion
  54. #region Variable
  55. /// <summary>
  56. /// Layer count.
  57. /// </summary>
  58. public int LayerCount = 1;
  59. /// <summary>
  60. /// List of cubism fade motion.
  61. /// </summary>
  62. private CubismFadeMotionList _cubismFadeMotionList;
  63. /// <summary>
  64. /// Motion controller is active.
  65. /// </summary>
  66. private bool _isActive = false;
  67. /// <summary>
  68. /// Playable graph controller.
  69. /// </summary>
  70. private PlayableGraph _playableGrap;
  71. /// <summary>
  72. /// Playable output.
  73. /// </summary>
  74. private AnimationPlayableOutput _playableOutput;
  75. /// <summary>
  76. /// Animation layer mixer.
  77. /// </summary>
  78. private AnimationLayerMixerPlayable _layerMixer;
  79. /// <summary>
  80. /// Cubism motion layers.
  81. /// </summary>
  82. private CubismMotionLayer[] _motionLayers;
  83. /// <summary>
  84. /// Cubism motion priorities.
  85. /// </summary>
  86. private int[] _motionPriorities;
  87. #endregion Variable
  88. #region Function
  89. /// <summary>
  90. /// Play animations.
  91. /// </summary>
  92. /// <param name="clip">Animator clip.</param>
  93. /// <param name="layerIndex">layer index.</param>
  94. /// <param name="priority">Animation priority</param>
  95. /// <param name="isLoop">Animation is loop.</param>
  96. /// <param name="speed">Animation speed.</param>
  97. public void PlayAnimation(AnimationClip clip, int layerIndex = 0, int priority = CubismMotionPriority.PriorityNormal, bool isLoop = true, float speed = 1.0f)
  98. {
  99. // Fail silently...
  100. if(!enabled || !_isActive || _cubismFadeMotionList == null || clip == null
  101. || layerIndex < 0 || layerIndex >= LayerCount ||
  102. ((_motionPriorities[layerIndex] >= priority) && (priority != CubismMotionPriority.PriorityForce)))
  103. {
  104. Debug.Log("can't start motion.");
  105. return;
  106. }
  107. _motionPriorities[layerIndex] = priority;
  108. _motionLayers[layerIndex].PlayAnimation(clip, isLoop, speed);
  109. // Play Playable Graph
  110. if(!_playableGrap.IsPlaying())
  111. {
  112. _playableGrap.Play();
  113. }
  114. }
  115. /// <summary>
  116. /// Stop animation.
  117. /// </summary>
  118. /// <param name="animationIndex">Animator index.</param>
  119. /// <param name="layerIndex">layer index.</param>
  120. public void StopAnimation(int animationIndex, int layerIndex = 0)
  121. {
  122. // Fail silently...
  123. if(layerIndex < 0 || layerIndex >= LayerCount)
  124. {
  125. return;
  126. }
  127. _motionLayers[layerIndex].StopAnimationClip();
  128. }
  129. /// <summary>
  130. /// Stop all animation.
  131. /// </summary>
  132. public void StopAllAnimation()
  133. {
  134. for(var i = 0; i < LayerCount; ++i)
  135. {
  136. _motionLayers[i].StopAnimationClip();
  137. }
  138. }
  139. /// <summary>
  140. /// Is playing animation.
  141. /// </summary>
  142. /// <returns>True if the animation is playing.</returns>
  143. public bool IsPlayingAnimation(int layerIndex = 0)
  144. {
  145. // Fail silently...
  146. if(layerIndex < 0 || layerIndex >= LayerCount)
  147. {
  148. return false;
  149. }
  150. return !_motionLayers[layerIndex].IsFinished;
  151. }
  152. /// <summary>
  153. /// Set layer weight.
  154. /// </summary>
  155. /// <param name="layerIndex">layer index.</param>
  156. /// <param name="weight">Layer weight.</param>
  157. public void SetLayerWeight(int layerIndex, float weight)
  158. {
  159. // Fail silently...
  160. if(layerIndex <= 0 || layerIndex >= LayerCount)
  161. {
  162. return;
  163. }
  164. _motionLayers[layerIndex].SetLayerWeight(weight);
  165. _layerMixer.SetInputWeight(layerIndex, weight);
  166. }
  167. /// <summary>
  168. /// Set layer blend type is additive.
  169. /// </summary>
  170. /// <param name="layerIndex">layer index.</param>
  171. /// <param name="isAdditive">Blend type is additive.</param>
  172. public void SetLayerAdditive(int layerIndex, bool isAdditive)
  173. {
  174. // Fail silently...
  175. if(layerIndex <= 0 || layerIndex >= LayerCount)
  176. {
  177. return;
  178. }
  179. _layerMixer.SetLayerAdditive((uint)layerIndex, isAdditive);
  180. }
  181. /// <summary>
  182. /// Set animation speed.
  183. /// </summary>
  184. /// <param name="layerIndex">layer index.</param>
  185. /// <param name="index">index of playing motion list.</param>
  186. /// <param name="speed">Animation speed.</param>
  187. public void SetAnimationSpeed(int layerIndex, int index, float speed)
  188. {
  189. // Fail silently...
  190. if(layerIndex < 0 || layerIndex >= LayerCount)
  191. {
  192. return;
  193. }
  194. _motionLayers[layerIndex].SetStateSpeed(index, speed);
  195. }
  196. /// <summary>
  197. /// Set animation is loop.
  198. /// </summary>
  199. /// <param name="layerIndex">layer index.</param>
  200. /// <param name="index">Index of playing motion list.</param>
  201. /// <param name="isLoop">State is loop.</param>
  202. public void SetAnimationIsLoop(int layerIndex, int index, bool isLoop)
  203. {
  204. // Fail silently...
  205. if(layerIndex < 0 || layerIndex >= LayerCount)
  206. {
  207. return;
  208. }
  209. _motionLayers[layerIndex].SetStateIsLoop(index, isLoop);
  210. }
  211. /// <summary>
  212. /// Get cubism fade states.
  213. /// </summary>
  214. public ICubismFadeState[] GetFadeStates()
  215. {
  216. if(_motionLayers == null)
  217. {
  218. LayerCount = (LayerCount < 1) ? 1 : LayerCount;
  219. _motionLayers = new CubismMotionLayer[LayerCount];
  220. _motionPriorities = new int[LayerCount];
  221. }
  222. return _motionLayers;
  223. }
  224. #endregion Function
  225. #region Unity Events Handling
  226. /// <summary>
  227. /// Called by Unity.
  228. /// </summary>
  229. private void OnEnable()
  230. {
  231. _cubismFadeMotionList = GetComponent<CubismFadeController>().CubismFadeMotionList;
  232. // Fail silently...
  233. if(_cubismFadeMotionList == null)
  234. {
  235. Debug.LogError("CubismMotionController : CubismFadeMotionList doesn't set in CubismFadeController.");
  236. return;
  237. }
  238. // Get Animator.
  239. var animator = GetComponent<Animator>();
  240. if (animator.runtimeAnimatorController != null)
  241. {
  242. Debug.LogWarning("Animator Controller was set in Animator component.");
  243. return;
  244. }
  245. _isActive = true;
  246. // Disable animator's playablegrap.
  247. var graph = animator.playableGraph;
  248. if(graph.IsValid())
  249. {
  250. graph.GetOutput(0).SetWeight(0);
  251. }
  252. // Create Playable Graph.
  253. #if UNITY_2018_1_OR_NEWER
  254. _playableGrap = PlayableGraph.Create("Playable Graph : " + this.FindCubismModel().name);
  255. #else
  256. _playableGrap = PlayableGraph.Create();
  257. #endif
  258. _playableGrap.SetTimeUpdateMode(DirectorUpdateMode.GameTime);
  259. // Create Playable Output.
  260. _playableOutput = AnimationPlayableOutput.Create(_playableGrap, "Animation", animator);
  261. _playableOutput.SetWeight(1);
  262. // Create animation layer mixer.
  263. _layerMixer = AnimationLayerMixerPlayable.Create(_playableGrap, LayerCount);
  264. // Create cubism motion layers.
  265. if(_motionLayers == null)
  266. {
  267. LayerCount = (LayerCount < 1) ? 1 : LayerCount;
  268. _motionLayers = new CubismMotionLayer[LayerCount];
  269. _motionPriorities = new int[LayerCount];
  270. }
  271. for(var i = 0; i < LayerCount; ++i)
  272. {
  273. _motionLayers[i] = CubismMotionLayer.CreateCubismMotionLayer(_playableGrap, _cubismFadeMotionList, i);
  274. _motionLayers[i].AnimationBeginHandler += OnAnimationBegin;
  275. _motionLayers[i].AnimationEndHandler += OnAnimationEnd;
  276. _layerMixer.ConnectInput(i, _motionLayers[i].PlayableOutput, 0);
  277. _layerMixer.SetInputWeight(i, 1.0f);
  278. }
  279. // Set Playable Output.
  280. _playableOutput.SetSourcePlayable(_layerMixer);
  281. }
  282. /// <summary>
  283. /// Called by Unity.
  284. /// </summary>
  285. private void OnDisable()
  286. {
  287. // Destroy _playableGrap.
  288. if(_playableGrap.IsValid())
  289. {
  290. _playableGrap.Destroy();
  291. }
  292. }
  293. /// <summary>
  294. /// Called by Unity.
  295. /// </summary>
  296. private void Update()
  297. {
  298. // Fail silently...
  299. if(!_isActive)
  300. {
  301. return;
  302. }
  303. for( var i = 0; i < _motionLayers.Length; ++i)
  304. {
  305. _motionLayers[i].Update();
  306. if (_motionLayers[i].IsFinished)
  307. {
  308. _motionPriorities[i] = 0;
  309. }
  310. }
  311. }
  312. #endregion Unity Events Handling
  313. }
  314. }