UGUI扩展编译器批量处理Text

14 篇文章 0 订阅

在编译模式下,批量修改Text的属性,将该脚本放在Editor文件夹里:

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 请将该脚本放在Editor文件夹下
/// </summary>
public class TextEditor : Editor {

    [MenuItem("Tools/UI.Text/Change Font")]
    public static void SpecifyTextFont()
    {
        //用于得到选中的物体
        GameObject[] objs = Selection.GetFiltered<GameObject>(SelectionMode.Assets);
        foreach (var go in objs)
        {
            Text text = go.GetComponent<Text>();
            text.raycastTarget = false; 
            text.fontStyle = FontStyle.Normal;
            text.alignment = TextAnchor.MiddleCenter;
            text.fontSize *= 2;

            RectTransform rect = text.GetComponent<RectTransform>();
            Vector2 size = rect.sizeDelta;
            //将缩放设置为0.5倍,更清晰
            rect.localScale = new Vector3(0.5f, 0.5f, 0.5f);
            rect.sizeDelta = size * 2;

            //加载文字渐变的脚本
            var gradient = go.GetComponent<Gradient>();
            if (!gradient)
            {
                go.AddComponent<Gradient>();
            }

            //添加阴影组件
            var shadow = go.GetComponent<Shadow>();
            if (!shadow)
            {
                shadow = go.AddComponent<Shadow>();
                shadow.effectDistance = new Vector2(5, -5);
            }
        }
    }
}

选中需要修改的的Text,点击编译栏的Tools -> UI.Text -> Change Font,如图。

下面是所用到的字体渐变源码。参考自:https://blog.csdn.net/hack_yin/article/details/77579077

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public enum Type
{
    Horizontal,
    Vertical
}

public enum Blend
{
    Override,
    Add,
    Multiply
}

[AddComponentMenu("UI/Effects/UGUI_Gradient")]
public class Gradient : BaseMeshEffect {

    [SerializeField]
    Type _gradientType;

    [SerializeField]
    Blend _blendMode = Blend.Multiply;

    [SerializeField]
    [Range(-1, 1)]
    float _offset = 0f;

    //Gradient : 梯度
    [SerializeField]
    UnityEngine.Gradient _effectGradient = new UnityEngine.Gradient()
    { colorKeys = new GradientColorKey[] 
    { new GradientColorKey(Color.black, 0), new GradientColorKey(Color.white, 1) } };

    #region 属性
    public Blend BlendMode
    {
        get { return _blendMode; }
        set { _blendMode = value; }
    }

    public UnityEngine.Gradient EffectGradient
    {
        get { return _effectGradient; }
        set { _effectGradient = value; }
    }

    public Type GradientType
    {
        get { return _gradientType; }
        set { _gradientType = value; }
    }

    public float Offset
    {
        get { return _offset; }
        set { _offset = value; }
    }

    #endregion

    public override void ModifyMesh(VertexHelper helper)
    {
        if (!IsActive() || helper.currentVertCount == 0)
        {
            return;
        }

        List<UIVertex> _vertexList = new List<UIVertex>();
        helper.GetUIVertexStream(_vertexList);

        int nCount = _vertexList.Count;
        switch (GradientType)
        {
            case Type.Horizontal:
                {
                    float left = _vertexList[0].position.x;
                    float right = _vertexList[0].position.x;

                    float x = 0f;
                    for (int i = nCount - 1; i >= 1; --i)
                    {
                        x = _vertexList[i].position.x;

                        if (x > right)
                        {
                            right = x;
                        }
                        else if (x < left)
                        {
                            left = x;
                        }
                    }

                    float width = 1f / (right - left);
                    UIVertex vertex = new UIVertex();

                    for (int i = 0; i < helper.currentVertCount; i++)
                    {
                        helper.PopulateUIVertex(ref vertex, i);
                        vertex.color = BlendColor(vertex.color, EffectGradient.Evaluate((vertex.position.x - left) * width - Offset));
                        helper.SetUIVertex(vertex, i);
                    }
                }
                break;
            case Type.Vertical:
                {
                    float bottom = _vertexList[0].position.y;
                    float top = _vertexList[0].position.y;
                    float y = 0f;
                    for (int i = nCount - 1; i >= 0; --i)
                    {
                        y = _vertexList[i].position.y;
                        if (y > top)
                        {
                            top = y;
                        }
                        else if (y < bottom)
                        {
                            bottom = y;
                        }
                    }

                    float height = 1f / (top - bottom);
                    UIVertex vertex = new UIVertex();

                    for (int i = 0; i < helper.currentVertCount; i++)
                    {
                        helper.PopulateUIVertex(ref vertex, i);
                        vertex.color = BlendColor(vertex.color, EffectGradient.Evaluate((vertex.position.y - bottom) * height - Offset));
                        helper.SetUIVertex(vertex, i);
                    }
                }
                break;

        }
    }

    Color BlendColor(Color colorA, Color colorB)
    {
        switch (BlendMode)
        {
            default:
                return colorB;
            case Blend.Add:
                return colorA + colorB;
            case Blend.Multiply:
                return colorA * colorB;
            
        }
    }


}

效果图:

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值