UIGradient.cs 888 B

12345678910111213141516171819
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. namespace DevilLink2.UI
  4. {
  5. [RequireComponent(typeof(Graphic))]
  6. public sealed class UIGradient : BaseMeshEffect
  7. {
  8. public Color Top = Color.white, Bottom = Color.white;
  9. public override void ModifyMesh(VertexHelper mesh)
  10. {
  11. if (!IsActive() || mesh.currentVertCount == 0) return;
  12. var vertex = new UIVertex(); float min = float.MaxValue, max = float.MinValue;
  13. for (int i = 0; i < mesh.currentVertCount; i++) { mesh.PopulateUIVertex(ref vertex, i); min = Mathf.Min(min, vertex.position.y); max = Mathf.Max(max, vertex.position.y); }
  14. for (int i = 0; i < mesh.currentVertCount; i++)
  15. { mesh.PopulateUIVertex(ref vertex, i); vertex.color = Color.Lerp(Bottom, Top, Mathf.InverseLerp(min, max, vertex.position.y)); mesh.SetUIVertex(vertex, i); }
  16. }
  17. }
  18. }