UnityTextLayer.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace CommonUI_Unity3D.Impl
  5. {
  6. public class UnityTextLayer : CommonUI.Display.TextLayer
  7. {
  8. internal Texture2D mTexture;
  9. private UnityImage mBuffer;
  10. public UnityTextLayer(string text, CommonUI.Display.FontStyle style, float size)
  11. : base(text, (int)size, style)
  12. {
  13. isDirty = true;
  14. }
  15. internal void Refresh()
  16. {
  17. if (this.isDirty)
  18. {
  19. this.isDirty = false;
  20. if (mBuffer != null)
  21. {
  22. mBuffer.Dispose();
  23. mBuffer = null;
  24. mTexture = null;
  25. }
  26. if (string.IsNullOrEmpty(mText))
  27. {
  28. return;
  29. }
  30. int boundW = 0;
  31. int boundH = 0;
  32. mTexture = UnityDriver.Platform.SysFontTexture(
  33. mText,
  34. false,
  35. mFontStyle,
  36. Math.Max(1, mFontSize),
  37. isEnable ? mFontColorRGBA : DefaultDisableTextColorRGBA,
  38. mBorderTime,
  39. mBorderColorRGBA,
  40. mExpectSize,
  41. out boundW,
  42. out boundH);
  43. if (mTexture != null)
  44. {
  45. mBounds.width = boundW;
  46. mBounds.height = boundH;
  47. mBuffer = new UnityImage(mTexture, boundW, boundH, mText);
  48. }
  49. }
  50. }
  51. protected override void Disposing()
  52. {
  53. if (mBuffer != null)
  54. {
  55. mBuffer.Dispose();
  56. }
  57. mTexture = null;
  58. }
  59. public override CommonUI.Display.Image GetBuffer()
  60. {
  61. Refresh();
  62. return mBuffer;
  63. }
  64. }
  65. }