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();
}
}
}
unity3d:text描边
最新推荐文章于 2024-02-06 15:21:08 发布
这篇博客详细介绍了Unity中实现UI元素Bold Outline效果的代码实现,通过使用`ModifyMesh`方法和`Shadow`类扩展,实现了四个方向的描边阴影。代码中还包含了一个经济模式(Eco Mode),在帧率有限的情况下只应用两个方向的阴影以节省性能。

2518

被折叠的 条评论
为什么被折叠?



