【Unity笔记01】基于单例模式的简单UI框架
单例模式的UIManager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class UIManager
{private static UIManager _instance;public Dictionary<string, string> pathDict;public Dictionary<string, GameObject> prefabDict;public Dictionary<string, BasePanel> panelDict;private Transform _uiRoot;//单例模式public static UIManager Instance{get{if( _instance == null ){_instance = new UIManager();}return _instance;}}private UIManager(){Init();}private void Init(){prefabDict = new Dictionary<string, GameObject>();panelDict = new Dictionary<string, BasePanel>();pathDict = new Dictionary<string, string>(){{UIConst.Menu,"Prefabs/Pop/Menu" },{UIConst.SettingPop,"Prefabs/Pop/SettingPop" },{UIConst.OtherPop,"Prefabs/Pop/OtherPop" }};}public Transform UIRoot{get{if( _uiRoot == null ){_uiRoot = GameObject.Find("Canvas").transform;}return _uiRoot;}}public BasePanel OpenPanel(string name, bool isOnly = true){//如果要只显示一个弹窗的话if(isOnly){CloseAllPops(name);}BasePanel panel = null;if(panelDict.TryGetValue(name, out panel)){Debug.Log("界面已经打开过" + name);panel.OpenPanel(name);return null;}string path = "";if( !pathDict.TryGetValue(name, out path) ){Debug.Log("路径错误"+path);return null;}GameObject panelPrefab = null;if(!panelDict.TryGetValue(name, out panel)){string realPath =path;panelPrefab=Resources.Load<GameObject>(realPath);prefabDict.Add(name, panelPrefab);}//openGameObject panelObject = GameObject.Instantiate(panelPrefab, UIRoot, false);Debug.Log(panelObject.name);panel= panelObject.GetComponent<BasePanel>();if( panel == null ){Debug.Log("没有脚本哦");}panelDict.Add(name,panel);panel.OpenPanel(name);return panel;}public bool ClosePanel(string name){BasePanel panel= null;if(! panelDict.TryGetValue(name,out panel)){Debug.Log("界面未打开");return false;}panel.ClosePanel();return true;}public void CloseAllPops(string name){foreach(var pair in panelDict){if (pair.Key == name) continue;pair.Value.ClosePanel();}}
}
面板的基类
控制面板的打开和关闭,带动画
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;public class BasePanel : MonoBehaviour
{private bool isRemove = false;private string name;public virtual void OpenPanel(string name){this.name = name;gameObject.SetActive(true);OpenAnimation(gameObject);}public virtual void ClosePanel(){isRemove = true;CloseAnimation(gameObject).OnComplete(() =>{gameObject.SetActive(false);});//gameObject.SetActive(false);}public void OpenAnimation(GameObject gameObject){//用dotween 从0%放大到105% 再缩小到95% 再到100%//初始缩放为 0gameObject.transform.localScale = Vector3.zero;//创建动画序列Sequence scaleSequence = DOTween.Sequence();// 添加动画步骤scaleSequence.Append(gameObject.transform.DOScale(1.05f, 0.3f)); // 放大到 105%scaleSequence.Append(gameObject.transform.DOScale(0.95f, 0.3f)); // 缩小到 95%scaleSequence.Append(gameObject.transform.DOScale(1.0f, 0.3f)); // 回到 100%// 缓动scaleSequence.SetEase(Ease.OutBack);}public Tween CloseAnimation(GameObject gameObject){Sequence scaleSequence = DOTween.Sequence();scaleSequence.Append(gameObject.transform.DOScale(0f, 0.2f));return scaleSequence;}
}
常量
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class UIConst
{public const string Menu = "Menu";public const string SettingPop = "SettingPop";public const string OtherPop = "OtherPop";
}
使用
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class MenuPanel : BasePanel
{public void OpenNest(){UIManager.Instance.OpenPanel(UIConst.OtherPop);}public void Close(){UIManager.Instance.ClosePanel(UIConst.Menu);}
}