当前位置:   article > 正文

Unity 为 UGUI Text 文字添加颜色渐变效果_ugui文本颜色渐变

ugui文本颜色渐变
  1. [AddComponentMenu("UI/Effects/Gradient")]
  2. public class TextGradient : BaseMeshEffect
  3. {
  4. [SerializeField] Color32 topColor = Color.white;
  5. [SerializeField] Color32 bottomColor = Color.black;
  6. public override void ModifyMesh(VertexHelper vh)
  7. {
  8. if(vh.currentVertCount <= 0) return;
  9. //取得模型
  10. float bottomY = -1;
  11. float topY = -1;
  12. for (int i = 0; i < vh.currentVertCount; i++)
  13. {
  14. UIVertex v = new UIVertex();
  15. vh.PopulateUIVertex(ref v, i);
  16. if (bottomY == -1)
  17. bottomY = v.position.y;
  18. if (topY == -1)
  19. topY = v.position.y;
  20. if (v.position.y > topY)
  21. topY = v.position.y;
  22. else if (v.position.y < bottomY)
  23. bottomY = v.position.y;
  24. }
  25. //混色
  26. UIVertex vx = new UIVertex();
  27. vh.PopulateUIVertex(ref vx, 0);
  28. Color32 topColor = new Color32((byte)((this.topColor.r + vx.color.r) / 2), (byte)((this.topColor.g + vx.color.g) / 2), (byte)((this.topColor.b + vx.color.b) / 2), 255);
  29. Color32 bottomColor = new Color32((byte)((this.bottomColor.r + vx.color.r) / 2), (byte)((this.bottomColor.g + vx.color.g) / 2), (byte)((this.bottomColor.b + vx.color.b) / 2), 255);
  30. //上色
  31. float uiElementHeight = topY - bottomY;
  32. for (int i = 0; i < vh.currentVertCount; i++)
  33. {
  34. UIVertex v = new UIVertex();
  35. vh.PopulateUIVertex(ref v, i);
  36. v.color = Color32.Lerp(bottomColor, topColor, (v.position.y - bottomY) / uiElementHeight);
  37. vh.SetUIVertex(v, i);
  38. }
  39. }
  40. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/107222
推荐阅读
相关标签
  

闽ICP备14008679号