OutlineDraw.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. namespace WXB
  4. {
  5. [ExecuteInEditMode]
  6. public class OutlineDraw : EffectDrawObjec, ICanvasElement
  7. {
  8. public override DrawType type { get { return DrawType.Outline; } }
  9. public bool isOpenAlpha
  10. {
  11. get { return GetOpen(0); }
  12. set { SetOpen<AlphaEffect>(0, value); }
  13. }
  14. public override void UpdateSelf(float deltaTime)
  15. {
  16. base.UpdateSelf(deltaTime);
  17. if (currentWidth >= maxWidth || m_Data == null)
  18. return;
  19. float temp = currentWidth;
  20. for (int i = 0; i < m_Data.lines.Count; ++i)
  21. {
  22. DrawLineStruct.Line l = m_Data.lines[i];
  23. if (temp >= l.width)
  24. {
  25. temp -= l.width;
  26. }
  27. else
  28. {
  29. // 还未达到,使用这个速度来
  30. float t = (l.width - temp) / l.dynSpeed;
  31. if (t >= deltaTime)
  32. {
  33. // 所用的时间要大于当前间隔时间
  34. currentWidth += deltaTime * l.dynSpeed;
  35. break;
  36. }
  37. else
  38. {
  39. currentWidth += l.dynSpeed * t;
  40. deltaTime -= t;
  41. temp -= l.dynSpeed * t;
  42. }
  43. }
  44. }
  45. CanvasUpdateRegistry.RegisterCanvasElementForGraphicRebuild(this); // 重绘
  46. }
  47. DrawLineStruct m_Data;
  48. float currentWidth = 0f;
  49. float maxWidth = 0f;
  50. public void AddLine(TextNode n, Vector2 left, float width, float height, Color color, Vector2 uv, int speed)
  51. {
  52. if (m_Data == null)
  53. {
  54. m_Data = new DrawLineStruct();
  55. maxWidth = 0f;
  56. currentWidth = 0f;
  57. }
  58. maxWidth += width;
  59. m_Data.lines.Add(new DrawLineStruct.Line() { leftPos = left, width = width, height = height, color = color, uv = uv, node = n, dynSpeed = speed });
  60. }
  61. public override void UpdateMaterial(Material mat)
  62. {
  63. base.UpdateMaterial(mat);
  64. rectTransform.SetAsLastSibling();
  65. }
  66. public void Rebuild(CanvasUpdate executing)
  67. {
  68. if (m_Data == null)
  69. return;
  70. if (executing != CanvasUpdate.PreRender)
  71. return;
  72. float width = currentWidth;
  73. #if UNITY_EDITOR
  74. if (!Application.isPlaying)
  75. width = maxWidth;
  76. #endif
  77. VertexHelper vh = Tools.vertexHelper;
  78. vh.Clear();
  79. m_Data.Render(width, vh);
  80. Mesh workerMesh = SymbolText.WorkerMesh;
  81. vh.FillMesh(workerMesh);
  82. canvasRenderer.SetMesh(workerMesh);
  83. }
  84. public override void Release()
  85. {
  86. base.Release();
  87. m_Data = null;
  88. }
  89. public void GraphicUpdateComplete() { }
  90. public bool IsDestroyed() { return this == null; }
  91. public void LayoutComplete() { }
  92. }
  93. }