Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEngine.Playables;
- [Serializable]
- public class TMPAppenderBehaviour : PlayableBehaviour
- {
- public string text;
- }
- [Serializable]
- public class TMPAppenderClip : PlayableAsset, ITimelineClipAsset,IPropertyPreview
- {
- public TMPAppenderBehaviour template = new TMPAppenderBehaviour ();
- public ClipCaps clipCaps
- {
- get { return ClipCaps.Blending; }
- }
- public override Playable CreatePlayable (PlayableGraph graph, GameObject owner)
- {
- var playable = ScriptPlayable<TMPAppenderBehaviour>.Create (graph, template);
- return playable;
- }
- public void GatherProperties(PlayableDirector director, IPropertyCollector driver)
- {
- driver.AddFromName<TextMeshProUGUI>("m_Text");
- }
- }
- [TrackClipType(typeof(TMPAppenderClip))]
- [TrackBindingType(typeof(TextMeshProUGUI))]
- public class TMPAppenderTrack : TrackAsset
- {
- public override Playable CreateTrackMixer(PlayableGraph graph, GameObject go, int inputCount)
- {
- return ScriptPlayable<TMPAppenderMixerBehaviour>.Create (graph, inputCount);
- }
- // Please note this assumes only one component of type TextMeshProUGUI on the same gameobject.
- public override void GatherProperties (PlayableDirector director, IPropertyCollector driver)
- {
- #if UNITY_EDITOR
- TextMeshProUGUI trackBinding = director.GetGenericBinding(this) as TextMeshProUGUI;
- if (trackBinding == null)
- return;
- // These field names are procedurally generated estimations based on the associated property names.
- // If any of the names are incorrect you will get a DrivenPropertyManager error saying it has failed to register the name.
- // In this case you will need to find the correct backing field name.
- // The suggested way of finding the field name is to:
- // 1. Make sure your scene is serialized to text.
- // 2. Search the text for the track binding component type.
- // 3. Look through the field names until you see one that looks correct.
- driver.AddFromName<TextMeshProUGUI>(trackBinding.gameObject, "m_Text");
- #endif
- base.GatherProperties (director, driver);
- }
- }
- public class TMPAppenderMixerBehaviour : PlayableBehaviour
- {
- string m_DefaultText;
- string m_AssignedText;
- TextMeshProUGUI m_TrackBinding;
- public override void ProcessFrame(Playable playable, FrameData info, object playerData)
- {
- m_TrackBinding = playerData as TextMeshProUGUI;
- if (m_TrackBinding == null)
- return;
- if (m_TrackBinding.text != m_AssignedText)
- m_DefaultText = m_TrackBinding.text;
- int inputCount = playable.GetInputCount ();
- float totalWeight = 0f;
- float greatestWeight = 0f;
- int currentInputs = 0;
- for (int i = 0; i < inputCount; i++)
- {
- float inputWeight = playable.GetInputWeight(i);
- ScriptPlayable<TMPAppenderBehaviour> inputPlayable = (ScriptPlayable<TMPAppenderBehaviour>)playable.GetInput(i);
- TMPAppenderBehaviour input = inputPlayable.GetBehaviour ();
- totalWeight += inputWeight;
- if (inputWeight > greatestWeight)
- {
- m_AssignedText = input.text;
- m_TrackBinding.text = m_AssignedText;
- greatestWeight = inputWeight;
- }
- if (!Mathf.Approximately (inputWeight, 0f))
- currentInputs++;
- }
- if (currentInputs != 1 && 1f - totalWeight > greatestWeight)
- {
- m_TrackBinding.text = m_DefaultText;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement