Advertisement
Guest User

Unity3D Gradient Effect

a guest
Jan 23rd, 2015
991
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 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 : BaseVertexEffect
  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 ModifyVertices(List<UIVertex> _vertexList)
  27. {
  28. if (!IsActive())
  29. return;
  30.  
  31. int nCount = _vertexList.Count;
  32. switch (GradientType)
  33. {
  34. case Type.Vertical:
  35. {
  36. float fBottomY = _vertexList[0].position.y;
  37. float fTopY = _vertexList[0].position.y;
  38. float fYPos = 0f;
  39.  
  40. for (int i = nCount - 1; i >= 1; --i)
  41. {
  42. fYPos = _vertexList[i].position.y;
  43. if (fYPos > fTopY)
  44. fTopY = fYPos;
  45. else if (fYPos < fBottomY)
  46. fBottomY = fYPos;
  47. }
  48.  
  49. float fUIElementHeight = 1f / (fTopY - fBottomY);
  50. for (int i = nCount - 1; i >= 0; --i)
  51. {
  52. UIVertex uiVertex = _vertexList[i];
  53. uiVertex.color = Color32.Lerp(EndColor, StartColor, (uiVertex.position.y - fBottomY) * fUIElementHeight - Offset);
  54. _vertexList[i] = uiVertex;
  55. }
  56. }
  57. break;
  58. case Type.Horizontal:
  59. {
  60. float fLeftX = _vertexList[0].position.x;
  61. float fRightX = _vertexList[0].position.x;
  62. float fXPos = 0f;
  63.  
  64. for (int i = nCount - 1; i >= 1; --i)
  65. {
  66. fXPos = _vertexList[i].position.x;
  67. if (fXPos > fRightX)
  68. fRightX = fXPos;
  69. else if (fXPos < fLeftX)
  70. fLeftX = fXPos;
  71. }
  72.  
  73. float fUIElementWidth = 1f / (fRightX - fLeftX);
  74. for (int i = nCount - 1; i >= 0; --i)
  75. {
  76. UIVertex uiVertex = _vertexList[i];
  77. uiVertex.color = Color32.Lerp(StartColor, EndColor, (uiVertex.position.x - fLeftX) * fUIElementWidth - Offset);
  78. _vertexList[i] = uiVertex;
  79. }
  80. }
  81. break;
  82. default: break;
  83. }
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement