Guest User

Untitled

a guest
Mar 9th, 2017
271
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.Experimental.Director;
  4. using System.Collections.Generic;
  5.  
  6. public class AnimationController : MonoBehaviour
  7. {
  8.  
  9.     Animator animator;
  10.     PlayableGraph graph;
  11.     PlayableHandle mixer;
  12.    
  13.     int curInputIndex = -1;
  14.     private void Awake()
  15.     {
  16.        
  17.         animator = GetComponent<Animator>();
  18.         mixer = AnimationPlayableUtilities.PlayMixer(animator, 3, out graph);
  19.     }
  20.  
  21.     private void OnDestroy()
  22.     {
  23.         graph.Destroy();
  24.     }
  25.     bool hadInit = false;
  26.     public void Play(string animationName)
  27.     {
  28.         Debug.Log("Play " + animationName);
  29.         var clip = AnimationStore.Instance.GetAnimation(animationName);
  30.         PlayableHandle handle;
  31.         if(clip == null)
  32.         {
  33.             var controller = AnimationStore.Instance.GetController(animationName);
  34.             handle = graph.CreateAnimatorControllerPlayable(controller);
  35.         }
  36.         else
  37.         {
  38.             handle = graph.CreateAnimationClipPlayable(clip);
  39.         }
  40.         curInputIndex++;
  41.         if (curInputIndex == mixer.inputCount)
  42.             curInputIndex = 0;
  43.         if(mixer.GetInput(curInputIndex) != PlayableHandle.Null)
  44.         {
  45.             var input = mixer.GetInput(curInputIndex);
  46.             graph.Disconnect(mixer, curInputIndex);
  47.             input.Destroy();
  48.         }
  49.         graph.Connect(handle, 0, mixer, curInputIndex);
  50.         if(hadInit)
  51.             mixer.SetInputWeight(curInputIndex, 0f);
  52.         else
  53.         {
  54.             mixer.SetInputWeight(curInputIndex, 1f);
  55.             hadInit = true;
  56.         }
  57.        
  58.     }
  59.  
  60.     float fadeSpeed = 0.001f;
  61.     private void Update()
  62.     {
  63.         for (int i = 0; i < mixer.inputCount; i++)
  64.         {
  65.             if (i == curInputIndex)
  66.             {
  67.                 var w = mixer.GetInputWeight(i) + fadeSpeed;
  68.                 if (w >= 1f)
  69.                     w = 1f;
  70.                 mixer.SetInputWeight(i, w);
  71.             }
  72.             else
  73.             {
  74.                 var w = mixer.GetInputWeight(i) - fadeSpeed;
  75.                 if (w < fadeSpeed * 2)
  76.                     w = 0f;
  77.                 mixer.SetInputWeight(i, w);
  78.             }
  79.         }
  80.  
  81.     }
  82.  
  83.     private void OnGUI()
  84.     {
  85.         if(gameObject.name == "NPC")
  86.         {
  87.             GUI.Label(Rect.MinMaxRect(100, 100, 400, 200), mixer.GetInputWeight(0).ToString());
  88.             GUI.Label(Rect.MinMaxRect(100, 200, 400, 300), mixer.GetInputWeight(1).ToString());
  89.             GUI.Label(Rect.MinMaxRect(100, 300, 400, 400), mixer.GetInputWeight(2).ToString());
  90.         }
  91.     }
  92.  
  93. }
RAW Paste Data