Unity制作文字光晕衬底效果

2025/01 02 13:01

添加一个文字修改器,把uv和position都修改掉

using UnityEngine.UI;
using UnityEngine;

public class UIFontBackground : BaseMeshEffect
{
    private static readonly Vector2[] mVertexUvs = new Vector2[]
    {
        new Vector2(0, 0),
        new Vector2(0, 1),
        new Vector2(1, 1),
        new Vector2(1, 0),
    };

    [SerializeField]
    private float mExpandVal = 16.0f;

    public override void ModifyMesh(VertexHelper vh)
    {
        Vector3[] mPosOffset = new Vector3[]
        {
            new Vector3(-mExpandVal, mExpandVal,  0),
            new Vector3(mExpandVal/2, mExpandVal, 0),
            new Vector3(mExpandVal/2, -mExpandVal, 0),
            new Vector3(-mExpandVal, -mExpandVal, 0),
        };

        var vsize = mVertexUvs.Length;
        UIVertex vert = new UIVertex();
        int size = vh.currentVertCount / vsize;
        for (int loop = 0; loop < size; loop++)
        {
            for(int vidx=0; vidx< vsize; ++vidx)
            {
                int idx = loop*vsize+vidx;
                vh.PopulateUIVertex(ref vert, idx);
                vert.uv0 = mVertexUvs[vidx];               
                vert.position += mPosOffset[vidx];
                vh.SetUIVertex(vert, idx);
            }
        }
    }    
}
Shader里面把颜色全部修改为黑色,并且制作一张独立的四周虚化的纹理
half4 color = tex2D(_MainTex2, IN.texcoord);
return half4(0,0,0, color.a);

然后把这个底和文字放在一起,文字叠在上面就行了