赞
踩
下面的组件做一个记录,可以使ui自己设置单个文字上下颜色,最终效果为,仿照艺术字
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
[AddComponentMenu("UI/Effect/UIColorGradient")]
public class UIColorGradient : BaseMeshEffect
{
public static List<UIVertex> helpList = new List<UIVertex>();
private bool isText;
private bool isCheck;
[SerializeField]
private Color32 topColor = Color.yellow;
[SerializeField]
private Color32 bottomColor = Color.red;
protected override void OnEnable()
{
base.OnEnable();
if (this.gameObject.GetComponent<Text>() != null)
isText = true;
else
isText = false;
}
private void ModifyMeshText(VertexHelper vertexHelper)
{
helpList.Clear();
vertexHelper.GetUIVertexStream(helpList);
int count = helpList.Count;
if (count > 0)
{
for (int i = 0; i < helpList.Count;)
{
float bottomY = helpList[i].position.y;
float topY = bottomY;
float dis = 1f;
for (int k = 1; k < 6; k++)
{
float y = helpList[k + i].position.y;
if (y > topY)
{
topY = y;
}
else if (y < bottomY)
{
bottomY = y;
}
}
dis = topY - bottomY;
for (int k = 0; k < 6; k++)
{
UIVertex vertText = helpList[k + i];
vertText.color = Color32.Lerp(bottomColor, topColor, (vertText.position.y - bottomY) / dis);
helpList[k + i] = vertText;
}
i += 6;
}
vertexHelper.Clear();
vertexHelper.AddUIVertexTriangleStream(helpList);
helpList.Clear();
}
}
public override void ModifyMesh(VertexHelper vertexHelper)
{
if (!IsActive())
{
return;
}
if(isText)
{
ModifyMeshText(vertexHelper);
return;
}
helpList.Clear();
vertexHelper.GetUIVertexStream(helpList);
int count = helpList.Count;
if (count > 0)
{
Vector3 pos = helpList[0].position;
float bottomY = pos.y;
float topY = pos.y;
for (int i = 1; i < count; i++)
{
float y = helpList[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 = helpList[i];
Color32 color32 = Color32.Lerp(bottomColor, topColor, (uIVertex.position.y - bottomY) / uiElementHeight);
uIVertex.color = color32;
helpList[i] = uIVertex;
}
vertexHelper.Clear();
vertexHelper.AddUIVertexTriangleStream(helpList);
}
helpList.Clear();
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。