Gradient.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace ShotSimulator.UI.Extend
  6. {
  7. // 定义渐变方向枚举
  8. [AddComponentMenu("UI/Effects/Gradient")]
  9. public enum Dir
  10. {
  11. Horizontal, // 水平方向
  12. Vertical, // 垂直方向
  13. }
  14. // 自定义梯度效果类,继承自BaseMeshEffect
  15. public class Gradient : BaseMeshEffect
  16. {
  17. [SerializeField] // 序列化字段,可在Inspector中编辑
  18. private Dir dir = Dir.Vertical; // 渐变方向,默认垂直
  19. [SerializeField]
  20. public Color32 color1 = Color.white; // 渐变起始颜色,默认白色
  21. [SerializeField]
  22. public Color32 color2 = Color.white; // 渐变结束颜色,默认白色
  23. [SerializeField]
  24. private float range = 0f; // 渐变范围,控制颜色的过渡区域,默认无范围(完全渐变)
  25. [SerializeField]
  26. private bool isFlip = false; // 是否翻转渐变方向,默认不翻转
  27. // 重写ModifyMesh方法,用于修改UI元素的网格
  28. public override void ModifyMesh(VertexHelper vh)
  29. {
  30. if (!IsActive()) // 如果组件未激活,则不执行后续操作
  31. {
  32. return;
  33. }
  34. int count = vh.currentVertCount; // 获取当前顶点数量
  35. if (count > 0) // 如果有顶点,则进行处理
  36. {
  37. List<UIVertex> vertices = new List<UIVertex>(); // 创建顶点列表
  38. // 遍历所有顶点并添加到列表中
  39. for (int i = 0; i < count; i++)
  40. {
  41. UIVertex uIVertex = new UIVertex();
  42. vh.PopulateUIVertex(ref uIVertex, i); // 填充顶点信息
  43. vertices.Add(uIVertex);
  44. }
  45. // 根据渐变方向调用相应的绘制方法
  46. switch (dir)
  47. {
  48. case Dir.Horizontal:
  49. DrawHorizontal(vh, vertices, count);
  50. break;
  51. case Dir.Vertical:
  52. DrawVertical(vh, vertices, count);
  53. break;
  54. default:
  55. break;
  56. }
  57. }
  58. }
  59. // 绘制垂直方向的渐变
  60. private void DrawVertical(VertexHelper vh, List<UIVertex> vertices, int count)
  61. {
  62. // 初始化顶部和底部Y坐标
  63. float topY = vertices[0].position.y;
  64. float bottomY = vertices[0].position.y;
  65. // 遍历所有顶点,找到最高和最低的Y坐标
  66. for (int i = 0; i < count; i++)
  67. {
  68. float y = vertices[i].position.y;
  69. if (y > topY) topY = y;
  70. else if (y < bottomY) bottomY = y;
  71. }
  72. float height = topY - bottomY; // 计算高度差
  73. // 遍历所有顶点,设置颜色渐变
  74. for (int i = 0; i < count; i++)
  75. {
  76. UIVertex vertex = vertices[i];
  77. Color32 color = Color.white;
  78. // 根据是否翻转,计算当前顶点的颜色
  79. if (isFlip)
  80. {
  81. color = Color32.Lerp(color2, color1, 1 - (vertex.position.y - bottomY) / height * (1f - range));
  82. }
  83. else
  84. {
  85. color = Color32.Lerp(color2, color1, (vertex.position.y - bottomY) / height * (1f - range));
  86. }
  87. vertex.color = color; // 设置顶点颜色
  88. vh.SetUIVertex(vertex, i); // 更新网格中的顶点
  89. }
  90. }
  91. // 绘制水平方向的渐变
  92. private void DrawHorizontal(VertexHelper vh, List<UIVertex> vertices, int count)
  93. {
  94. // 注意:这里应该是找到最左和最右的X坐标,注释中存在笔误
  95. float leftX = vertices[0].position.x;
  96. float rightX = vertices[0].position.x;
  97. // 遍历所有顶点,找到最左和最右的X坐标
  98. for (int i = 0; i < count; i++)
  99. {
  100. float x = vertices[i].position.x;
  101. if (x > rightX) rightX = x;
  102. else if (x < leftX) leftX = x;
  103. }
  104. float width = rightX - leftX; // 计算宽度差
  105. // 遍历所有顶点,设置颜色渐变
  106. for (int i = 0; i < count; i++)
  107. {
  108. UIVertex vertex = vertices[i];
  109. Color32 color = Color.white;
  110. // 根据是否翻转,计算当前顶点的颜色
  111. if (isFlip)
  112. {
  113. color = Color32.Lerp(color2, color1, 1 - (vertex.position.x - leftX) / width * (1f - range));
  114. }
  115. else
  116. {
  117. color = Color32.Lerp(color2, color1, (vertex.position.x - leftX) / width * (1f - range));
  118. }
  119. vertex.color = color; // 设置顶点颜色
  120. vh.SetUIVertex(vertex, i); // 更新网格中的顶点
  121. }
  122. }
  123. }
  124. }