TextMeshProUGUIVertexGradientMixer.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using TMPro;
  2. using UnityEngine;
  3. namespace UnityUIPlayables
  4. {
  5. public class TextMeshProUGUIVertexGradientMixer
  6. {
  7. private Color _blendedBottomLeft;
  8. private Color _blendedBottomRight;
  9. private Color _blendedTopLeft;
  10. private Color _blendedTopRight;
  11. private float _totalWeight;
  12. public void SetupFrame()
  13. {
  14. _blendedTopLeft = Color.clear;
  15. _blendedTopRight = Color.clear;
  16. _blendedBottomLeft = Color.clear;
  17. _blendedBottomRight = Color.clear;
  18. _totalWeight = 0.0f;
  19. }
  20. public void Blend(VertexGradient startValue, VertexGradient endValue, float inputWeight, float progress)
  21. {
  22. _blendedTopLeft = Color.Lerp(startValue.topLeft, endValue.topLeft, progress) * inputWeight;
  23. _blendedTopRight = Color.Lerp(startValue.topRight, endValue.topRight, progress) * inputWeight;
  24. _blendedBottomLeft = Color.Lerp(startValue.bottomLeft, endValue.bottomLeft, progress) * inputWeight;
  25. _blendedBottomRight = Color.Lerp(startValue.bottomRight, endValue.bottomRight, progress) * inputWeight;
  26. _totalWeight += inputWeight;
  27. }
  28. public void ApplyFrame(TextMeshProUGUI binding)
  29. {
  30. if (_totalWeight == 0)
  31. {
  32. return;
  33. }
  34. _blendedTopLeft += binding.colorGradient.topLeft * (1f - _totalWeight);
  35. _blendedTopRight += binding.colorGradient.topRight * (1f - _totalWeight);
  36. _blendedBottomLeft += binding.colorGradient.bottomLeft * (1f - _totalWeight);
  37. _blendedBottomRight += binding.colorGradient.bottomRight * (1f - _totalWeight);
  38. binding.colorGradientPreset = null;
  39. binding.colorGradient =
  40. new VertexGradient(_blendedTopLeft, _blendedTopRight, _blendedBottomLeft, _blendedBottomRight);
  41. }
  42. }
  43. }