NicerOutline.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /// Credit Melang
  2. /// Sourced from - http://forum.unity3d.com/members/melang.593409/
  3. using System.Collections.Generic;
  4. namespace UnityEngine.UI.Extensions
  5. {
  6. //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
  7. [AddComponentMenu("UI/Effects/Extensions/Nicer Outline")]
  8. public class NicerOutline : BaseMeshEffect
  9. {
  10. [SerializeField]
  11. private Color m_EffectColor = new Color (0f, 0f, 0f, 0.5f);
  12. [SerializeField]
  13. private Vector2 m_EffectDistance = new Vector2 (1f, -1f);
  14. [SerializeField]
  15. private bool m_UseGraphicAlpha = true;
  16. private List < UIVertex > m_Verts = new List<UIVertex>();
  17. //
  18. // Properties
  19. //
  20. public Color effectColor
  21. {
  22. get
  23. {
  24. return this.m_EffectColor;
  25. }
  26. set
  27. {
  28. this.m_EffectColor = value;
  29. if (base.graphic != null)
  30. {
  31. base.graphic.SetVerticesDirty ();
  32. }
  33. }
  34. }
  35. public Vector2 effectDistance
  36. {
  37. get
  38. {
  39. return this.m_EffectDistance;
  40. }
  41. set
  42. {
  43. if (value.x > 600f)
  44. {
  45. value.x = 600f;
  46. }
  47. if (value.x < -600f)
  48. {
  49. value.x = -600f;
  50. }
  51. if (value.y > 600f)
  52. {
  53. value.y = 600f;
  54. }
  55. if (value.y < -600f)
  56. {
  57. value.y = -600f;
  58. }
  59. if (this.m_EffectDistance == value)
  60. {
  61. return;
  62. }
  63. this.m_EffectDistance = value;
  64. if (base.graphic != null)
  65. {
  66. base.graphic.SetVerticesDirty ();
  67. }
  68. }
  69. }
  70. public bool useGraphicAlpha
  71. {
  72. get
  73. {
  74. return this.m_UseGraphicAlpha;
  75. }
  76. set
  77. {
  78. this.m_UseGraphicAlpha = value;
  79. if (base.graphic != null)
  80. {
  81. base.graphic.SetVerticesDirty ();
  82. }
  83. }
  84. }
  85. protected void ApplyShadowZeroAlloc(List<UIVertex> verts, Color32 color, int start, int end, float x, float y)
  86. {
  87. UIVertex vt;
  88. var neededCpacity = verts.Count * 2;
  89. if (verts.Capacity < neededCpacity)
  90. verts.Capacity = neededCpacity;
  91. for (int i = start; i < end; ++i)
  92. {
  93. vt = verts[i];
  94. verts.Add(vt);
  95. Vector3 v = vt.position;
  96. v.x += x;
  97. v.y += y;
  98. vt.position = v;
  99. var newColor = color;
  100. if (m_UseGraphicAlpha)
  101. newColor.a = (byte)((newColor.a * verts[i].color.a) / 255);
  102. vt.color = newColor;
  103. verts[i] = vt;
  104. }
  105. }
  106. protected void ApplyShadow(List<UIVertex> verts, Color32 color, int start, int end, float x, float y)
  107. {
  108. var neededCpacity = verts.Count * 2;
  109. if (verts.Capacity < neededCpacity)
  110. verts.Capacity = neededCpacity;
  111. ApplyShadowZeroAlloc(verts, color, start, end, x, y);
  112. }
  113. public override void ModifyMesh(VertexHelper vh)
  114. {
  115. if (!this.IsActive ())
  116. {
  117. return;
  118. }
  119. m_Verts.Clear();
  120. vh.GetUIVertexStream(m_Verts);
  121. Text foundtext = GetComponent<Text>();
  122. float best_fit_adjustment = 1f;
  123. if (foundtext && foundtext.resizeTextForBestFit)
  124. {
  125. best_fit_adjustment = (float)foundtext.cachedTextGenerator.fontSizeUsedForBestFit / (foundtext.resizeTextMaxSize-1); //max size seems to be exclusive
  126. }
  127. float distanceX = this.effectDistance.x * best_fit_adjustment;
  128. float distanceY = this.effectDistance.y * best_fit_adjustment;
  129. int start = 0;
  130. int count = m_Verts.Count;
  131. this.ApplyShadow (m_Verts, this.effectColor, start, m_Verts.Count, distanceX, distanceY);
  132. start = count;
  133. count = m_Verts.Count;
  134. this.ApplyShadow (m_Verts, this.effectColor, start, m_Verts.Count, distanceX, -distanceY);
  135. start = count;
  136. count = m_Verts.Count;
  137. this.ApplyShadow (m_Verts, this.effectColor, start, m_Verts.Count, -distanceX, distanceY);
  138. start = count;
  139. count = m_Verts.Count;
  140. this.ApplyShadow (m_Verts, this.effectColor, start, m_Verts.Count, -distanceX, -distanceY);
  141. start = count;
  142. count = m_Verts.Count;
  143. this.ApplyShadow (m_Verts, this.effectColor, start, m_Verts.Count, distanceX, 0);
  144. start = count;
  145. count = m_Verts.Count;
  146. this.ApplyShadow (m_Verts, this.effectColor, start, m_Verts.Count, -distanceX, 0);
  147. start = count;
  148. count = m_Verts.Count;
  149. this.ApplyShadow (m_Verts, this.effectColor, start, m_Verts.Count, 0, distanceY);
  150. start = count;
  151. count = m_Verts.Count;
  152. this.ApplyShadow (m_Verts, this.effectColor, start, m_Verts.Count, 0, -distanceY);
  153. vh.Clear();
  154. vh.AddUIVertexTriangleStream(m_Verts);
  155. }
  156. #if UNITY_EDITOR
  157. protected override void OnValidate ()
  158. {
  159. this.effectDistance = this.m_EffectDistance;
  160. base.OnValidate ();
  161. }
  162. #endif
  163. }
  164. }