Unity3D-UGUI的Text组件渐变效果
来源:互联网 发布:易云数据vps 编辑:程序博客网 时间:2024/06/11 01:01
参考:http://blog.csdn.net/pz789as/article/details/65628796
今日在做项目中发现,很多时候需要对文字多特殊处理,渐变就是最常用的。
特别是文字特别多,还有动态的时候,就不能只靠图了,否则包的大小就吃不消了。
在网上搜到雨松写的渐变代码,于是就拿来用了。可是版本不一样,我的是5.5.0版本,函数ModifyMesh的参数已经是VertexHelper了,所以就需要改动改动
具体代码如下:
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;}}}
效果截图:
center为0.8时
center为0.2时
center为0.5时
0 0
- Unity3D-UGUI的Text组件渐变效果
- unity3d ugui text 文字颜色渐变效果~
- 【Unity3D】UGUI组件的监听
- UGUI自带的颜色渐变效果
- Unity3D的UGUI里Text 组件怎么输入换行符的一个小技巧(新人)
- Unity UGUI--Text组件
- UGUI基础组件Text
- uGUI-Effect-Tool 渐变效果
- Unity5的uGUI中实现文字渐变效果(Gradient)
- UGUI研究院之Text文本渐变
- UGUI之Text组件详解
- UGUI Text组件实际文本宽高的获取
- Unity3D 基于UGUI的图文混排组件
- [Unity3D]UGUI分辨率自适应的组件和方法
- Unity3D UGUI组件的Inspector属性探究(一)--RectTransform
- Unity3D 官方文档 UGUI总览 可互动组件的介绍
- Unity3D 官方文档 UGUI总览 自动布局组件的介绍
- Unity实现UI渐变效果(UGUI)
- 利用Python 生成hash值
- 排序总结
- 欢迎使用CSDN-markdown编辑器
- 打印机常见问题!
- 4类抽奖算法总结
- Unity3D-UGUI的Text组件渐变效果
- bzoj 4455: [Zjoi2016]小星星 (容斥原理+DP)
- 【Java线程】Callable和Future
- 生成格雷码
- Python面试题整理
- FFmpeg编码基础流程
- java中几种创建对象的方式 五
- 工业4.0时代,机器人发展现状及技术趋势揭示
- c# dapper oracle like 参数话