当前位置: 代码迷 >> 综合 >> UGUI Text渐变、弯曲、文字跳动(BaseMeshEffect拓展)
  详细解决方案

UGUI Text渐变、弯曲、文字跳动(BaseMeshEffect拓展)

热度:20   发布时间:2023-12-15 17:15:10.0

如果UGUI文字想实现复杂效果建议使用TextMeshPro。

1、文字弯曲效果

using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;[AddComponentMenu("UI/Effects/FontCurve")]
public class FontCurve: BaseMeshEffect
{
    [SerializeField]AnimationCurve curve = AnimationCurve.Linear(0, 0, 1, 10);[SerializeField]float curveMultiplier = 1;RectTransform rectTransform;public override void ModifyMesh(VertexHelper vh){
    if (!IsActive()) return;List<UIVertex> verts = new List<UIVertex>();vh.GetUIVertexStream(verts);for (int index = 0; index < verts.Count; index++){
    var uiVertex = verts[index];uiVertex.position.y += curve.Evaluate(rectTransform.rect.width * rectTransform.pivot.x + uiVertex.position.x) * curveMultiplier;verts[index] = uiVertex;}vh.AddUIVertexTriangleStream(verts);}#if UNITY_EDITORprotected override void OnValidate(){
    base.OnValidate();if (curve[0].time != 0){
    var tmpRect = curve[0];tmpRect.time = 0;curve.MoveKey(0, tmpRect);}if (rectTransform== null)rectTransform= GetComponent<RectTransform>();if (curve[curve.length - 1].time != rectTransform.rect.width)OnRectTransformDimensionsChange();}
#endifprotected override void Awake(){
    base.Awake();rectTransform= GetComponent<RectTransform>();OnRectTransformDimensionsChange();}protected override void OnEnable(){
    base.OnEnable();rectTransform= GetComponent<RectTransform>();OnRectTransformDimensionsChange();}protected override void OnRectTransformDimensionsChange(){
    var temp = curve[curve.length - 1];if (rectTransform!= null){
    temp .time = rectTransform.rect.width;curve.MoveKey(curve.length - 1, temp );}}
}

2、文字颜色上下渐变效果

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using System;[AddComponentMenu("UI/Effects/FontGradient")]
public class FontGradient : BaseMeshEffect
{
    [SerializeField]Color32 topColor = Color.white;[SerializeField]Color32 bottomColor = Color.black;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);vertex.color = color;vh.SetUIVertex(vertex, i);}}
}

3、文字跳动效果

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;[AddComponentMenu("UI/Effects/FontBeat")]
public class FontBeat : BaseMeshEffect
{
    [SerializeField] int StartIndex;[SerializeField] int EndIndex;[SerializeField] float Speed;[SerializeField] float Density;[SerializeField] float Magnitude;private Text m_text;List<UIVertex> vertices = new List<UIVertex>();List<UIVertex> newVerts = new List<UIVertex>();public override void ModifyMesh(VertexHelper vh){
    vertices.Clear();newVerts.Clear();vh.GetUIVertexStream(vertices);for (int i = 0; i < (vertices.Count / 6); i++){
    if (i >= StartIndex && i < EndIndex){
    newVerts.AddRange(Characters(vertices.GetRange(i * 6, 6), i));}else{
    newVerts.AddRange(vertices.GetRange(i * 6, 6));}}vh.Clear();vh.AddUIVertexTriangleStream(newVerts);}protected override void Start(){
    m_text = GetComponent<Text>();base.Start();}List<UIVertex> Characters(List<UIVertex> verts, int characterindex){
    for (int i = 0; i < verts.Count; i++){
    UIVertex c = verts[i];c.position = c.position + new Vector3(1, Magnitude * Mathf.Sin((Time.timeSinceLevelLoad * Speed) + (characterindex * Density)), 1);verts[i] = c;}return verts;}void Update(){
    UpdateBeat();}public void UpdateBeat(){
    m_text.SetVerticesDirty();}
}
  相关解决方案