AnimationMixerBehaviour.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using UnityEngine;
  2. using UnityEngine.Playables;
  3. namespace UnityUIPlayables
  4. {
  5. public class AnimationMixerBehaviour<TBinding, TValueMixer, TAnimationBehaviour> : PlayableBehaviour
  6. where TValueMixer : AnimationMixer<TBinding, TAnimationBehaviour>, new()
  7. where TBinding : class
  8. where TAnimationBehaviour : AnimationBehaviour, new()
  9. {
  10. private TValueMixer _mixer = new TValueMixer();
  11. public override void ProcessFrame(Playable playable, FrameData info, object playerData)
  12. {
  13. if (!(playerData is TBinding binding))
  14. {
  15. return;
  16. }
  17. var inputCount = playable.GetInputCount();
  18. var mixer = new TValueMixer();
  19. mixer.SetupFrame(binding);
  20. for (var i = 0; i < inputCount; i++)
  21. {
  22. var playableInput = (ScriptPlayable<TAnimationBehaviour>) playable.GetInput(i);
  23. var behaviour = playableInput.GetBehaviour();
  24. var inputWeight = playable.GetInputWeight(i);
  25. var time = (float) playableInput.GetTime();
  26. var duration = (float) playableInput.GetDuration();
  27. var progress = behaviour.EvaluateCurve(time, duration);
  28. if (inputWeight == 0)
  29. {
  30. continue;
  31. }
  32. mixer.Blend(behaviour, inputWeight, progress);
  33. }
  34. mixer.ApplyFrame();
  35. }
  36. }
  37. }