TextSwitcherMixerBehaviour.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Playables;
  4. using UnityEngine.Timeline;
  5. using UnityEngine.UI;
  6. public class TextSwitcherMixerBehaviour : PlayableBehaviour
  7. {
  8. Color m_DefaultColor;
  9. int m_DefaultFontSize;
  10. string m_DefaultText;
  11. Text m_TrackBinding;
  12. bool m_FirstFrameHappened;
  13. public override void ProcessFrame(Playable playable, FrameData info, object playerData)
  14. {
  15. m_TrackBinding = playerData as Text;
  16. if (m_TrackBinding == null)
  17. return;
  18. if (!m_FirstFrameHappened)
  19. {
  20. m_DefaultColor = m_TrackBinding.color;
  21. m_DefaultFontSize = m_TrackBinding.fontSize;
  22. m_DefaultText = m_TrackBinding.text;
  23. m_FirstFrameHappened = true;
  24. }
  25. int inputCount = playable.GetInputCount ();
  26. Color blendedColor = Color.clear;
  27. float blendedFontSize = 0f;
  28. float totalWeight = 0f;
  29. float greatestWeight = 0f;
  30. int currentInputs = 0;
  31. for (int i = 0; i < inputCount; i++)
  32. {
  33. float inputWeight = playable.GetInputWeight(i);
  34. ScriptPlayable<TextSwitcherBehaviour> inputPlayable = (ScriptPlayable<TextSwitcherBehaviour>)playable.GetInput(i);
  35. TextSwitcherBehaviour input = inputPlayable.GetBehaviour ();
  36. blendedColor += input.color * inputWeight;
  37. blendedFontSize += input.fontSize * inputWeight;
  38. totalWeight += inputWeight;
  39. if (inputWeight > greatestWeight)
  40. {
  41. m_TrackBinding.text = input.text;
  42. greatestWeight = inputWeight;
  43. }
  44. if (!Mathf.Approximately (inputWeight, 0f))
  45. currentInputs++;
  46. }
  47. m_TrackBinding.color = blendedColor + m_DefaultColor * (1f - totalWeight);
  48. m_TrackBinding.fontSize = Mathf.RoundToInt (blendedFontSize + m_DefaultFontSize * (1f - totalWeight));
  49. if (currentInputs != 1 && 1f - totalWeight > greatestWeight)
  50. {
  51. m_TrackBinding.text = m_DefaultText;
  52. }
  53. }
  54. public override void OnPlayableDestroy (Playable playable)
  55. {
  56. m_FirstFrameHappened = false;
  57. if (m_TrackBinding == null)
  58. return;
  59. m_TrackBinding.color = m_DefaultColor;
  60. m_TrackBinding.fontSize = m_DefaultFontSize;
  61. m_TrackBinding.text = m_DefaultText;
  62. }
  63. }