在编译模式下,批量修改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;
}
}
}
效果图: