当前位置:   article > 正文

unity UGUI Text 增加渐变色_unity 编辑器扩展 渐变颜色

unity 编辑器扩展 渐变颜色

首先要保证代码在UI字体特效的前面 效果是这样 

如果UI代码放在特效的后面的话就会出现很难看的现象

上代码

  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using UnityEngine.UI;
  4. [AddComponentMenu("UI/Effects/TextGradient")]
  5. [RequireComponent(typeof(Text))]
  6. public class Font_Manager : BaseMeshEffect
  7. {
  8. public Color32 topColor = Color.white;
  9. public Color32 bottomColor = Color.black;
  10. //后面自己添加的控制中心移动属性,有时候看着渐变不顺眼,中心偏离高或者低了,就可以通过这个去调整
  11. [RangeAttribute(0, 1)]
  12. public float center = 0.5f;
  13. public override void ModifyMesh(VertexHelper vh)
  14. {
  15. if (!IsActive())
  16. {
  17. return;
  18. }
  19. var count = vh.currentVertCount;
  20. if (count == 0)
  21. return;
  22. var vertexs = new List<UIVertex>();
  23. for (var i = 0; i < count; i++)
  24. {
  25. var vertex = new UIVertex();
  26. vh.PopulateUIVertex(ref vertex, i);
  27. vertexs.Add(vertex);
  28. }
  29. var topY = vertexs[0].position.y;
  30. var bottomY = vertexs[0].position.y;
  31. for (var i = 1; i < count; i++)
  32. {
  33. var y = vertexs[i].position.y;
  34. if (y > topY)
  35. {
  36. topY = y;
  37. }
  38. else if (y < bottomY)
  39. {
  40. bottomY = y;
  41. }
  42. }
  43. var height = topY - bottomY;
  44. for (var i = 0; i < count; i++)
  45. {
  46. var vertex = vertexs[i];
  47. //使用处理过后的颜色
  48. // var color = Color32.Lerp(bottomColor, topColor, (vertex.position.y - bottomY) / height);
  49. var color = CenterColor(bottomColor, topColor, (vertex.position.y - bottomY) / height);
  50. vertex.color = color;
  51. vh.SetUIVertex(vertex, i);
  52. }
  53. }
  54. //加了一个对颜色处理的函数,主要调整中心的位置
  55. private Color32 CenterColor(Color32 bc, Color32 tc, float time)
  56. {
  57. if (center == 0)
  58. {
  59. return bc;
  60. }
  61. else if (center == 1)
  62. {
  63. return tc;
  64. }
  65. else
  66. {
  67. var centerColor = Color32.Lerp(bottomColor, topColor, 0.5f);
  68. var resultColor = tc;
  69. if (time < center)
  70. {
  71. resultColor = Color32.Lerp(bottomColor, centerColor, time / center);
  72. }
  73. else
  74. {
  75. resultColor = Color32.Lerp(centerColor, topColor, (time - center) / (1 - center));
  76. }
  77. return resultColor;
  78. }
  79. }
  80. }

 

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

闽ICP备14008679号