Advertisement
GibTreaty

Gradient.cs

Apr 30th, 2016
625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.48 KB | None | 0 0
  1. using System.Collections.Generic;
  2.  
  3. /// <summary>
  4. /// Modified Gradient effect script from http://answers.unity3d.com/questions/1086415/gradient-text-in-unity-522-basevertexeffect-is-obs.html
  5. /// -Uses Unity's Gradient class to define the color
  6. /// -Offset is now limited to -1,1
  7. /// -Multiple color blend modes
  8. ///
  9. /// Remember that the colors are applied per-vertex so if you have multiple points on your gradient where the color changes and there aren't enough vertices, you won't see all of the colors.
  10. /// </summary>
  11. namespace UnityEngine.UI {
  12.     [AddComponentMenu("UI/Effects/Gradient")]
  13.     public class Gradient : BaseMeshEffect {
  14.         [SerializeField]
  15.         Type _gradientType;
  16.  
  17.         [SerializeField]
  18.         Blend _blendMode = Blend.Multiply;
  19.  
  20.         [SerializeField]
  21.         [Range(-1, 1)]
  22.         float _offset = 0f;
  23.  
  24.         [SerializeField]
  25.         UnityEngine.Gradient _effectGradient = new UnityEngine.Gradient() { colorKeys = new GradientColorKey[] { new GradientColorKey(Color.black, 0), new GradientColorKey(Color.white, 1) } };
  26.  
  27.         #region Properties
  28.         public Blend BlendMode {
  29.             get { return _blendMode; }
  30.             set { _blendMode = value; }
  31.         }
  32.  
  33.         public UnityEngine.Gradient EffectGradient {
  34.             get { return _effectGradient; }
  35.             set { _effectGradient = value; }
  36.         }
  37.  
  38.         public Type GradientType {
  39.             get { return _gradientType; }
  40.             set { _gradientType = value; }
  41.         }
  42.  
  43.         public float Offset {
  44.             get { return _offset; }
  45.             set { _offset = value; }
  46.         }
  47.         #endregion
  48.  
  49.         public override void ModifyMesh(VertexHelper helper) {
  50.             if(!IsActive() || helper.currentVertCount == 0)
  51.                 return;
  52.  
  53.             List<UIVertex> _vertexList = new List<UIVertex>();
  54.  
  55.             helper.GetUIVertexStream(_vertexList);
  56.  
  57.             int nCount = _vertexList.Count;
  58.             switch(GradientType) {
  59.                 case Type.Horizontal: {
  60.                         float left = _vertexList[0].position.x;
  61.                         float right = _vertexList[0].position.x;
  62.                         float x = 0f;
  63.  
  64.                         for(int i = nCount - 1; i >= 1; --i) {
  65.                             x = _vertexList[i].position.x;
  66.  
  67.                             if(x > right) right = x;
  68.                             else if(x < left) left = x;
  69.                         }
  70.  
  71.                         float width = 1f / (right - left);
  72.                         UIVertex vertex = new UIVertex();
  73.  
  74.                         for(int i = 0; i < helper.currentVertCount; i++) {
  75.                             helper.PopulateUIVertex(ref vertex, i);
  76.  
  77.                             vertex.color = BlendColor(vertex.color, EffectGradient.Evaluate((vertex.position.x - left) * width - Offset));
  78.  
  79.                             helper.SetUIVertex(vertex, i);
  80.                         }
  81.                     }
  82.                     break;
  83.  
  84.                 case Type.Vertical: {
  85.                         float bottom = _vertexList[0].position.y;
  86.                         float top = _vertexList[0].position.y;
  87.                         float y = 0f;
  88.  
  89.                         for(int i = nCount - 1; i >= 1; --i) {
  90.                             y = _vertexList[i].position.y;
  91.  
  92.                             if(y > top) top = y;
  93.                             else if(y < bottom) bottom = y;
  94.                         }
  95.  
  96.                         float height = 1f / (top - bottom);
  97.                         UIVertex vertex = new UIVertex();
  98.  
  99.                         for(int i = 0; i < helper.currentVertCount; i++) {
  100.                             helper.PopulateUIVertex(ref vertex, i);
  101.  
  102.                             vertex.color = BlendColor(vertex.color, EffectGradient.Evaluate((vertex.position.y - bottom) * height - Offset));
  103.  
  104.                             helper.SetUIVertex(vertex, i);
  105.                         }
  106.                     }
  107.                     break;
  108.             }
  109.         }
  110.  
  111.         Color BlendColor(Color colorA, Color colorB) {
  112.             switch(BlendMode) {
  113.                 default: return colorB;
  114.                 case Blend.Add: return colorA + colorB;
  115.                 case Blend.Multiply: return colorA * colorB;
  116.             }
  117.         }
  118.  
  119.         public enum Type {
  120.             Horizontal,
  121.             Vertical
  122.         }
  123.  
  124.         public enum Blend {
  125.             Override,
  126.             Add,
  127.             Multiply
  128.         }
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement