Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. using System;
  2. using UnityEngine.Playables;
  3.  
  4. [Serializable]
  5. public class TMPAppenderBehaviour : PlayableBehaviour
  6. {
  7. public string text;
  8. }
  9.  
  10. [Serializable]
  11. public class TMPAppenderClip : PlayableAsset, ITimelineClipAsset,IPropertyPreview
  12. {
  13. public TMPAppenderBehaviour template = new TMPAppenderBehaviour ();
  14.  
  15. public ClipCaps clipCaps
  16. {
  17. get { return ClipCaps.Blending; }
  18. }
  19.  
  20. public override Playable CreatePlayable (PlayableGraph graph, GameObject owner)
  21. {
  22. var playable = ScriptPlayable<TMPAppenderBehaviour>.Create (graph, template);
  23. return playable;
  24. }
  25.  
  26. public void GatherProperties(PlayableDirector director, IPropertyCollector driver)
  27. {
  28. driver.AddFromName<TextMeshProUGUI>("m_Text");
  29. }
  30. }
  31.  
  32.  
  33. [TrackClipType(typeof(TMPAppenderClip))]
  34. [TrackBindingType(typeof(TextMeshProUGUI))]
  35. public class TMPAppenderTrack : TrackAsset
  36. {
  37. public override Playable CreateTrackMixer(PlayableGraph graph, GameObject go, int inputCount)
  38. {
  39. return ScriptPlayable<TMPAppenderMixerBehaviour>.Create (graph, inputCount);
  40. }
  41.  
  42. // Please note this assumes only one component of type TextMeshProUGUI on the same gameobject.
  43. public override void GatherProperties (PlayableDirector director, IPropertyCollector driver)
  44. {
  45. #if UNITY_EDITOR
  46. TextMeshProUGUI trackBinding = director.GetGenericBinding(this) as TextMeshProUGUI;
  47. if (trackBinding == null)
  48. return;
  49.  
  50. // These field names are procedurally generated estimations based on the associated property names.
  51. // If any of the names are incorrect you will get a DrivenPropertyManager error saying it has failed to register the name.
  52. // In this case you will need to find the correct backing field name.
  53. // The suggested way of finding the field name is to:
  54. // 1. Make sure your scene is serialized to text.
  55. // 2. Search the text for the track binding component type.
  56. // 3. Look through the field names until you see one that looks correct.
  57. driver.AddFromName<TextMeshProUGUI>(trackBinding.gameObject, "m_Text");
  58. #endif
  59. base.GatherProperties (director, driver);
  60. }
  61. }
  62.  
  63.  
  64. public class TMPAppenderMixerBehaviour : PlayableBehaviour
  65. {
  66. string m_DefaultText;
  67.  
  68. string m_AssignedText;
  69.  
  70. TextMeshProUGUI m_TrackBinding;
  71.  
  72. public override void ProcessFrame(Playable playable, FrameData info, object playerData)
  73. {
  74. m_TrackBinding = playerData as TextMeshProUGUI;
  75.  
  76. if (m_TrackBinding == null)
  77. return;
  78.  
  79. if (m_TrackBinding.text != m_AssignedText)
  80. m_DefaultText = m_TrackBinding.text;
  81.  
  82. int inputCount = playable.GetInputCount ();
  83.  
  84. float totalWeight = 0f;
  85. float greatestWeight = 0f;
  86. int currentInputs = 0;
  87.  
  88. for (int i = 0; i < inputCount; i++)
  89. {
  90. float inputWeight = playable.GetInputWeight(i);
  91. ScriptPlayable<TMPAppenderBehaviour> inputPlayable = (ScriptPlayable<TMPAppenderBehaviour>)playable.GetInput(i);
  92. TMPAppenderBehaviour input = inputPlayable.GetBehaviour ();
  93.  
  94. totalWeight += inputWeight;
  95.  
  96. if (inputWeight > greatestWeight)
  97. {
  98. m_AssignedText = input.text;
  99. m_TrackBinding.text = m_AssignedText;
  100. greatestWeight = inputWeight;
  101. }
  102.  
  103. if (!Mathf.Approximately (inputWeight, 0f))
  104. currentInputs++;
  105. }
  106.  
  107. if (currentInputs != 1 && 1f - totalWeight > greatestWeight)
  108. {
  109. m_TrackBinding.text = m_DefaultText;
  110. }
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement