今日在做项目中发现,很多时候需要对文字多特殊处理,渐变就是最常用的。
特别是文字特别多,还有动态的时候,就不能只靠图了,否则包的大小就吃不消了。
在网上搜到雨松写的渐变代码,于是就拿来用了。可是版本不一样,我的是5.5.0版本,函数ModifyMesh的参数已经是VertexHelper了,所以就需要改动改动
具体代码如下:
---------------------
作者:苏小败在路上
来源:CSDN
原文:https://blog.csdn.net/pz789as/article/details/65628796
版权声明:本文为博主原创文章,转载请附上博文链接!
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; [AddComponentMenu("UI/Effects/TextGradient")] [RequireComponent(typeof(Text))] public class UICustomTextGradient : BaseMeshEffect { public Color32 topColor = Color.white; public Color32 bottomColor = Color.black; //后面自己添加的控制中心移动属性,有时候看着渐变不顺眼,中心偏离高或者低了,就可以通过这个去调整 [RangeAttribute(0, 1)] public float center = 0.5f; public override void ModifyMesh(VertexHelper vh) { if (!IsActive()) { return; } var count = vh.currentVertCount; if (count == 0) return; var vertexs = new List<UIVertex>(); for (var i = 0; i < count; i++) { var vertex = new UIVertex(); vh.PopulateUIVertex(ref vertex, i); vertexs.Add(vertex); } var topY = vertexs[0].position.y; var bottomY = vertexs[0].position.y; for (var i = 1; i < count; i++) { var y = vertexs[i].position.y; if (y > topY) { topY = y; } else if (y < bottomY) { bottomY = y; } } var height = topY - bottomY; for (var i = 0; i < count; i++) { var vertex = vertexs[i]; //使用处理过后的颜色 // var color = Color32.Lerp(bottomColor, topColor, (vertex.position.y - bottomY) / height); var color = CenterColor(bottomColor, topColor, (vertex.position.y - bottomY) / height); vertex.color = color; vh.SetUIVertex(vertex, i); } } //加了一个对颜色处理的函数,主要调整中心的位置 private Color32 CenterColor(Color32 bc, Color32 tc, float time){ if (center == 0){ return bc; }else if (center == 1){ return tc; }else{ var centerColor = Color32.Lerp(bottomColor, topColor, 0.5f); var resultColor = tc; if (time < center) { resultColor = Color32.Lerp(bottomColor, centerColor, time / center); }else{ resultColor = Color32.Lerp(centerColor, topColor, (time - center)/(1-center)); } return resultColor; } } } --------------------- 作者:苏小败在路上 来源:CSDN 原文:https://blog.csdn.net/pz789as/article/details/65628796 版权声明:本文为博主原创文章,转载请附上博文链接!