这是我无意间逛国外论坛发现的,感觉还可以就分享给大家。原文 http://pastebin.com/dJabCfWn
如下图所示,用法和UGUI自带的outline和shadow一样,可以同时使用。
代码在这里了。我加了个判断,它原来的有越界的隐患。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
[AddComponentMenu("UI/Effects/Gradient")]
public class Gradient : BaseVertexEffect {
[SerializeField]
private Color32 topColor = Color.white;
[SerializeField]
private Color32 bottomColor = Color.black;
public override void ModifyVertices(List<UIVertex> vertexList) {
if (!IsActive()) {
return;
}
int count = vertexList.Count;
if(count>0){
float bottomY = vertexList[0].position.y;
float topY = vertexList[0].position.y;
for (int i = 1; i < count; i++) {
float y = vertexList[i].position.y;
if (y > topY) {
topY = y;
}
else if (y < bottomY) {
bottomY = y;
}
}
float uiElementHeight = topY - bottomY;
for (int i = 0; i < count; i++) {
UIVertex uiVertex = vertexList[i];
uiVertex.color = Color32.Lerp(bottomColor, topColor, (uiVertex.position.y - bottomY) / uiElementHeight);
vertexList[i] = uiVertex;
}
}
}
}
我也在测试中,欢迎大家提意见。
如果你的项目升级到了5.2 请使用下面的代码,感谢代码的提供者。@獨立遊戲開發熊
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
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 (Mesh mesh)
{
if (!IsActive ()) {
return;
}
Vector3[] vertexList = mesh.vertices;
int count = mesh.vertexCount;
if (count > 0) {
float bottomY = vertexList [0].y;
float topY = vertexList [0].y;
for (int i = 1; i < count; i++) {
float y = vertexList [i].y;
if (y > topY) {
topY = y;
} else if (y < bottomY) {
bottomY = y;
}
}
List<Color32> colors = new List<Color32> ();
float uiElementHeight = topY - bottomY;
for (int i = 0; i < count; i++) {
colors.Add (Color32.Lerp (bottomColor, topColor, (vertexList [i].y - bottomY) / uiElementHeight));
}
mesh.SetColors (colors);
}
}
}
- 本文固定链接: https://www.xuanyusong.com/archives/3471
- 转载请注明: 雨松MOMO于 雨松MOMO程序研究院 发表
捐 赠写博客不易,如果您想请我喝一杯星巴克的话?就进来看吧!



