_xiaofang/xiaofang/Assets/Res/gsj/scripts/UIManager.cs

117 lines
2.7 KiB
C#
Raw Normal View History

2024-12-20 10:10:19 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UIManager
{
private Transform canvasTf;//画布的变换组件
private List<GameObject> uiList;//存储加载过的界面的集合
public void Init()
{
//找世界中的画布
2024-12-26 03:46:07 +08:00
canvasTf = GameObject.Find("HostCanvas").transform;
2024-12-20 10:10:19 +08:00
//初始化集合
uiList = new List<GameObject>();
}
//显示
public T ShowUI<T>(string uiName) where T : Component
{
2024-12-28 21:42:16 +08:00
Debug.LogError(1111111111111);
2024-12-20 10:10:19 +08:00
T ui = Find<T>(uiName);
if (ui == null)
{
//集合中没有 需要从Resources/UI文件夹中加载
GameObject obj = Object.Instantiate(Resources.Load("gsjUI/" + uiName), canvasTf) as GameObject;
2024-12-28 21:42:16 +08:00
obj.transform.SetParent(canvasTf);
2024-12-20 10:10:19 +08:00
//改名字
obj.name = uiName;
2024-12-28 21:42:16 +08:00
Debug.LogError(1111111111111);
2024-12-20 10:10:19 +08:00
//添加需要的脚本
//ui = obj.AddComponent<T>();
//添加到集合进行存储
uiList.Add(obj);
}
else
{
2024-12-28 21:42:16 +08:00
Debug.LogError(1111111111111);
2024-12-20 10:10:19 +08:00
//显示
ui.gameObject.SetActive(true);
}
return ui;
}
//隐藏
public void HideUI(string uiName)
{
GameObject ui = Find(uiName);
if (ui != null)
{
ui.SetActive(true);
}
}
//关闭所有界面
public void CloseAllUI()
{
for (int i = uiList.Count - 1; i >= 0; i--)
{
Object.Destroy(uiList[i].gameObject);
}
uiList.Clear();//清空集合
}
//关闭某个界面
public void CloseUI(string uiName)
{
GameObject ui = Find(uiName);
if (ui != null)
{
uiList.Remove(ui);
Object.Destroy(ui.gameObject);
}
}
//从集合中找到名字对应的界面脚本
public T Find<T>(string uiName) where T : Component
{
for (int i = 0; i < uiList.Count; i++)
{
if (uiList[i].name == uiName)
{
return uiList[i].GetComponent<T>();
}
}
return null;
}
public GameObject Find(string uiName)
{
for (int i = 0; i < uiList.Count; i++)
{
if (uiList[i].name == uiName)
{
return uiList[i];
}
}
return null;
}
//获得某个界面的脚本
public T GetUI<T>(string uiName) where T : Component
{
GameObject ui = Find(uiName);
if (ui != null)
{
return ui.GetComponent<T>();
}
return null;
}
}