Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using UnityEngine.Experimental.Director;
- using System.Collections.Generic;
- public class AnimationController : MonoBehaviour
- {
- Animator animator;
- PlayableGraph graph;
- PlayableHandle mixer;
- int curInputIndex = -1;
- private void Awake()
- {
- animator = GetComponent<Animator>();
- graph = PlayableGraph.CreateGraph();
- mixer = graph.CreateGenericMixerPlayable(3);
- AnimationPlayableUtilities.Play(animator, mixer, graph);
- }
- private void OnDestroy()
- {
- graph.Destroy();
- }
- bool hadInit = false;
- public void Play(string animationName)
- {
- Debug.Log("Play " + animationName);
- var clip = AnimationStore.Instance.GetAnimation(animationName);
- PlayableHandle handle;
- if(clip == null)
- {
- var controller = AnimationStore.Instance.GetController(animationName);
- handle = graph.CreateAnimatorControllerPlayable(controller);
- }
- else
- {
- handle = graph.CreateAnimationClipPlayable(clip);
- }
- curInputIndex++;
- if (curInputIndex == mixer.inputCount)
- curInputIndex = 0;
- if(mixer.GetInput(curInputIndex) != PlayableHandle.Null)
- {
- var input = mixer.GetInput(curInputIndex);
- graph.Disconnect(mixer, curInputIndex);
- input.Destroy();
- }
- graph.Connect(handle, 0, mixer, curInputIndex);
- if(hadInit)
- {
- mixer.SetInputWeight(curInputIndex, 0f);
- }
- else
- {
- mixer.SetInputWeight(curInputIndex, 1f);
- hadInit = true;
- }
- }
- float fadeSpeed = 0.001f;
- private void Update()
- {
- float totalWeight = 0f;
- for (int i = 0; i < mixer.inputCount; i++)
- {
- if (i == curInputIndex)
- {
- var w = mixer.GetInputWeight(i) + fadeSpeed;
- if (w >= 1f)
- w = 1f;
- mixer.SetInputWeight(i, w);
- totalWeight += w;
- }
- else
- {
- var w = mixer.GetInputWeight(i) - fadeSpeed;
- if (w < fadeSpeed * 2)
- w = 0f;
- mixer.SetInputWeight(i, w);
- totalWeight += w;
- }
- }
- if(totalWeight != 0f)
- for (int i = 0; i < mixer.inputCount; i++)
- {
- var w = mixer.GetInputWeight(i) / totalWeight;
- if (w < 0f)
- w = 0f;
- else if (w > 1f)
- w = 1f;
- mixer.SetInputWeight(i, w);
- }
- }
- private void OnGUI()
- {
- GraphVisualizerClient.Show(mixer.GetObject(), gameObject.name);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement