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