1、代码
public class UIManager
{
private static UIManager instance = new UIManager();
public static UIManager Instance => instance;
//存储显示着的面板脚本(不是面板Gameobject),每显示一个面板就存入字典
//隐藏的时候获取字典中对应面板 进行隐藏
private Dictionary<string, BasePanel> panelDic = new Dictionary<string, BasePanel>();
//得到Canvas用于设置为面板父对象
private Transform canvasTrans;
private UIManager()
{
//将Canvas作为预设体,UI管理器启动时创建Canvas
GameObject canvas = GameObject.Instantiate(Resources.Load<GameObject>("UI/Canvas"));
//得到创建的canvas
canvasTrans = canvas.transform;
//不移除Canvas,保证整个游戏中只有一个
GameObject.DontDestroyOnLoad(canvas);
}
//显示面板
public T ShowPanel<T>() where T:BasePanel
{
//保证泛型T的类型和预设体名字一样(规则)
string panelName = typeof(T).Name;
//判断字典中是否已经有这个面板
if (panelDic.ContainsKey(panelName))
{
return panelDic[panelName] as T;
}
//如果字典中没有
//根据面板名字动态创建预设体 设置父对象
GameObject panelObj = GameObject.Instantiate(Resources.Load<GameObject>("UI/" + panelName));
//把这个对象放到场景Canvas下面
panelObj.transform.SetParent(canvasTrans,false);
//
T panel = panelObj.GetComponent<T>();
//把这个面板脚本 存储到字典中
panelDic.Add(panelName, panel);
panel.ShowMe();
return panel;
}
/// <summary>
/// 隐藏面板
/// </summary>
/// <typeparam name="T">面板类型</typeparam>
public void HidePanel<T>() where T : BasePanel
{
//根据泛型得名字
string panelName = typeof(T).Name;
//判断当前显示的面板有没有传入的面板
if (panelDic.ContainsKey(panelName))
{
//面板淡出完毕后删除
panelDic[panelName].HideMe(()=>
{
GameObject.Destroy(panelDic[panelName].gameObject);
//删除字典里面存储的面板脚本
panelDic.Remove(panelName);
});
}
}
//得到面板
public T GetPanel<T>() where T : BasePanel
{
string panelName = typeof(T).Name;
if (panelDic.ContainsKey(panelName))
{
return panelDic[panelName] as T;
}
//没有对应面板返回空
return null;
}
}
2、如何使用
创建一个测试的UI面板(随便乱拼),将他作为预设体
创建一个测试类
public class TestMain : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
UIManager.Instance.ShowPanel<TestPanel>();
}
// Update is called once per frame
void Update()
{
}
}
启动发现已经动态创建了Canvas和对应面板文章来源:https://www.toymoban.com/news/detail-708391.html
文章来源地址https://www.toymoban.com/news/detail-708391.html
到了这里,关于Unity的UI管理器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!