Advertisement
Guest User

Outline

a guest
Jun 19th, 2015
907
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.46 KB | None | 0 0
  1. using System.Collections.Generic;
  2.  
  3. namespace UnityEngine.UI.Extensions
  4. {
  5.     public class Outline : BaseVertexEffect
  6.     {
  7.         [SerializeField]
  8.         private Color m_EffectColor = new Color(0f, 0f, 0f, 0.5f);
  9.  
  10.         [SerializeField]
  11.         private bool m_Precise;
  12.  
  13.         [SerializeField]
  14.         private Vector2 m_EffectDistance = new Vector2(1f, -1f);
  15.  
  16.         protected Outline()
  17.         {
  18.         }
  19.  
  20. #if UNITY_EDITOR
  21.         protected override void OnValidate()
  22.         {
  23.             effectDistance = m_EffectDistance;
  24.             base.OnValidate();
  25.         }
  26. #endif
  27.  
  28.         public Color effectColor
  29.         {
  30.             get { return m_EffectColor; }
  31.             set
  32.             {
  33.                 m_EffectColor = value;
  34.                 if (graphic != null)
  35.                     graphic.SetVerticesDirty();
  36.             }
  37.         }
  38.  
  39.         public Vector2 effectDistance
  40.         {
  41.             get { return m_EffectDistance; }
  42.             set
  43.             {
  44.                 if (value.x > 600)
  45.                     value.x = 600;
  46.                 if (value.x < -600)
  47.                     value.x = -600;
  48.  
  49.                 if (value.y > 600)
  50.                     value.y = 600;
  51.                 if (value.y < -600)
  52.                     value.y = -600;
  53.  
  54.                 if (m_EffectDistance == value)
  55.                     return;
  56.  
  57.                 m_EffectDistance = value;
  58.  
  59.                 if (graphic != null)
  60.                     graphic.SetVerticesDirty();
  61.             }
  62.         }
  63.  
  64.         protected void ApplyOutline(List<UIVertex> verts, Color32 color, int num, float x, float y)
  65.         {
  66.             var count = verts.Count;
  67.             var index = 0;
  68.  
  69.             for (int pos = (num - 1) * 4; index < count; index++, pos += num * 4)
  70.             {
  71.                 if (pos >= verts.Count)
  72.                     break;
  73.                 var first = verts[pos];
  74.                 var second = verts[pos + 1];
  75.                 var third = verts[pos + 2];
  76.                 var fourth = verts[pos + 3];
  77.  
  78.                 first.color = color;
  79.                 second.color = color;
  80.                 third.color = color;
  81.                 fourth.color = color;
  82.                 first.position += new Vector3(x, y);
  83.                 second.position += new Vector3(x, y);
  84.                 third.position += new Vector3(x, y);
  85.                 fourth.position += new Vector3(x, y);
  86.  
  87.                 verts.Insert(pos, first);
  88.                 verts.Insert(pos, second);
  89.                 verts.Insert(pos, third);
  90.                 verts.Insert(pos, fourth);
  91.                 pos += 4;
  92.             }
  93.         }
  94.  
  95.         public override void ModifyVertices(List<UIVertex> verts)
  96.         {
  97.             if (!IsActive())
  98.                 return;
  99.  
  100.             ApplyOutline(verts, effectColor, 1, effectDistance.x, effectDistance.y);
  101.             ApplyOutline(verts, effectColor, 2, -effectDistance.x, -effectDistance.y);
  102.             ApplyOutline(verts, effectColor, 3, -effectDistance.x, effectDistance.y);
  103.             ApplyOutline(verts, effectColor, 4, effectDistance.x, -effectDistance.y);
  104.             if (m_Precise)
  105.             {
  106.                 ApplyOutline(verts, effectColor, 5, 0, -effectDistance.y);
  107.                 ApplyOutline(verts, effectColor, 6, 0, effectDistance.y);
  108.                 ApplyOutline(verts, effectColor, 7, -effectDistance.x, 0);
  109.                 ApplyOutline(verts, effectColor, 8, effectDistance.x, 0);
  110.             }
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement