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