Guest User

Untitled

a guest
Nov 23rd, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Playables;
  5. using UnityEngine.Animations;
  6. using UnityEngine.Timeline;
  7.  
  8. [RequireComponent (typeof(Animator))]
  9. public class SimpleAnimator : MonoBehaviour
  10. {
  11. PlayableGraph graph;
  12. AnimationMixerPlayable mixer;
  13. AnimationClipPlayable prePlayable, currentPlayable;
  14.  
  15. Coroutine coroutinePlayAnimation;
  16.  
  17. public List<AnimationClip> clipList;
  18.  
  19. void OnEnable ()
  20. {
  21. graph = PlayableGraph.Create ();
  22. AnimationPlayableOutput.Create(graph, name, GetComponent<Animator>());
  23.  
  24. mixer = AnimationMixerPlayable.Create (graph, 2, true);
  25.  
  26. var output = graph.GetOutput (0);
  27. output.SetSourcePlayable (mixer);
  28. if (clipList.Count != 0) {
  29. currentPlayable = AnimationClipPlayable.Create (graph, clipList [0]);
  30. mixer.ConnectInput (0, currentPlayable, 0);
  31. mixer.SetInputWeight (0, 1);
  32. graph.Play ();
  33. }
  34. }
  35.  
  36. void OnDisable()
  37. {
  38. graph.Destroy ();
  39. }
  40.  
  41. public void CrossFade (string animation, float fadeLength)
  42. {
  43. CrossFade (clipList.Find ((c) => c.name == animation), fadeLength);
  44. }
  45.  
  46. public void CrossFade (string animation)
  47. {
  48. CrossFade (animation, 0.3f);
  49. }
  50.  
  51. public void CrossFade (AnimationClip clip)
  52. {
  53. CrossFade (clip, 0.3f);
  54. }
  55.  
  56. public void CrossFade (AnimationClip clip, float fadeLength)
  57. {
  58. if( coroutinePlayAnimation != null)
  59. StopCoroutine (coroutinePlayAnimation);
  60. coroutinePlayAnimation = StartCoroutine (PlayAnimation (clip, fadeLength));
  61. }
  62.  
  63. public void Play (string clipName)
  64. {
  65. Play (clipList.Find ((c) => c.name == clipName));
  66. }
  67.  
  68. public void Play (AnimationClip newAnimation)
  69. {
  70. if (currentPlayable.IsValid ())
  71. currentPlayable.Destroy ();
  72. DisconnectPlayables ();
  73. currentPlayable = AnimationClipPlayable.Create (graph, newAnimation);
  74. mixer.ConnectInput (0, currentPlayable, 0);
  75.  
  76. mixer.SetInputWeight (1, 0);
  77. mixer.SetInputWeight (0, 1);
  78. }
  79.  
  80. void DisconnectPlayables ()
  81. {
  82. graph.Disconnect (mixer, 0);
  83. graph.Disconnect (mixer, 1);
  84. if (prePlayable.IsValid ())
  85. prePlayable.Destroy ();
  86. }
  87.  
  88. IEnumerator PlayAnimation (AnimationClip newAnimation, float transitionTime)
  89. {
  90. DisconnectPlayables ();
  91.  
  92. prePlayable = currentPlayable;
  93. currentPlayable = AnimationClipPlayable.Create (graph, newAnimation);
  94. mixer.ConnectInput (1, prePlayable, 0);
  95. mixer.ConnectInput (0, currentPlayable, 0);
  96.  
  97. // 指定時間でアニメーションをブレンド
  98. float waitTime = Time.timeSinceLevelLoad + transitionTime;
  99. yield return new WaitWhile (() => {
  100. var diff = waitTime - Time.timeSinceLevelLoad;
  101. if (diff <= 0) {
  102. mixer.SetInputWeight (1, 0);
  103. mixer.SetInputWeight (0, 1);
  104. return false;
  105. } else {
  106. var rate = Mathf.Clamp01 (diff / transitionTime);
  107. mixer.SetInputWeight (1, rate);
  108. mixer.SetInputWeight (0, 1 - rate);
  109. return true;
  110. }
  111. });
  112.  
  113. coroutinePlayAnimation = null;
  114. }
  115. }
Add Comment
Please, Sign In to add comment