| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- //-------------------------------------------------------------------------------
- // Copyright (c) 2016 Tag of Joy
- // E-mail: info@tagofjoy.lt
- // To use this, you must have purchased it on the Unity Asset Store (http://u3d.as/n57)
- // Sharing or distribution are not permitted
- //-------------------------------------------------------------------------------
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine.UI;
- using System.Text.RegularExpressions;
- [AddComponentMenu("UI/ToJ Effects/Gradient Color", 1)]
- [RequireComponent(typeof(Text))]
- public class GradientColor : BaseMeshEffect
- {
- public enum GradientMode
- {
- Local = 0,
- GlobalTextArea = 1,
- GlobalFullRect = 2
- }
- public enum GradientDirection
- {
- Vertical,
- Horizontal
- }
- public enum ColorMode
- {
- Override,
- Additive,
- Multiply
- }
- [SerializeField]
- private GradientMode m_GradientMode = GradientMode.Local;
- [SerializeField]
- private GradientDirection m_GradientDirection = GradientDirection.Vertical;
- [SerializeField]
- private ColorMode m_ColorMode = ColorMode.Override;
- [SerializeField]
- public Color m_FirstColor = Color.white;
- [SerializeField]
- public Color m_SecondColor = Color.black;
- [SerializeField]
- private bool m_UseGraphicAlpha = true;
- private List<UIVertex> m_Verts = new List<UIVertex>();
- protected GradientColor () { }
-
- #if UNITY_EDITOR
- protected override void OnValidate()
- {
- gradientMode = m_GradientMode;
- gradientDirection = m_GradientDirection;
- colorMode = m_ColorMode;
- firstColor = m_FirstColor;
- secondColor = m_SecondColor;
- useGraphicAlpha = m_UseGraphicAlpha;
- base.OnValidate();
- }
- #endif
- public GradientMode gradientMode
- {
- get { return m_GradientMode; }
- set
- {
- m_GradientMode = value;
- if (graphic != null)
- graphic.SetVerticesDirty();
- }
- }
- public GradientDirection gradientDirection
- {
- get { return m_GradientDirection; }
- set
- {
- m_GradientDirection = value;
- if (graphic != null)
- graphic.SetVerticesDirty();
- }
- }
- public ColorMode colorMode
- {
- get { return m_ColorMode; }
- set
- {
- m_ColorMode = value;
- if (graphic != null)
- graphic.SetVerticesDirty();
- }
- }
- public Color firstColor
- {
- get { return m_FirstColor; }
- set
- {
- m_FirstColor = value;
- if (graphic != null)
- graphic.SetVerticesDirty();
- }
- }
- public Color secondColor
- {
- get { return m_SecondColor; }
- set
- {
- m_SecondColor = value;
- if (graphic != null)
- graphic.SetVerticesDirty();
- }
- }
- public bool useGraphicAlpha
- {
- get { return m_UseGraphicAlpha; }
- set
- {
- m_UseGraphicAlpha = value;
- if (graphic != null)
- graphic.SetVerticesDirty();
- }
- }
- public override void ModifyMesh (VertexHelper vh)
- {
- if (!IsActive ())
- {
- return;
- }
- vh.GetUIVertexStream(m_Verts);
- if (m_Verts.Count == 0)
- {
- return;
- }
- if ((gradientMode == GradientMode.GlobalTextArea) || (gradientMode == GradientMode.GlobalFullRect))
- {
- Vector2 topLeftPos = Vector2.zero;
- Vector2 bottomRightPos = Vector2.zero;
- if (gradientMode == GradientMode.GlobalFullRect)
- {
- Rect rect = GetComponent<RectTransform>().rect;
- topLeftPos = new Vector2(rect.xMin, rect.yMax);
- bottomRightPos = new Vector2(rect.xMax, rect.yMin);
- }
- else
- {
- topLeftPos = m_Verts[0].position;
- bottomRightPos = m_Verts[m_Verts.Count - 1].position;
- for (int i = 0; i < m_Verts.Count; i++)
- {
- if (m_Verts[i].position.x < topLeftPos.x)
- {
- topLeftPos.x = m_Verts[i].position.x;
- }
- if (m_Verts[i].position.y > topLeftPos.y)
- {
- topLeftPos.y = m_Verts[i].position.y;
- }
- if (m_Verts[i].position.x > bottomRightPos.x)
- {
- bottomRightPos.x = m_Verts[i].position.x;
- }
- if (m_Verts[i].position.y < bottomRightPos.y)
- {
- bottomRightPos.y = m_Verts[i].position.y;
- }
- }
- }
- float overallHeight = topLeftPos.y - bottomRightPos.y;
- float overallWidth = bottomRightPos.x - topLeftPos.x;
- for (int i = 0; i < m_Verts.Count; i++)
- {
- UIVertex uiVertex = m_Verts[i];
- if (gradientDirection == GradientDirection.Vertical)
- {
- Color newColor = Color.Lerp(firstColor, secondColor, (topLeftPos.y - uiVertex.position.y) / overallHeight);
- uiVertex.color = CalculateColor(uiVertex.color, newColor, colorMode);
- }
- else
- {
- Color newColor = Color.Lerp(firstColor, secondColor, (uiVertex.position.x - topLeftPos.x) / overallWidth);
- uiVertex.color = CalculateColor(uiVertex.color, newColor, colorMode);
- }
- if (useGraphicAlpha)
- {
- uiVertex.color.a = (byte)((uiVertex.color.a * m_Verts[i].color.a) / 255);
- }
- m_Verts[i] = uiVertex;
- }
- }
- else
- {
- for (int i = 0; i < m_Verts.Count; i++)
- {
- UIVertex uiVertex = m_Verts[i];
- if (gradientDirection == GradientDirection.Vertical)
- {
- if ((i % 6 == 0) || (i % 6 == 1) || (i % 6 == 5))
- {
- Color newColor = firstColor;
- uiVertex.color = CalculateColor(uiVertex.color, newColor, colorMode);
- }
- else
- {
- Color newColor = secondColor;
- uiVertex.color = CalculateColor(uiVertex.color, newColor, colorMode);
- }
- }
- else
- {
- if ((i % 6 == 0) || (i % 6 == 4) || (i % 6 == 5))
- {
- Color newColor = firstColor;
- uiVertex.color = CalculateColor(uiVertex.color, newColor, colorMode);
- }
- else
- {
- Color newColor = secondColor;
- uiVertex.color = CalculateColor(uiVertex.color, newColor, colorMode);
- }
- }
- if (useGraphicAlpha)
- {
- uiVertex.color.a = (byte)((uiVertex.color.a * m_Verts[i].color.a) / 255);
- }
- m_Verts[i] = uiVertex;
- }
- }
- vh.Clear();
- vh.AddUIVertexTriangleStream(m_Verts);
- }
- private Color CalculateColor (Color initialColor, Color newColor, ColorMode colorMode)
- {
- if (colorMode == ColorMode.Override)
- {
- return newColor;
- }
- else if (colorMode == ColorMode.Additive)
- {
- return initialColor + newColor;
- }
- else if (colorMode == ColorMode.Multiply)
- {
- return initialColor * newColor;
- }
- return newColor;
- }
- }
|