不是有组件能支持文本渐变吗?gradient color 组件。。。
UGUI自带吗?没找到啊?
喔。。我看错了。。。
5.3版本实测已经变成这个了:
for (int i = 0; i = 6; )
{
ChangeColor(ref vertexList, i, topColor);
ChangeColor(ref vertexList, i 1, topColor);
ChangeColor(ref vertexList, i 2, bottomColor);
ChangeColor(ref vertexList, i 3, bottomColor);
ChangeColor(ref vertexList, i 4, bottomColor);
ChangeColor(ref vertexList, i 5, topColor);
i = 6;
}
话说这个代码放在什么位置呢??for (int i = 0; i < vertexList.Count; )
unity5.22版本参考http://answers.unity3d.com/questions/1086415/gradient-text-in-unity-522-basevertexeffect-is-obs.html
使用正常。代码如下:
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;
[AddComponentMenu(“UI/Effects/Gradient”)]
public class Gradient : BaseMeshEffect
{
public Color32 topColor = Color.white;
public Color32 bottomColor = Color.black;
public override void ModifyMesh(VertexHelper helper)
{
if (!IsActive() || helper.currentVertCount == 0)
return;
List vertices = new List();
helper.GetUIVertexStream(vertices);
float bottomY = vertices[0].position.y;
float topY = vertices[0].position.y;
for (int i = 1; i topY)
{
topY = y;
}
else if (y < bottomY)
{
bottomY = y;
}
}
float uiElementHeight = topY – bottomY;
UIVertex v = new UIVertex();
for (int i = 0; i < helper.currentVertCount; i )
{
helper.PopulateUIVertex(ref v, i);
v.color = Color32.Lerp(bottomColor, topColor, (v.position.y – bottomY) / uiElementHeight);
helper.SetUIVertex(v, i);
}
}
}
unity5.22版本参考http://answers.unity3d.com/questions/1086415/gradient-text-in-unity-522-basevertexeffect-is-obs.html
使用正常。代码如下:
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;
[AddComponentMenu(“UI/Effects/Gradient”)]
public class Gradient : BaseMeshEffect
{
public Color32 topColor = Color.white;
public Color32 bottomColor = Color.black;
public override void ModifyMesh(VertexHelper helper)
{
if (!IsActive() || helper.currentVertCount == 0)
return;
List vertices = new List();
helper.GetUIVertexStream(vertices);
float bottomY = vertices[0].position.y;
float topY = vertices[0].position.y;
for (int i = 1; i topY)
{
topY = y;
}
else if (y < bottomY)
{
bottomY = y;
}
}
float uiElementHeight = topY – bottomY;
UIVertex v = new UIVertex();
for (int i = 0; i < helper.currentVertCount; i++)
{
helper.PopulateUIVertex(ref v, i);
v.color = Color32.Lerp(bottomColor, topColor, (v.position.y – bottomY) / uiElementHeight);
helper.SetUIVertex(v, i);
}
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using System;
[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 (!this.IsActive())
return;
List vertexList = new List();
vh.GetUIVertexStream(vertexList);
ModifyVertices(vertexList);
vh.Clear();
vh.AddUIVertexTriangleStream(vertexList);
}
public void ModifyVertices(List vertexList)
{
if (!IsActive())
{
return;
}
int count = vertexList.Count;
if (count > 0)
{
float bottomY = vertexList[0].position.y;
float topY = vertexList[0].position.y;
for (int i = 1; i topY)
{
topY = y;
}
else if (y < bottomY)
{
bottomY = y;
}
}
float uiElementHeight = topY – bottomY;
for (int i = 0; i < count; i )
{
UIVertex uiVertex = vertexList[i];
uiVertex.color = Color32.Lerp(bottomColor, topColor, (uiVertex.position.y – bottomY) / uiElementHeight);
vertexList[i] = uiVertex;
}
}
}
}
Unity5.2.1p4版本
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using System;
[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 (!this.IsActive())
return;
List vertexList = new List();
vh.GetUIVertexStream(vertexList);
ModifyVertices(vertexList);
vh.Clear();
vh.AddUIVertexTriangleStream(vertexList);
}
public void ModifyVertices(List vertexList)
{
if (!IsActive())
{
return;
}
int count = vertexList.Count;
if (count > 0)
{
float bottomY = vertexList[0].position.y;
float topY = vertexList[0].position.y;
for (int i = 1; i topY)
{
topY = y;
}
else if (y < bottomY)
{
bottomY = y;
}
}
float uiElementHeight = topY – bottomY;
for (int i = 0; i < count; i++)
{
UIVertex uiVertex = vertexList[i];
uiVertex.color = Color32.Lerp(bottomColor, topColor, (uiVertex.position.y – bottomY) / uiElementHeight);
vertexList[i] = uiVertex;
}
}
}
}
Unity5.2.1p4版本
[good]
现在UGUI有这个效果,添加组件-effect 就可以找到
能详细说一下么,你unity的版本,组件的路径和组件的名称
5,2 上没有
大神,我想问下UGUI 的Text 文本要加 中划线 怎么弄???我查了文档好像只有加粗斜体和颜色。我记得以前用NGUI是可以的。。求解答
https://github.com/nicoplv/underlined-text-unity-ui
为什么我按照上面的代码 写了一份,UI/Effects 里面没有出现Gradient 选项啊
可不可以自己写一个描边Outline?感觉现在的不好用,想描粗一点就走样了
自己写图文混排工具 代替一切!!
Image上面覆盖一层Text,就是button的情况。然后有重叠的时候会产生大量的draw call。不知道要怎么解决啊。
不是一个图集。。重叠产生draw call 很正常啊。。
同一个图片中的切片啊。
把所有text放一个canvas下就是一个DC
我修改了下.可以支持多行显示,大家可以测试下.for (int i = 0; i < vertexList.Count; ){ChangeColor( ref vertexList, i, topColor);ChangeColor( ref vertexList, i 1, topColor);ChangeColor( ref vertexList, i 2, bottomColor);ChangeColor( ref vertexList, i 3, bottomColor);i = 4;}private void ChangeColor( ref List verList, int index, Color color){UIVertex temp = verList[index];temp.color = color;verList[index] = temp;}
大神,用你修改的测试了下,怎么没有效果啊,还是只能单行渐变。行数多了就不行了,是不是我代码修改地方错了
我修改了下.可以支持多行显示,大家可以测试下.for (int i = 0; i < vertexList.Count; ){ ChangeColor( ref vertexList, i, topColor); ChangeColor( ref vertexList, i + 1, topColor); ChangeColor( ref vertexList, i + 2, bottomColor); ChangeColor( ref vertexList, i + 3, bottomColor); i += 4;}private void ChangeColor( ref List verList, int index, Color color){ UIVertex temp = verList[index]; temp.color = color; verList[index] = temp;}
赞~
这个很实用!
冒昧地更正一下,那个ChangeColor的第一个参数类型应该是List,不是List
冒昧地更正一下,那个ChangeColor的第一个参数类型应该是List(左尖括号)UIVertex(右尖括号),不是List。不是你的手误,是被html吃掉了
Unity5.2.2显示有问题
5.3版本实测已经变成这个了:
for (int i = 0; i = 6; )
{
ChangeColor(ref vertexList, i, topColor);
ChangeColor(ref vertexList, i + 1, topColor);
ChangeColor(ref vertexList, i + 2, bottomColor);
ChangeColor(ref vertexList, i + 3, bottomColor);
ChangeColor(ref vertexList, i + 4, bottomColor);
ChangeColor(ref vertexList, i + 5, topColor);
i += 6;
}
发现一个蛋疼的问题。 UGUI的Text 没有字间距这种属性。。。。。。。。。。。。。。
所以要自己写工具啊。。
完全木有思路,看了半天。。。
http://forum.unity3d.com/threads/adjustable-character-spacing-free-script.288277/
还有我的outline和shadow的颜色也被同化了 并没有出黑色的阴影和描边效果 调大distance出现的是一个和原来文字效果一样的文字
调整一下Gradient组件的顺序 放在最上面就好了。
这个是对于整个字段总体的一个渐变啊!~如果是一段文字就发现上面一行都是一个颜色 有没有方法让每行文字都有渐变
好吧 我明白了。。 我项目封装了图文混排工具是一行一行的绘制的。 所以没这个问题。。
请问脚本是C#么?有一些不是很懂!
是C#
是哪个国外论坛?最近在学习u3d,麻烦雨松推荐几个Unity3D的论坛和网站
也没有特定的, 就是找问题的时候用英文在谷歌里找 不用中文找。
赞 好厉害