Advertisement
Guest User

Untitled

a guest
Mar 10th, 2017
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.87 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. using UnityEngine.Experimental.Director;
  5. using System.Collections.Generic;
  6.  
  7. public class AnimationController : MonoBehaviour
  8. {
  9.  
  10.     Animator animator;
  11.     PlayableGraph graph;
  12.     PlayableHandle mixer;
  13.     int curInputIndex = -1;
  14.     private void Awake()
  15.     {
  16.        
  17.         animator = GetComponent<Animator>();
  18.  
  19.         graph = PlayableGraph.CreateGraph();
  20.  
  21.         mixer = graph.CreateGenericMixerPlayable(3);
  22.         AnimationPlayableUtilities.Play(animator, mixer, graph);
  23.     }
  24.  
  25.     private void OnDestroy()
  26.     {
  27.         graph.Destroy();
  28.     }
  29.     bool hadInit = false;
  30.     public void Play(string animationName)
  31.     {
  32.         Debug.Log("Play " + animationName);
  33.         var clip = AnimationStore.Instance.GetAnimation(animationName);
  34.         PlayableHandle handle;
  35.        
  36.         if(clip == null)
  37.         {
  38.             var controller = AnimationStore.Instance.GetController(animationName);
  39.             handle = graph.CreateAnimatorControllerPlayable(controller);
  40.         }
  41.         else
  42.         {
  43.             handle = graph.CreateAnimationClipPlayable(clip);
  44.         }
  45.         curInputIndex++;
  46.         if (curInputIndex == mixer.inputCount)
  47.             curInputIndex = 0;
  48.         if(mixer.GetInput(curInputIndex) != PlayableHandle.Null)
  49.         {
  50.             var input = mixer.GetInput(curInputIndex);
  51.             graph.Disconnect(mixer, curInputIndex);
  52.             input.Destroy();
  53.         }
  54.         graph.Connect(handle, 0, mixer, curInputIndex);
  55.         if(hadInit)
  56.         {
  57.  
  58.             mixer.SetInputWeight(curInputIndex, 0f);
  59.         }
  60.         else
  61.         {
  62.             mixer.SetInputWeight(curInputIndex, 1f);
  63.             hadInit = true;
  64.         }
  65.  
  66.     }
  67.     float fadeSpeed = 0.001f;
  68.     private void Update()
  69.     {
  70.         float totalWeight = 0f;
  71.         for (int i = 0; i < mixer.inputCount; i++)
  72.         {
  73.             if (i == curInputIndex)
  74.             {
  75.                 var w = mixer.GetInputWeight(i) + fadeSpeed;
  76.                 if (w >= 1f)
  77.                     w = 1f;
  78.                 mixer.SetInputWeight(i, w);
  79.                 totalWeight += w;
  80.             }
  81.             else
  82.             {
  83.                 var w = mixer.GetInputWeight(i) - fadeSpeed;
  84.                 if (w < fadeSpeed * 2)
  85.                     w = 0f;
  86.                 mixer.SetInputWeight(i, w);
  87.                 totalWeight += w;
  88.  
  89.             }
  90.         }
  91.         if(totalWeight != 0f)
  92.         for (int i = 0; i < mixer.inputCount; i++)
  93.         {
  94.             var w = mixer.GetInputWeight(i) / totalWeight;
  95.             if (w < 0f)
  96.                 w = 0f;
  97.             else if (w > 1f)
  98.                 w = 1f;
  99.             mixer.SetInputWeight(i, w);
  100.         }
  101.        
  102.         }
  103.  
  104.     private void OnGUI()
  105.     {
  106.         GraphVisualizerClient.Show(mixer.GetObject(), gameObject.name);
  107.     }
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement