HtmlPageContext.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace FairyGUI.Utils
  4. {
  5. /// <summary>
  6. ///
  7. /// </summary>
  8. public class HtmlPageContext : IHtmlPageContext
  9. {
  10. Stack<IHtmlObject> _imagePool;
  11. Stack<IHtmlObject> _inputPool;
  12. Stack<IHtmlObject> _buttonPool;
  13. Stack<IHtmlObject> _selectPool;
  14. Stack<IHtmlObject> _linkPool;
  15. public static HtmlPageContext inst = new HtmlPageContext();
  16. static Transform _poolManager;
  17. public HtmlPageContext()
  18. {
  19. _imagePool = new Stack<IHtmlObject>();
  20. _inputPool = new Stack<IHtmlObject>();
  21. _buttonPool = new Stack<IHtmlObject>();
  22. _selectPool = new Stack<IHtmlObject>();
  23. _linkPool = new Stack<IHtmlObject>();
  24. if (Application.isPlaying && _poolManager == null)
  25. _poolManager = Stage.inst.CreatePoolManager("HtmlObjectPool");
  26. }
  27. virtual public IHtmlObject CreateObject(RichTextField owner, HtmlElement element)
  28. {
  29. IHtmlObject ret = null;
  30. bool fromPool = false;
  31. if (element.type == HtmlElementType.Image)
  32. {
  33. if (_imagePool.Count > 0 && _poolManager != null)
  34. {
  35. ret = _imagePool.Pop();
  36. fromPool = true;
  37. }
  38. else
  39. ret = new HtmlImage();
  40. }
  41. else if (element.type == HtmlElementType.Link)
  42. {
  43. if (_linkPool.Count > 0 && _poolManager != null)
  44. {
  45. ret = _linkPool.Pop();
  46. fromPool = true;
  47. }
  48. else
  49. ret = new HtmlLink();
  50. }
  51. else if (element.type == HtmlElementType.Input)
  52. {
  53. string type = element.GetString("type");
  54. if (type != null)
  55. type = type.ToLower();
  56. if (type == "button" || type == "submit")
  57. {
  58. if (_buttonPool.Count > 0 && _poolManager != null)
  59. {
  60. ret = _buttonPool.Pop();
  61. fromPool = true;
  62. }
  63. else
  64. ret = new HtmlButton();
  65. }
  66. else
  67. {
  68. if (_inputPool.Count > 0 && _poolManager != null)
  69. {
  70. ret = _inputPool.Pop();
  71. fromPool = true;
  72. }
  73. else
  74. ret = new HtmlInput();
  75. }
  76. }
  77. else if (element.type == HtmlElementType.Select)
  78. {
  79. if (_selectPool.Count > 0 && _poolManager != null)
  80. {
  81. ret = _selectPool.Pop();
  82. fromPool = true;
  83. }
  84. else
  85. ret = new HtmlSelect();
  86. }
  87. //Debug.Log("from=" + fromPool);
  88. if (ret != null)
  89. {
  90. //可能已经被GameObject tree deleted了,不再使用
  91. if (fromPool && ret.displayObject != null && ret.displayObject.isDisposed)
  92. {
  93. ret.Dispose();
  94. return CreateObject(owner, element);
  95. }
  96. ret.Create(owner, element);
  97. if (ret.displayObject != null)
  98. ret.displayObject.home = owner.cachedTransform;
  99. }
  100. return ret;
  101. }
  102. virtual public void FreeObject(IHtmlObject obj)
  103. {
  104. if (_poolManager == null)
  105. {
  106. obj.Dispose();
  107. return;
  108. }
  109. //可能已经被GameObject tree deleted了,不再回收
  110. if (obj.displayObject != null && obj.displayObject.isDisposed)
  111. {
  112. obj.Dispose();
  113. return;
  114. }
  115. obj.Release();
  116. if (obj is HtmlImage)
  117. _imagePool.Push(obj);
  118. else if (obj is HtmlInput)
  119. _inputPool.Push(obj);
  120. else if (obj is HtmlButton)
  121. _buttonPool.Push(obj);
  122. else if (obj is HtmlLink)
  123. _linkPool.Push(obj);
  124. if (obj.displayObject != null)
  125. obj.displayObject.cachedTransform.SetParent(_poolManager, false);
  126. }
  127. virtual public NTexture GetImageTexture(HtmlImage image)
  128. {
  129. return null;
  130. }
  131. virtual public void FreeImageTexture(HtmlImage image, NTexture texture)
  132. {
  133. }
  134. }
  135. }