Unity之UI管理器

这月要开始备战期末考试了,尤其是系统解剖,因此没太多时间学习Unity,本次记录UI管理器。

思路记录

首先需要想,我们如何去做UI管理器?根据我对“面向对象编程”的思考,我们需要找到不同的UI的共同点——它们都有一个ShowPanel(显示面板)和HidePanel(隐藏面板)的方法,这是共同性;它们的差异性在于不同UI逻辑不同,那就用抽象方法,根据具体差异书写不同逻辑。
其次,UI管理器只能有一个,那就想到了单例模式。我们把不同UI做成预设体存在Resources文件夹,通过单例模式+泛型实现显示面板、隐藏面板的方法:ShowPanel,ShowPanel,这样就很方便。 于是我们需要写两个基础的脚本,第一个是BasePanel,所有UI面板的基类,并声明为抽象类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Events;

public abstract class BasePanel : MonoBehaviour
{
//声明组件用于全体管理
private CanvasGroup canvasGroup;
//淡入淡出速度
public float alphaSpeed = 10f;
private bool isShow = false;
//面板隐藏后要做的事情
private UnityAction hideCallBack = null;

private void Awake()
{
canvasGroup = GetComponent<CanvasGroup>();
if (canvasGroup == null)
canvasGroup = this.AddComponent<CanvasGroup>();
}
protected virtual void Start()
{
Init();
}

//不同的UI有不同的逻辑,写在Init方法里
protected abstract void Init();

public virtual void Showme()
{
canvasGroup.alpha = 0f;
isShow = true;
}

public virtual void HideMe(UnityAction callback)
{
canvasGroup.alpha = 1.0f;
isShow = false;
hideCallBack = callback;
}
// Update is called once per frame
void Update()
{
if (isShow && canvasGroup.alpha != 1f)
{
canvasGroup.alpha += Time.deltaTime * alphaSpeed;
if (canvasGroup.alpha == 1f)
canvasGroup.alpha = 1f;
}
else if (!isShow && canvasGroup.alpha != 0f)
{
canvasGroup.alpha -= Time.deltaTime * alphaSpeed;
if (canvasGroup.alpha == 0f)
{
canvasGroup.alpha = 0f;
hideCallBack?.Invoke();
}

}
}
}

之后再来写UI管理器具体的逻辑:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System.Collections.Generic;
using Unity.VisualScripting.FullSerializer;
using UnityEngine;

public class UIManager
{
public static UIManager instance = new UIManager();
public static UIManager Instance => instance;
private Transform canvasTrans;

//字典存储
private Dictionary<string,BasePanel> panelDic = new Dictionary<string,BasePanel>();
//显示面板
public T ShowPanel<T>() where T : BasePanel
{
//获取面板名称
string panelName = typeof(T).Name;
//检测是不是有了
if(panelDic.ContainsKey(panelName) )
return panelDic[panelName] as T;
//如果没有我们就实例化一个UI
GameObject panelObj = GameObject.Instantiate(Resources.Load<GameObject>("UI" + panelName));
//把这个对象放到场景Canvas下边
panelObj.transform.SetParent(canvasTrans, false);
//获取这个UI上的脚本
T panel = panelObj.GetComponent<T>();
//把这个脚本存在字典里
panelDic.Add(panelName, panel);
panel.Showme();
return panel;
}
//UIManager的构造函数
public UIManager()
{
//得到场景的Canvas对象
GameObject canvas = GameObject.Instantiate(Resources.Load<GameObject>("UI/Canvas"));
canvasTrans = canvas.transform;
GameObject.DontDestroyOnLoad(canvas);
}

//隐藏面板,isFade用来控制你想不想实现UI淡入淡出效果
public void HidePanel<T>(bool isFade = true)where T : BasePanel
{
string panelName = typeof(T).Name;
if(panelDic.ContainsKey(panelName) )
{
if(isFade)
{
panelDic[panelName].HideMe(() =>
{
GameObject.Destroy(panelDic[panelName].gameObject);
panelDic.Remove(panelName);
});
}
else
{
//删除UI
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;
}
}