文章转载出处1: http://www.xuanyusong.com/archives/3471
2: http://lib.csdn.net/snippet/unity3d/39429
/
出处1:
如下图所示,用法和UGUI自带的outline和shadow一样,可以同时使用。
代码在这里了。我加了个判断,它原来的有越界的隐患。
usingUnityEngine;
usingSystem.Collections; usingSystem.Collections.Generic; usingUnityEngine.UI; [AddComponentMenu("UI/Effects/Gradient")] publicclassGradient:BaseVertexEffect{ [SerializeField] privateColor32topColor=Color.white; [SerializeField] privateColor32bottomColor=Color.black; publicoverridevoidModifyVertices(List<UIVertex>vertexList){ if(!IsActive()){ return; } intcount=vertexList.Count; if(count>0){ floatbottomY=vertexList[0].position.y; floattopY=vertexList[0].position.y; for(inti=1;i<count;i++){ floaty=vertexList[i].position.y; if(y>topY){ topY=y; } elseif(y<bottomY){ bottomY=y; } } floatuiElementHeight=topY-bottomY; for(inti=0;i<count;i++){ UIVertexuiVertex=vertexList[i]; uiVertex.color=Color32.Lerp(bottomColor,topColor,(uiVertex.position.y-bottomY)/uiElementHeight); vertexList[i]=uiVertex; } } } }
如果你的项目升级到了5.2 请使用下面的代码,感谢代码的提供者。@獨立遊戲開發熊
usingUnityEngine; usingSystem.Collections; usingSystem.Collections.Generic; usingUnityEngine.UI; [AddComponentMenu("UI/Effects/Gradient")] publicclassGradient: BaseMeshEffect { [SerializeField] privateColor32 topColor=Color.white; [SerializeField] privateColor32 bottomColor=Color.black; publicoverridevoidModifyMesh(Meshmesh) { if(!IsActive()){ return; } Vector3[]vertexList=mesh.vertices; intcount=mesh.vertexCount; if(count>0)
{ floatbottomY=vertexList[0].y; floattopY=vertexList[0].y; for(inti=1;i<count;i++){ floaty=vertexList[i].y; if(y>topY){ topY=y; }elseif(y<bottomY){ bottomY=y; } } List<Color32>colors=newList<Color32>(); floatuiElementHeight=topY-bottomY; for(inti=0;i<count;i++){ colors.Add(Color32.Lerp(bottomColor,topColor,(vertexList[i].y-bottomY)/uiElementHeight)); } mesh.SetColors(colors); } } }
本文固定链接: http://www.xuanyusong.com/archives/3471 转载请注明: 雨松MOMO 2015年05月08日 于 雨松MOMO程序研究院 发表出处2:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; [AddComponentMenu("UI/Effects/Gradient")] public class Gradient : BaseMeshEffect { [SerializeField] private Color32 topColor = Color.white; [SerializeField] private Color32 bottomColor = Color.black; public override void ModifyMesh(VertexHelper vh) { if(!IsActive()) { return; } varvertexList = newList<UIVertex>(); vh.GetUIVertexStream(vertexList); int count = vertexList.Count; ApplyGradient(vertexList, 0, count); vh.Clear(); vh.AddUIVertexTriangleStream(vertexList); } private void ApplyGradient(List<UIVertex> vertexList, int start, int end) { float bottomY = vertexList[0].position.y; float topY = vertexList[0].position.y; for(int i = start; i < end; ++i) { float y = vertexList[i].position.y; if(y > topY) { topY = y; } elseif (y < bottomY) { bottomY = y; } } float uiElementHeight = topY - bottomY; for(int i = start; i < end; ++i) { UIVertex uiVertex = vertexList[i]; uiVertex.color = Color32.Lerp(bottomColor, topColor, (uiVertex.position.y - bottomY)/uiElementHeight); vertexList[i] = uiVertex; } } }