Effect.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections.Generic;
  4. namespace WXB
  5. {
  6. static public class Effect
  7. {
  8. static void ApplyShadow(List<UIVertex> verts, Color32 color, int start, int end, float x, float y)
  9. {
  10. ApplyShadowZeroAlloc(verts, color, start, end, x, y);
  11. }
  12. static void ApplyShadowZeroAlloc(List<UIVertex> verts, Color32 color, int start, int end, float x, float y)
  13. {
  14. UIVertex vt;
  15. var neededCapacity = verts.Count + end - start;
  16. if (verts.Capacity < neededCapacity)
  17. verts.Capacity = neededCapacity;
  18. for (int i = start; i < end; ++i)
  19. {
  20. vt = verts[i];
  21. verts.Add(vt);
  22. Vector3 v = vt.position;
  23. v.x += x;
  24. v.y += y;
  25. vt.position = v;
  26. var newColor = color;
  27. newColor.a = (byte)((newColor.a * verts[i].color.a) / 255);
  28. vt.color = newColor;
  29. verts[i] = vt;
  30. }
  31. }
  32. static public void Shadow(VertexHelper vh, int start, Color effectColor, Vector2 effectDistance)
  33. {
  34. var output = ListPool<UIVertex>.Get();
  35. vh.GetUIVertexStream(output);
  36. ApplyShadow(output, effectColor, start, output.Count, effectDistance.x, effectDistance.y);
  37. vh.Clear();
  38. vh.AddUIVertexTriangleStream(output);
  39. ListPool<UIVertex>.Release(output);
  40. }
  41. static public void Outline(VertexHelper vh, int start, Color effectColor, Vector2 effectDistance)
  42. {
  43. var verts = ListPool<UIVertex>.Get();
  44. vh.GetUIVertexStream(verts);
  45. var neededCpacity = (verts.Count - start) * 5;
  46. if (verts.Capacity < neededCpacity)
  47. verts.Capacity = neededCpacity;
  48. var end = verts.Count;
  49. ApplyShadowZeroAlloc(verts, effectColor, start, verts.Count, effectDistance.x, effectDistance.y);
  50. start = end;
  51. end = verts.Count;
  52. ApplyShadowZeroAlloc(verts, effectColor, start, verts.Count, effectDistance.x, -effectDistance.y);
  53. start = end;
  54. end = verts.Count;
  55. ApplyShadowZeroAlloc(verts, effectColor, start, verts.Count, -effectDistance.x, effectDistance.y);
  56. start = end;
  57. end = verts.Count;
  58. ApplyShadowZeroAlloc(verts, effectColor, start, verts.Count, -effectDistance.x, -effectDistance.y);
  59. vh.Clear();
  60. vh.AddUIVertexTriangleStream(verts);
  61. ListPool<UIVertex>.Release(verts);
  62. }
  63. }
  64. public interface IEffect
  65. {
  66. void UpdateEffect(Draw draw, float deltaTime);
  67. void Release();
  68. }
  69. }