Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- namespace UnityEngine.UI.Extensions
- {
- public class Outline : BaseVertexEffect
- {
- [SerializeField]
- private Color m_EffectColor = new Color(0f, 0f, 0f, 0.5f);
- [SerializeField]
- private bool m_Precise;
- [SerializeField]
- private Vector2 m_EffectDistance = new Vector2(1f, -1f);
- protected Outline()
- {
- }
- #if UNITY_EDITOR
- protected override void OnValidate()
- {
- effectDistance = m_EffectDistance;
- base.OnValidate();
- }
- #endif
- public Color effectColor
- {
- get { return m_EffectColor; }
- set
- {
- m_EffectColor = value;
- if (graphic != null)
- graphic.SetVerticesDirty();
- }
- }
- public Vector2 effectDistance
- {
- get { return m_EffectDistance; }
- set
- {
- if (value.x > 600)
- value.x = 600;
- if (value.x < -600)
- value.x = -600;
- if (value.y > 600)
- value.y = 600;
- if (value.y < -600)
- value.y = -600;
- if (m_EffectDistance == value)
- return;
- m_EffectDistance = value;
- if (graphic != null)
- graphic.SetVerticesDirty();
- }
- }
- protected void ApplyOutline(List<UIVertex> verts, Color32 color, int num, float x, float y)
- {
- var count = verts.Count;
- var index = 0;
- for (int pos = (num - 1) * 4; index < count; index++, pos += num * 4)
- {
- if (pos >= verts.Count)
- break;
- var first = verts[pos];
- var second = verts[pos + 1];
- var third = verts[pos + 2];
- var fourth = verts[pos + 3];
- first.color = color;
- second.color = color;
- third.color = color;
- fourth.color = color;
- first.position += new Vector3(x, y);
- second.position += new Vector3(x, y);
- third.position += new Vector3(x, y);
- fourth.position += new Vector3(x, y);
- verts.Insert(pos, first);
- verts.Insert(pos, second);
- verts.Insert(pos, third);
- verts.Insert(pos, fourth);
- pos += 4;
- }
- }
- public override void ModifyVertices(List<UIVertex> verts)
- {
- if (!IsActive())
- return;
- ApplyOutline(verts, effectColor, 1, effectDistance.x, effectDistance.y);
- ApplyOutline(verts, effectColor, 2, -effectDistance.x, -effectDistance.y);
- ApplyOutline(verts, effectColor, 3, -effectDistance.x, effectDistance.y);
- ApplyOutline(verts, effectColor, 4, effectDistance.x, -effectDistance.y);
- if (m_Precise)
- {
- ApplyOutline(verts, effectColor, 5, 0, -effectDistance.y);
- ApplyOutline(verts, effectColor, 6, 0, effectDistance.y);
- ApplyOutline(verts, effectColor, 7, -effectDistance.x, 0);
- ApplyOutline(verts, effectColor, 8, effectDistance.x, 0);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement