1
This commit is contained in:
90
unity/Assets/Script/Managers/UIManager.cs
Normal file
90
unity/Assets/Script/Managers/UIManager.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class UIManager : Singleton<UIManager>
|
||||
{
|
||||
class UIElement
|
||||
{
|
||||
public string Resources; //资源路径
|
||||
public bool Cache; //是否启用
|
||||
public GameObject Instance; //资源预制体
|
||||
}
|
||||
private Dictionary<Type, UIElement> UIResources = new Dictionary<Type, UIElement>(); //存放
|
||||
public UIManager()
|
||||
{
|
||||
this.UIResources.Add(typeof(UILoad), new UIElement() { Resources = "UI/UILoad", Cache = false });
|
||||
this.UIResources.Add(typeof(UISetting), new UIElement() { Resources = "UI/UISetting", Cache = false });
|
||||
this.UIResources.Add(typeof(UIGameOver), new UIElement() { Resources = "UI/GameOver", Cache = false });
|
||||
this.UIResources.Add(typeof(UILottery), new UIElement() { Resources = "UI/LotterySystem", Cache = false });
|
||||
this.UIResources.Add(typeof(UIEnterScene), new UIElement() { Resources = "UI/EnterScene", Cache = false });
|
||||
this.UIResources.Add(typeof(UIOpen), new UIElement() { Resources = "UI/UIOpen", Cache = false });
|
||||
this.UIResources.Add(typeof(EndUI), new UIElement() { Resources = "UI/EndUI", Cache = false });
|
||||
this.UIResources.Add(typeof(UIBagInfo), new UIElement() { Resources = "UI/BagUI", Cache = false });
|
||||
this.UIResources.Add(typeof(UILevelUp), new UIElement() { Resources = "UI/UILevelUp", Cache = false });
|
||||
|
||||
this.UIResources.Add(typeof(MessageBox), new UIElement() { Resources = "UI/MessageBox", Cache = false });
|
||||
|
||||
this.UIResources.Add(typeof(UISaveDate), new UIElement() { Resources = "UI/UISave", Cache = false });
|
||||
|
||||
}
|
||||
~UIManager()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载UI
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public T Show<T>()
|
||||
{
|
||||
Type type=typeof(T);
|
||||
if(this.UIResources.ContainsKey(type))
|
||||
{
|
||||
UIElement info = UIResources[type];
|
||||
if(info.Instance!=null)
|
||||
{
|
||||
info.Instance.SetActive(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
UnityEngine.Object prefab = Resources.Load(info.Resources);
|
||||
if (prefab == null)
|
||||
{
|
||||
return default(T);
|
||||
}
|
||||
info.Instance = (GameObject)GameObject.Instantiate(prefab);
|
||||
}
|
||||
return info.Instance.GetComponent<T>();
|
||||
}
|
||||
return default(T);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 关闭UI
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
public void Close(Type type)
|
||||
{
|
||||
if (this.UIResources.ContainsKey(type))
|
||||
{
|
||||
UIElement info = this.UIResources[type];
|
||||
if (info.Cache)
|
||||
{
|
||||
info.Instance.SetActive(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
GameObject.Destroy(info.Instance);
|
||||
info.Instance = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
public void Close<T>()
|
||||
{
|
||||
this.Close(typeof(T));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user