unity3d:text描边

2023-08-24 15:04:27 浏览数 (1)

代码语言:javascript复制
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;

namespace Effects
{
    [RequireComponent(typeof(Text))]
    [AddComponentMenu("UI/Effects/BoldOutline")]
    public class BoldOutline : Shadow
    {
        public bool IsEcoMode = true;
        public static List<UIVertex> verts = new List<UIVertex>();
        const int OUTLINE_BUDGET = 50;
        static int frameNo = 0;
        static int iBudgetUsed = 0;


        public override void ModifyMesh(VertexHelper vh)
        {
            bool bIsBudgetEnough = true;
            if(Time.frameCount ==frameNo)
            {
                if (iBudgetUsed >= OUTLINE_BUDGET)
                {
                    bIsBudgetEnough = false;
                }
                else
                {
                    iBudgetUsed  ;
                }
            }
            else
            {
                frameNo = Time.frameCount;
                iBudgetUsed = 0;
            }
            
            if (!IsActive())
                return;

            verts.Clear();
            vh.GetUIVertexStream(verts);

            var neededCpacity = verts.Count * 5;
            if (verts.Capacity < neededCpacity)
                verts.Capacity = neededCpacity;

            var start = 0;
            var end = verts.Count;
            ApplyShadowZeroAlloc(verts, effectColor, start, verts.Count, effectDistance.x, effectDistance.y);

            start = end;
            end = verts.Count;
            ApplyShadowZeroAlloc(verts, effectColor, start, verts.Count, -effectDistance.x, -effectDistance.y);

            if(bIsBudgetEnough)
            {
                start = end;
                end = verts.Count;
                ApplyShadowZeroAlloc(verts, effectColor, start, verts.Count, effectDistance.x, -effectDistance.y);

                start = end;
                end = verts.Count;
                ApplyShadowZeroAlloc(verts, effectColor, start, verts.Count, -effectDistance.x, effectDistance.y);
            }
           
            if(!IsEcoMode)
            {
                start = end;
                end = verts.Count;
                ApplyShadowZeroAlloc(verts, effectColor, start, verts.Count, effectDistance.x, 0);

                start = end;
                end = verts.Count;
                ApplyShadowZeroAlloc(verts, effectColor, start, verts.Count, -effectDistance.x, 0);

                start = end;
                end = verts.Count;
                ApplyShadowZeroAlloc(verts, effectColor, start, verts.Count, 0, effectDistance.y);

                start = end;
                end = verts.Count;
                ApplyShadowZeroAlloc(verts, effectColor, start, verts.Count, 0, -effectDistance.y);
            }
            vh.Clear();
            vh.AddUIVertexTriangleStream(verts);
            verts.Clear();
        }
    }
}

0 人点赞