2025-06-07 17:43:34 +08:00

180 lines
5.5 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections.Generic;
using UnityEngine;
namespace FairyGUI.Utils
{
/// <summary>
///
/// </summary>
public class HtmlPageContext : IHtmlPageContext
{
Stack<IHtmlObject> _imagePool;
Stack<IHtmlObject> _inputPool;
Stack<IHtmlObject> _buttonPool;
Stack<IHtmlObject> _selectPool;
Stack<IHtmlObject> _linkPool;
static HtmlPageContext _inst;
public static HtmlPageContext inst
{
get
{
if (_inst == null)
_inst = new HtmlPageContext();
return _inst;
}
}
static Transform _poolManager;
#if UNITY_2019_3_OR_NEWER
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
static void InitializeOnLoad()
{
_inst = null;
_poolManager = null;
}
#endif
public HtmlPageContext()
{
_imagePool = new Stack<IHtmlObject>();
_inputPool = new Stack<IHtmlObject>();
_buttonPool = new Stack<IHtmlObject>();
_selectPool = new Stack<IHtmlObject>();
_linkPool = new Stack<IHtmlObject>();
if (Application.isPlaying && _poolManager == null)
_poolManager = Stage.inst.CreatePoolManager("HtmlObjectPool");
}
virtual public IHtmlObject CreateObject(RichTextField owner, HtmlElement element)
{
IHtmlObject ret = null;
bool fromPool = false;
if (element.type == HtmlElementType.Image)
{
if (_imagePool.Count > 0 && _poolManager != null)
{
ret = _imagePool.Pop();
fromPool = true;
}
else
ret = new HtmlImage();
}
else if (element.type == HtmlElementType.Link)
{
if (_linkPool.Count > 0 && _poolManager != null)
{
ret = _linkPool.Pop();
fromPool = true;
}
else
ret = new HtmlLink();
}
else if (element.type == HtmlElementType.Input)
{
string type = element.GetString("type");
if (type != null)
type = type.ToLower();
if (type == "button" || type == "submit")
{
if (_buttonPool.Count > 0 && _poolManager != null)
{
ret = _buttonPool.Pop();
fromPool = true;
}
else
{
if (HtmlButton.resource != null)
ret = new HtmlButton();
else
Debug.LogWarning("FairyGUI: Set HtmlButton.resource first");
}
}
else
{
if (_inputPool.Count > 0 && _poolManager != null)
{
ret = _inputPool.Pop();
fromPool = true;
}
else
ret = new HtmlInput();
}
}
else if (element.type == HtmlElementType.Select)
{
if (_selectPool.Count > 0 && _poolManager != null)
{
ret = _selectPool.Pop();
fromPool = true;
}
else
{
if (HtmlSelect.resource != null)
ret = new HtmlSelect();
else
Debug.LogWarning("FairyGUI: Set HtmlSelect.resource first");
}
}
//Debug.Log("from=" + fromPool);
if (ret != null)
{
//可能已经被GameObject tree deleted了不再使用
if (fromPool && ret.displayObject != null && ret.displayObject.isDisposed)
{
ret.Dispose();
return CreateObject(owner, element);
}
ret.Create(owner, element);
if (ret.displayObject != null)
ret.displayObject.home = owner.cachedTransform;
}
return ret;
}
virtual public void FreeObject(IHtmlObject obj)
{
if (_poolManager == null)
{
obj.Dispose();
return;
}
//可能已经被GameObject tree deleted了不再回收
if (obj.displayObject != null && obj.displayObject.isDisposed)
{
obj.Dispose();
return;
}
obj.Release();
if (obj is HtmlImage)
_imagePool.Push(obj);
else if (obj is HtmlInput)
_inputPool.Push(obj);
else if (obj is HtmlButton)
_buttonPool.Push(obj);
else if (obj is HtmlLink)
_linkPool.Push(obj);
if (obj.displayObject != null)
obj.displayObject.cachedTransform.SetParent(_poolManager, false);
}
virtual public NTexture GetImageTexture(HtmlImage image)
{
return null;
}
virtual public void FreeImageTexture(HtmlImage image, NTexture texture)
{
}
}
}