설명 없음
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TextTrackMixerBehaviour.cs 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #if TEXT_TRACK_REQUIRES_TEXTMESH_PRO
  2. using TMPro;
  3. using UnityEngine;
  4. using UnityEngine.Playables;
  5. namespace Timeline.Samples
  6. {
  7. // The runtime instance of a the TextTrack. It is responsible for blending and setting the final data
  8. // on the Text binding
  9. public class TextTrackMixerBehaviour : PlayableBehaviour
  10. {
  11. Color m_DefaultColor;
  12. float m_DefaultFontSize;
  13. string m_DefaultText;
  14. TMP_Text m_TrackBinding;
  15. // Called every frame that the timeline is evaluated. ProcessFrame is invoked after its' inputs.
  16. public override void ProcessFrame(Playable playable, FrameData info, object playerData)
  17. {
  18. SetDefaults(playerData as TMP_Text);
  19. if (m_TrackBinding == null)
  20. return;
  21. int inputCount = playable.GetInputCount();
  22. Color blendedColor = Color.clear;
  23. float blendedFontSize = 0f;
  24. float totalWeight = 0f;
  25. float greatestWeight = 0f;
  26. string text = m_DefaultText;
  27. for (int i = 0; i < inputCount; i++)
  28. {
  29. float inputWeight = playable.GetInputWeight(i);
  30. ScriptPlayable<TextPlayableBehaviour> inputPlayable = (ScriptPlayable<TextPlayableBehaviour>)playable.GetInput(i);
  31. TextPlayableBehaviour input = inputPlayable.GetBehaviour();
  32. blendedColor += input.color * inputWeight;
  33. blendedFontSize += input.fontSize * inputWeight;
  34. totalWeight += inputWeight;
  35. // use the text with the highest weight
  36. if (inputWeight > greatestWeight)
  37. {
  38. text = input.text;
  39. greatestWeight = inputWeight;
  40. }
  41. }
  42. // blend to the default values
  43. m_TrackBinding.color = Color.Lerp(m_DefaultColor, blendedColor, totalWeight);
  44. m_TrackBinding.fontSize = Mathf.RoundToInt(Mathf.Lerp(m_DefaultFontSize, blendedFontSize, totalWeight));
  45. m_TrackBinding.text = text;
  46. }
  47. // Invoked when the playable graph is destroyed, typically when PlayableDirector.Stop is called or the timeline
  48. // is complete.
  49. public override void OnPlayableDestroy(Playable playable)
  50. {
  51. RestoreDefaults();
  52. }
  53. void SetDefaults(TMP_Text text)
  54. {
  55. if (text == m_TrackBinding)
  56. return;
  57. RestoreDefaults();
  58. m_TrackBinding = text;
  59. if (m_TrackBinding != null)
  60. {
  61. m_DefaultColor = m_TrackBinding.color;
  62. m_DefaultFontSize = m_TrackBinding.fontSize;
  63. m_DefaultText = m_TrackBinding.text;
  64. }
  65. }
  66. void RestoreDefaults()
  67. {
  68. if (m_TrackBinding == null)
  69. return;
  70. m_TrackBinding.color = m_DefaultColor;
  71. m_TrackBinding.fontSize = m_DefaultFontSize;
  72. m_TrackBinding.text = m_DefaultText;
  73. }
  74. }
  75. }
  76. #endif