TimeDilationMixerBehaviour.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Playables;
  4. using UnityEngine.Timeline;
  5. public class TimeDilationMixerBehaviour : PlayableBehaviour
  6. {
  7. float m_OldTimeScale = 1f;
  8. public override void OnPlayableCreate (Playable playable)
  9. {
  10. m_OldTimeScale = Time.timeScale;
  11. }
  12. public override void ProcessFrame(Playable playable, FrameData info, object playerData)
  13. {
  14. int inputCount = playable.GetInputCount ();
  15. float mixedTimeScale = 0f;
  16. float totalWeight = 0f;
  17. for (int i = 0; i < inputCount; i++)
  18. {
  19. float inputWeight = playable.GetInputWeight(i);
  20. totalWeight += inputWeight;
  21. ScriptPlayable<TimeDilationBehaviour> playableInput = (ScriptPlayable<TimeDilationBehaviour>)playable.GetInput (i);
  22. TimeDilationBehaviour input = playableInput.GetBehaviour ();
  23. mixedTimeScale += inputWeight * input.timeScale;
  24. }
  25. Time.timeScale = mixedTimeScale + m_OldTimeScale * (1f - totalWeight);
  26. }
  27. public override void OnPlayableDestroy (Playable playable)
  28. {
  29. Time.timeScale = m_OldTimeScale;
  30. }
  31. }