TextMeshProUGUISpacingMixer.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using TMPro;
  2. using UnityEngine;
  3. namespace UnityUIPlayables
  4. {
  5. public class TextMeshProUGUISpacingMixer
  6. {
  7. private float _blendedCharacter;
  8. private float _blendedLine;
  9. private float _blendedParagraph;
  10. private float _blendedWord;
  11. private float _totalWeight;
  12. public void SetupFrame()
  13. {
  14. _blendedCharacter = 0.0f;
  15. _blendedLine = 0.0f;
  16. _blendedWord = 0.0f;
  17. _blendedParagraph = 0.0f;
  18. _totalWeight = 0.0f;
  19. }
  20. public void Blend(TextMeshProUGUIAnimationValue.SpacingValue startValue,
  21. TextMeshProUGUIAnimationValue.SpacingValue endValue, float inputWeight, float progress)
  22. {
  23. _blendedCharacter += Mathf.Lerp(startValue.Character, endValue.Character, progress) * inputWeight;
  24. _blendedLine += Mathf.Lerp(startValue.Line, endValue.Line, progress) * inputWeight;
  25. _blendedWord += Mathf.Lerp(startValue.Word, endValue.Word, progress) * inputWeight;
  26. _blendedParagraph += Mathf.Lerp(startValue.Paragraph, endValue.Paragraph, progress) * inputWeight;
  27. _totalWeight += inputWeight;
  28. }
  29. public void ApplyFrame(TextMeshProUGUI binding)
  30. {
  31. if (_totalWeight == 0)
  32. {
  33. return;
  34. }
  35. _blendedCharacter += binding.characterSpacing * (1f - _totalWeight);
  36. _blendedLine += binding.lineSpacing * (1f - _totalWeight);
  37. _blendedWord += binding.wordSpacing * (1f - _totalWeight);
  38. _blendedParagraph += binding.paragraphSpacing * (1f - _totalWeight);
  39. binding.characterSpacing = _blendedCharacter;
  40. binding.lineSpacing = _blendedLine;
  41. binding.wordSpacing = _blendedWord;
  42. binding.paragraphSpacing = _blendedParagraph;
  43. }
  44. }
  45. }