123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- using System;
- using UnityEngine;
- using UnityEngine.Playables;
- using UnityEngine.Timeline;
- using UnityEngine.UI;
- public class TextSwitcherMixerBehaviour : PlayableBehaviour
- {
- Color m_DefaultColor;
- int m_DefaultFontSize;
- string m_DefaultText;
- Text m_TrackBinding;
- bool m_FirstFrameHappened;
- public override void ProcessFrame(Playable playable, FrameData info, object playerData)
- {
- m_TrackBinding = playerData as Text;
- if (m_TrackBinding == null)
- return;
- if (!m_FirstFrameHappened)
- {
- m_DefaultColor = m_TrackBinding.color;
- m_DefaultFontSize = m_TrackBinding.fontSize;
- m_DefaultText = m_TrackBinding.text;
- m_FirstFrameHappened = true;
- }
- int inputCount = playable.GetInputCount ();
- Color blendedColor = Color.clear;
- float blendedFontSize = 0f;
- float totalWeight = 0f;
- float greatestWeight = 0f;
- int currentInputs = 0;
- for (int i = 0; i < inputCount; i++)
- {
- float inputWeight = playable.GetInputWeight(i);
- ScriptPlayable<TextSwitcherBehaviour> inputPlayable = (ScriptPlayable<TextSwitcherBehaviour>)playable.GetInput(i);
- TextSwitcherBehaviour input = inputPlayable.GetBehaviour ();
-
- blendedColor += input.color * inputWeight;
- blendedFontSize += input.fontSize * inputWeight;
- totalWeight += inputWeight;
- if (inputWeight > greatestWeight)
- {
- m_TrackBinding.text = input.text;
- greatestWeight = inputWeight;
- }
- if (!Mathf.Approximately (inputWeight, 0f))
- currentInputs++;
- }
- m_TrackBinding.color = blendedColor + m_DefaultColor * (1f - totalWeight);
- m_TrackBinding.fontSize = Mathf.RoundToInt (blendedFontSize + m_DefaultFontSize * (1f - totalWeight));
- if (currentInputs != 1 && 1f - totalWeight > greatestWeight)
- {
- m_TrackBinding.text = m_DefaultText;
- }
- }
- public override void OnPlayableDestroy (Playable playable)
- {
- m_FirstFrameHappened = false;
- if (m_TrackBinding == null)
- return;
- m_TrackBinding.color = m_DefaultColor;
- m_TrackBinding.fontSize = m_DefaultFontSize;
- m_TrackBinding.text = m_DefaultText;
- }
- }
|