2023-02-07 22:35:43 +08:00
|
|
|
/// Credit Melang, Lee Hui
|
2015-02-03 07:07:31 +08:00
|
|
|
/// Sourced from - http://forum.unity3d.com/members/melang.593409/
|
2023-02-07 22:35:43 +08:00
|
|
|
/// GC Alloc fix - https://bitbucket.org/UnityUIExtensions/unity-ui-extensions/pull-requests/130
|
|
|
|
/// NOT supported in Unity 2022
|
2015-02-03 07:07:31 +08:00
|
|
|
|
2023-02-07 22:35:43 +08:00
|
|
|
#if !UNITY_2022_1_OR_NEWER
|
2015-02-03 07:07:31 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
namespace UnityEngine.UI.Extensions
|
|
|
|
{
|
2017-05-05 21:25:48 +08:00
|
|
|
//An outline that looks a bit nicer than the default one. It has less "holes" in the outline by drawing more copies of the effect
|
|
|
|
[AddComponentMenu("UI/Effects/Extensions/Nicer Outline")]
|
2015-09-10 05:31:17 +08:00
|
|
|
public class NicerOutline : BaseMeshEffect
|
2015-02-03 07:07:31 +08:00
|
|
|
{
|
|
|
|
[SerializeField]
|
|
|
|
private Color m_EffectColor = new Color (0f, 0f, 0f, 0.5f);
|
2017-12-07 21:34:55 +08:00
|
|
|
|
2015-02-03 07:07:31 +08:00
|
|
|
[SerializeField]
|
|
|
|
private Vector2 m_EffectDistance = new Vector2 (1f, -1f);
|
2017-12-07 21:34:55 +08:00
|
|
|
|
2015-02-03 07:07:31 +08:00
|
|
|
[SerializeField]
|
|
|
|
private bool m_UseGraphicAlpha = true;
|
2017-12-07 21:34:55 +08:00
|
|
|
|
|
|
|
private List < UIVertex > m_Verts = new List<UIVertex>();
|
|
|
|
|
2015-02-03 07:07:31 +08:00
|
|
|
//
|
|
|
|
// Properties
|
|
|
|
//
|
|
|
|
public Color effectColor
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return this.m_EffectColor;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
this.m_EffectColor = value;
|
|
|
|
if (base.graphic != null)
|
|
|
|
{
|
|
|
|
base.graphic.SetVerticesDirty ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-07 21:34:55 +08:00
|
|
|
|
2015-02-03 07:07:31 +08:00
|
|
|
public Vector2 effectDistance
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return this.m_EffectDistance;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value.x > 600f)
|
|
|
|
{
|
|
|
|
value.x = 600f;
|
|
|
|
}
|
|
|
|
if (value.x < -600f)
|
|
|
|
{
|
|
|
|
value.x = -600f;
|
|
|
|
}
|
|
|
|
if (value.y > 600f)
|
|
|
|
{
|
|
|
|
value.y = 600f;
|
|
|
|
}
|
|
|
|
if (value.y < -600f)
|
|
|
|
{
|
|
|
|
value.y = -600f;
|
|
|
|
}
|
|
|
|
if (this.m_EffectDistance == value)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.m_EffectDistance = value;
|
|
|
|
if (base.graphic != null)
|
|
|
|
{
|
|
|
|
base.graphic.SetVerticesDirty ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-07 21:34:55 +08:00
|
|
|
|
2015-02-03 07:07:31 +08:00
|
|
|
public bool useGraphicAlpha
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return this.m_UseGraphicAlpha;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
this.m_UseGraphicAlpha = value;
|
|
|
|
if (base.graphic != null)
|
|
|
|
{
|
|
|
|
base.graphic.SetVerticesDirty ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-07 22:35:43 +08:00
|
|
|
|
2015-10-11 18:33:43 +08:00
|
|
|
public override void ModifyMesh(VertexHelper vh)
|
|
|
|
{
|
|
|
|
if (!this.IsActive ())
|
2015-02-03 07:07:31 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2017-12-07 21:34:55 +08:00
|
|
|
|
|
|
|
m_Verts.Clear();
|
|
|
|
vh.GetUIVertexStream(m_Verts);
|
2015-02-03 07:07:31 +08:00
|
|
|
|
2015-09-10 05:31:17 +08:00
|
|
|
Text foundtext = GetComponent<Text>();
|
2017-12-07 21:34:55 +08:00
|
|
|
|
2015-02-03 07:07:31 +08:00
|
|
|
float best_fit_adjustment = 1f;
|
2017-12-07 21:34:55 +08:00
|
|
|
|
|
|
|
if (foundtext && foundtext.resizeTextForBestFit)
|
2015-02-03 07:07:31 +08:00
|
|
|
{
|
2017-12-07 21:34:55 +08:00
|
|
|
best_fit_adjustment = (float)foundtext.cachedTextGenerator.fontSizeUsedForBestFit / (foundtext.resizeTextMaxSize-1); //max size seems to be exclusive
|
2015-02-03 07:07:31 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
float distanceX = this.effectDistance.x * best_fit_adjustment;
|
|
|
|
float distanceY = this.effectDistance.y * best_fit_adjustment;
|
|
|
|
|
2023-02-07 22:35:43 +08:00
|
|
|
vh.Clear();
|
|
|
|
|
2015-02-03 07:07:31 +08:00
|
|
|
int start = 0;
|
2023-02-07 22:35:43 +08:00
|
|
|
|
|
|
|
// Apply Outline
|
|
|
|
start += this.ApplyOutlineNoGC(m_Verts, this.effectColor, distanceX, distanceY, vh, start);
|
|
|
|
start += this.ApplyOutlineNoGC(m_Verts, this.effectColor, distanceX, -distanceY, vh, start);
|
|
|
|
start += this.ApplyOutlineNoGC(m_Verts, this.effectColor, -distanceX, distanceY, vh, start);
|
|
|
|
start += this.ApplyOutlineNoGC(m_Verts, this.effectColor, -distanceX, -distanceY, vh, start);
|
|
|
|
start += this.ApplyOutlineNoGC(m_Verts, this.effectColor, distanceX, 0, vh, start);
|
|
|
|
start += this.ApplyOutlineNoGC(m_Verts, this.effectColor, -distanceX, 0, vh, start);
|
|
|
|
start += this.ApplyOutlineNoGC(m_Verts, this.effectColor, 0, distanceY, vh, start);
|
|
|
|
start += this.ApplyOutlineNoGC(m_Verts, this.effectColor, 0, -distanceY, vh, start);
|
|
|
|
|
|
|
|
// Apply self Text stuff
|
|
|
|
start += ApplyText(m_Verts, vh, start);
|
2015-09-10 05:31:17 +08:00
|
|
|
}
|
2023-02-07 22:35:43 +08:00
|
|
|
|
2023-07-06 13:20:50 +08:00
|
|
|
private int ApplyOutlineNoGC(List<UIVertex> verts, Color32 color, float x, float y, VertexHelper vh, int startIndex)
|
2023-02-07 22:35:43 +08:00
|
|
|
{
|
|
|
|
int length = verts.Count;
|
|
|
|
for (int i = 0; i < length; ++i)
|
|
|
|
{
|
|
|
|
UIVertex vt = verts[i];
|
|
|
|
|
|
|
|
Vector3 v = vt.position;
|
|
|
|
v.x += x;
|
|
|
|
v.y += y;
|
|
|
|
vt.position = v;
|
|
|
|
var newColor = color;
|
|
|
|
if (m_UseGraphicAlpha)
|
|
|
|
newColor.a = (byte)((newColor.a * verts[i].color.a) / 255);
|
|
|
|
vt.color = newColor;
|
|
|
|
|
|
|
|
// Tips: Since two triangles share same two vertices, in theory vertices can reduce to 4 / 6
|
|
|
|
// But VertexHelper.FillMesh forbid, so leave it be.
|
|
|
|
|
|
|
|
vh.AddVert(vt);
|
|
|
|
}
|
|
|
|
|
|
|
|
int triangleCount = length / 3;
|
|
|
|
for(int i=0; i<triangleCount; ++i)
|
|
|
|
{
|
|
|
|
int start = startIndex + 3 * i;
|
|
|
|
vh.AddTriangle(start + 0, start + 1, start + 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int ApplyText(List<UIVertex> verts, VertexHelper vh, int startIndex)
|
|
|
|
{
|
|
|
|
int length = verts.Count;
|
|
|
|
|
|
|
|
for (int i = 0; i < length; ++i)
|
|
|
|
{
|
|
|
|
vh.AddVert(verts[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
int triangleCount = length / 3;
|
|
|
|
for (int i = 0; i < triangleCount; ++i)
|
|
|
|
{
|
|
|
|
int start = startIndex + 3 * i;
|
|
|
|
vh.AddTriangle(start + 0, start + 1, start + 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2015-08-05 06:25:26 +08:00
|
|
|
|
|
|
|
#if UNITY_EDITOR
|
2015-02-03 07:07:31 +08:00
|
|
|
protected override void OnValidate ()
|
|
|
|
{
|
|
|
|
this.effectDistance = this.m_EffectDistance;
|
|
|
|
base.OnValidate ();
|
|
|
|
}
|
2015-08-05 06:25:26 +08:00
|
|
|
#endif
|
2015-02-03 07:07:31 +08:00
|
|
|
}
|
|
|
|
}
|
2023-02-07 22:35:43 +08:00
|
|
|
#endif
|