ScreenFaderMixerBehaviour.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Playables;
  4. using UnityEngine.Timeline;
  5. using UnityEngine.UI;
  6. public class ScreenFaderMixerBehaviour : PlayableBehaviour
  7. {
  8. Color m_DefaultColor;
  9. Image m_TrackBinding;
  10. bool m_FirstFrameHappened;
  11. public override void ProcessFrame(Playable playable, FrameData info, object playerData)
  12. {
  13. m_TrackBinding = playerData as Image;
  14. if (m_TrackBinding == null)
  15. return;
  16. if (!m_FirstFrameHappened)
  17. {
  18. m_DefaultColor = m_TrackBinding.color;
  19. m_FirstFrameHappened = true;
  20. }
  21. int inputCount = playable.GetInputCount ();
  22. Color blendedColor = Color.clear;
  23. float totalWeight = 0f;
  24. float greatestWeight = 0f;
  25. int currentInputs = 0;
  26. for (int i = 0; i < inputCount; i++)
  27. {
  28. float inputWeight = playable.GetInputWeight(i);
  29. ScriptPlayable<ScreenFaderBehaviour> inputPlayable = (ScriptPlayable<ScreenFaderBehaviour>)playable.GetInput(i);
  30. ScreenFaderBehaviour input = inputPlayable.GetBehaviour ();
  31. blendedColor += input.color * inputWeight;
  32. totalWeight += inputWeight;
  33. if (inputWeight > greatestWeight)
  34. {
  35. greatestWeight = inputWeight;
  36. }
  37. if (!Mathf.Approximately (inputWeight, 0f))
  38. currentInputs++;
  39. }
  40. m_TrackBinding.color = blendedColor + m_DefaultColor * (1f - totalWeight);
  41. }
  42. public override void OnPlayableDestroy (Playable playable)
  43. {
  44. m_FirstFrameHappened = false;
  45. if (m_TrackBinding == null)
  46. return;
  47. m_TrackBinding.color = m_DefaultColor;
  48. }
  49. }