浏览一下 GitHub ,找了找UGUI开源的东西 https://github.com/WestHillApps/uGUI-Effect-Tool
发现了 uGuiEffectTool (包括Blend【意义不大】 和 渐变)
这个是原始图片
Blend的效果:
Blend的代码:
using UnityEngine; using System.Collections.Generic; using UnityEngine.UI; namespace UiEffect { [AddComponentMenu ("UI/Effects/Blend Color")] [RequireComponent (typeof (Graphic))] public class BlendColor : BaseVertexEffect { public enum BLEND_MODE { Multiply, Additive, Subtractive, Override, } public BLEND_MODE blendMode = BLEND_MODE.Multiply; public Color color = Color.grey; Graphic graphic; public override void ModifyVertices (List<UIVertex> vList) { if (IsActive () == false || vList == null || vList.Count == 0) { return; } UIVertex tempVertex = vList[0]; for (int i = 0; i < vList.Count; i++) { tempVertex = vList[i]; byte orgAlpha = tempVertex.color.a; switch (blendMode) { case BLEND_MODE.Multiply: tempVertex.color *= color; break; case BLEND_MODE.Additive: tempVertex.color += color; break; case BLEND_MODE.Subtractive: tempVertex.color -= color; break; case BLEND_MODE.Override: tempVertex.color = color; break; } tempVertex.color.a = orgAlpha; vList[i] = tempVertex; } } /// <summary> /// Refresh Blend Color on playing. /// </summary> public void Refresh () { if (graphic == null) { graphic = GetComponent<Graphic> (); } if (graphic != null) { graphic.SetVerticesDirty (); } } } }
Gradient效果:
Gradient代码:
using UnityEngine; using System.Collections.Generic; using UnityEngine.UI; namespace UiEffect { [AddComponentMenu ("UI/Effects/Gradient Color")] [RequireComponent (typeof (Graphic))] public class GradientColor : BaseVertexEffect { public enum DIRECTION { Vertical, Horizontal, Both, } public DIRECTION direction = DIRECTION.Both; public Color colorTop = Color.white; public Color colorBottom = Color.black; public Color colorLeft = Color.red; public Color colorRight = Color.blue; Graphic graphic; public override void ModifyVertices (List<UIVertex> vList) { if (IsActive () == false || vList == null || vList.Count == 0) { return; } float topX = 0f, topY = 0f, bottomX = 0f, bottomY = 0f; foreach (var vertex in vList) { topX = Mathf.Max (topX, vertex.position.x); topY = Mathf.Max (topY, vertex.position.y); bottomX = Mathf.Min (bottomX, vertex.position.x); bottomY = Mathf.Min (bottomY, vertex.position.y); } float width = topX - bottomX; float height = topY - bottomY; UIVertex tempVertex = vList[0]; for (int i = 0; i < vList.Count; i++) { tempVertex = vList[i]; byte orgAlpha = tempVertex.color.a; Color colorOrg = tempVertex.color; Color colorV = Color.Lerp (colorBottom, colorTop, (tempVertex.position.y - bottomY) / height); Color colorH = Color.Lerp (colorLeft, colorRight, (tempVertex.position.x - bottomX) / width); switch (direction) { case DIRECTION.Both: tempVertex.color = colorOrg * colorV * colorH; break; case DIRECTION.Vertical: tempVertex.color = colorOrg * colorV; break; case DIRECTION.Horizontal: tempVertex.color = colorOrg * colorH; break; } tempVertex.color.a = orgAlpha; vList[i] = tempVertex; } } /// <summary> /// Refresh Gradient Color on playing. /// </summary> public void Refresh () { if (graphic == null) { graphic = GetComponent<Graphic> (); } if (graphic != null) { graphic.SetVerticesDirty (); } } } }