Advertisement
Guest User

Unity3D 5.2 Gradient Effect

a guest
Sep 10th, 2015
829
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.35 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine.UI;
  5.  
  6. [AddComponentMenu("UI/Effects/Gradient")]
  7. public class Gradient : BaseMeshEffect
  8. {
  9.     public enum Type
  10.     {
  11.         Vertical,
  12.         Horizontal
  13.     }
  14.     [SerializeField]
  15.     public Type GradientType = Type.Vertical;
  16.  
  17.     [SerializeField]
  18.     [Range(-1.5f, 1.5f)]
  19.     public float Offset = 0f;
  20.  
  21.     [SerializeField]
  22.     private Color32 StartColor = Color.white;
  23.     [SerializeField]
  24.     private Color32 EndColor = Color.black;
  25.  
  26.     public override void ModifyMesh(Mesh mesh)
  27.     {
  28.         if (!this.IsActive())
  29.             return;
  30.  
  31.         List<UIVertex> list = new List<UIVertex>();
  32.         using (VertexHelper vertexHelper = new VertexHelper(mesh))
  33.         {
  34.             vertexHelper.GetUIVertexStream(list);
  35.         }
  36.  
  37.         ModifyVertices(list);
  38.  
  39.         using (VertexHelper vertexHelper2 = new VertexHelper())
  40.         {
  41.             vertexHelper2.AddUIVertexTriangleStream(list);
  42.             vertexHelper2.FillMesh(mesh);
  43.         }
  44.     }
  45.  
  46.     public void ModifyVertices(List<UIVertex> _vertexList)
  47.     {
  48.         if (!IsActive())
  49.             return;
  50.  
  51.         int nCount = _vertexList.Count;
  52.         switch (GradientType)
  53.         {
  54.             case Type.Vertical:
  55.                 {
  56.                     float fBottomY = _vertexList[0].position.y;
  57.                     float fTopY = _vertexList[0].position.y;
  58.                     float fYPos = 0f;
  59.  
  60.                     for (int i = nCount - 1; i >= 1; --i)
  61.                     {
  62.                         fYPos = _vertexList[i].position.y;
  63.                         if (fYPos > fTopY)
  64.                             fTopY = fYPos;
  65.                         else if (fYPos < fBottomY)
  66.                             fBottomY = fYPos;
  67.                     }
  68.  
  69.                     float fUIElementHeight = 1f / (fTopY - fBottomY);
  70.                     for (int i = nCount - 1; i >= 0; --i)
  71.                     {
  72.                         UIVertex uiVertex = _vertexList[i];
  73.                         uiVertex.color = Color32.Lerp(EndColor, StartColor, (uiVertex.position.y - fBottomY) * fUIElementHeight - Offset);
  74.                         _vertexList[i] = uiVertex;
  75.                     }
  76.                 }
  77.                 break;
  78.             case Type.Horizontal:
  79.                 {
  80.                     float fLeftX = _vertexList[0].position.x;
  81.                     float fRightX = _vertexList[0].position.x;
  82.                     float fXPos = 0f;
  83.  
  84.                     for (int i = nCount - 1; i >= 1; --i)
  85.                     {
  86.                         fXPos = _vertexList[i].position.x;
  87.                         if (fXPos > fRightX)
  88.                             fRightX = fXPos;
  89.                         else if (fXPos < fLeftX)
  90.                             fLeftX = fXPos;
  91.                     }
  92.  
  93.                     float fUIElementWidth = 1f / (fRightX - fLeftX);
  94.                     for (int i = nCount - 1; i >= 0; --i)
  95.                     {
  96.                         UIVertex uiVertex = _vertexList[i];
  97.                         uiVertex.color = Color32.Lerp(StartColor, EndColor, (uiVertex.position.x - fLeftX) * fUIElementWidth - Offset);
  98.                         _vertexList[i] = uiVertex;
  99.                     }
  100.                 }
  101.                 break;
  102.             default: break;
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement