CubismMotionController.cs 10 KB

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