UGUI-文字颜色渐变效果

作者:追风剑情 发布于:2019-7-10 15:11 分类:Unity3d

示例

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. /// <summary>
  6. /// 文本颜色渐变效果
  7. /// </summary>
  8. public class GradientTextEffect : BaseMeshEffect
  9. {
  10. public Color32 topColor = Color.white;
  11. public Color32 bottomColor = Color.yellow;
  12.  
  13. public override void ModifyMesh(VertexHelper vh)
  14. {
  15. if (!IsActive())
  16. return;
  17.  
  18. if (vh.currentVertCount == 0)
  19. return;
  20.  
  21. //ListPool类参见 http://www.devacg.com/?post=1026
  22. var output = ListPool<UIVertex>.Get();
  23. vh.GetUIVertexStream(output);
  24.  
  25. ApplyGradient(output);
  26. vh.Clear();
  27. vh.AddUIVertexTriangleStream(output);
  28. ListPool<UIVertex>.Release(output);
  29. }
  30.  
  31. private void ApplyGradient(List<UIVertex> verts)
  32. {
  33. float topY = 0;
  34. float bottomY = 0;
  35. for (int i = 0; i < verts.Count; i++)
  36. {
  37. float y = verts[i].position.y;
  38. if (y > topY)
  39. topY = y;
  40. else if (y < bottomY)
  41. bottomY = y;
  42. }
  43.  
  44. //计算出文字高度
  45. float height = topY - bottomY;
  46.  
  47. for (int i=0; i<verts.Count; i++)
  48. {
  49. UIVertex v = verts[i];
  50. Color32 color = Color32.Lerp(bottomColor, topColor, (v.position.y - bottomY)/height);
  51. v.color = color;
  52. verts[i] = v;
  53. }
  54. }
  55. }

效果

3333333.png

1111.png

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号