Files
JJBB/Assets/Project/Script/LuaScripts/UI/LuaUIManager.cs
2024-08-23 15:49:34 +08:00

223 lines
7.1 KiB
C#
Raw 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;
using System.Collections.Generic;
using Module.Log;
using UnityEngine;
using XLua;
public class LuaUIManager
{
[CSharpCallLua]
public delegate void OnOpenUIDelegate(bool bSuccess, object param, GameObject uiObj);
private static LuaUIManager _Instance;
public Dictionary<string, GameObject> LuaUIs = new Dictionary<string, GameObject>();
public Dictionary<string, GameObject> ShowingUIs = new Dictionary<string, GameObject>();
public static LuaUIManager Instance
{
get
{
if (_Instance == null)
_Instance = new LuaUIManager();
return _Instance;
}
}
public GameObject GetUIInstance(string uiName)
{
if (LuaUIs.ContainsKey(uiName))
return LuaUIs[uiName];
return null;
}
#region load ui
public enum UIContainerType
{
Base = 1,
Pop = 4,
MenuPop = 5,
Tip = 6,
Message = 8
}
public void ShowLuaUI(string uiName, OnOpenUIDelegate callBack = null, object param = null,
bool defaultHide = false)
{
ShowLuaUIType(uiName, UIContainerType.MenuPop, callBack, param, defaultHide);
}
public void ShowLuaUIType(string uiName, UIContainerType uiType, OnOpenUIDelegate callBack = null,
object param = null, bool defaultHide = false)
{
if (LuaUIs.ContainsKey(uiName))
{
if (!defaultHide)
{
LuaUIs[uiName].gameObject.SetActive(true);
LuaUIs[uiName].gameObject.transform.SetAsLastSibling();
}
if (callBack != null)
callBack(true, param, LuaUIs[uiName].gameObject);
}
else
{
var hash = new Hashtable
{
{"UIType", uiType}, {"OnOpenUIDelegate", callBack}, {"Param", param}, {"DefaultHide", defaultHide}
};
LoadAssetBundle.Instance.LoadUI(LoadAssetBundle.BUNDLE_PATH_UI + uiName, uiName, LoadUICallBack, hash);
}
//UIManager.Instance().ShowBlurBackGround();
}
public void ShowLuaUIAsChild(string uiName, GameObject parent, OnOpenUIDelegate callBack = null,
object param = null, bool defaultHide = false)
{
var hash = new Hashtable
{
{"Parent", parent}, {"OnOpenUIDelegate", callBack}, {"Param", param}, {"DefaultHide", defaultHide}
};
if (LuaUIs.ContainsKey(uiName))
{
if (parent != null)
{
LuaUIs[uiName].transform.SetParent(parent.transform);
LuaUIs[uiName].transform.localPosition = Vector3.zero;
LuaUIs[uiName].transform.localScale = Vector3.one;
LuaUIs[uiName].GetComponent<RectTransform>().offsetMax = Vector2.zero;
LuaUIs[uiName].GetComponent<RectTransform>().offsetMin = Vector2.zero;
}
if (!defaultHide) LuaUIs[uiName].gameObject.SetActive(true);
if (callBack != null)
callBack(true, param, LuaUIs[uiName].gameObject);
}
else
{
LoadAssetBundle.Instance.LoadUI(LoadAssetBundle.BUNDLE_PATH_UI + uiName, uiName, LoadUICallBack, hash);
}
}
// 因为会存先加载的资源比后加载的资源慢,所以提供取消回调,保证加载顺序关系
public void CancelLoadUICallBack(string uiName)
{
LoadAssetBundle.Instance.UnloadAssetWithPathFix(LoadAssetBundle.BUNDLE_PATH_UI, uiName);
}
private void LoadUICallBack(string uiName, GameObject resObj, Hashtable hashParam)
{
GameObject newUI;
var defaultHide = (bool) hashParam["DefaultHide"];
if (hashParam.ContainsKey("Parent"))
{
var parent = (GameObject) hashParam["Parent"];
newUI = UIManager.InitUIFromPrefab(resObj, parent.transform, -1, defaultHide);
newUI.name = uiName;
if (!LuaUIs.ContainsKey(uiName))
LuaUIs.Add(uiName, newUI);
LogModule.DebugLog("init ui in parent:" + uiName);
}
else
{
var parent = UIManager.Instance().PopUIRoot;
var uiTypeIndex = -1;
if (hashParam.ContainsKey("UIType"))
{
var uiType = (UIContainerType) hashParam["UIType"];
uiTypeIndex = (int) uiType;
switch (uiType)
{
case UIContainerType.Base:
parent = UIManager.Instance().BaseUIRoot;
break;
case UIContainerType.Pop:
parent = UIManager.Instance().PopUIRoot;
break;
case UIContainerType.MenuPop:
parent = UIManager.Instance().MenuPopUIRoot;
break;
case UIContainerType.Tip:
parent = UIManager.Instance().TipUIRoot;
break;
case UIContainerType.Message:
parent = UIManager.Instance().MessageUIRoot;
break;
}
}
newUI = UIManager.InitUIFromPrefab(resObj, parent, uiTypeIndex, defaultHide);
LuaUIs.Add(uiName, newUI);
ShowingUIs.Add(uiName, newUI);
}
var delOpenUI = hashParam["OnOpenUIDelegate"] as OnOpenUIDelegate;
if (delOpenUI != null)
delOpenUI(newUI != null, hashParam["Param"], newUI);
}
public void CloseUI(string uiName)
{
if (LuaUIs.ContainsKey(uiName)) LuaUIs[uiName].SetActive(false);
//UIManager.Instance().HideBlurBackGround();
}
public void CloseAnyUI()
{
foreach (var luaUI in ShowingUIs)
if (luaUI.Value.activeSelf)
if (luaUI.Value.transform.parent != UIManager.Instance().BaseUIRoot)
CloseUI(luaUI.Key);
}
public void ClearAllUI()
{
foreach (var luaUI in LuaUIs)
if (luaUI.Value)
Object.Destroy(luaUI.Value);
LuaUIs.Clear();
ShowingUIs.Clear();
}
public void ClearAllPop(bool closeAlways = false)
{
var popUI = new List<string>();
foreach (var luaUI in LuaUIs)
{
//ui 为空直接删除某些UI由UIManager打开但是包含LuaUI的Page可能导致此情况
if (luaUI.Value == null)
popUI.Add(luaUI.Key);
if (!closeAlways && luaUI.Value.activeInHierarchy)
continue;
if (luaUI.Value.transform.parent != UIManager.Instance().BaseUIRoot) popUI.Add(luaUI.Key);
}
for (var i = 0; i < popUI.Count; ++i)
{
if (LuaUIs[popUI[i]] != null) Object.Destroy(LuaUIs[popUI[i]]);
LuaUIs.Remove(popUI[i]);
if (ShowingUIs.ContainsKey(popUI[i])) ShowingUIs.Remove(popUI[i]);
}
}
public void OnDestroyCallBack(List<string> uiNameList)
{
for (var index = 0; index < uiNameList.Count; index++)
if (LuaUIs.ContainsKey(uiNameList[index]))
LuaUIs.Remove(uiNameList[index]);
}
#endregion
}