Unity游戏通用框架——UI的管理和加载

embedded/2024/10/15 20:00:54/

需求:为了方便UI的管理,编写一个管理类,管理所有UI的加载、隐藏或销魂,每个UI都继承自一个UIWindow类,存放在Resource的指定目录下,通过UIManager进行管理。每个继承自UIWindow的UI天然有UI的打开关闭等基本功能。

UIWindow

using UnityEngine;public abstract class UIWindow : MonoBehaviour
{public delegate void CloseHandler(UIWindow sender, WindowResult result);public event CloseHandler OnClose;public UIWindowType WindowType= UIWindowType.Page;public virtual System.Type Type { get { return this.GetType(); } }public GameObject Root;public enum WindowResult{None = 0,Yes,No,}public void Close(WindowResult result = WindowResult.None){UIManager.Instance.Close(this.Type);if (this.OnClose != null)this.OnClose(this, result);this.OnClose = null;}public virtual void OnCloseClick(){this.Close();}public virtual void OnYesClick(){this.Close(WindowResult.Yes);}public virtual void OnNoClick(){this.Close(WindowResult.No);}
}

UIManager

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;public class UIManager : Singleton<UIManager>
{class UIElement{public string Resources;public bool Cache ;public GameObject Instance;}private Dictionary<Type, UIElement> UIResources = new();private Transform UIParent;public void Init( Transform parent = null){UIParent = parent;//UI在这注册UIResources.Add(typeof(UIHomePage), new UIElement() { Resources = "Prefabs/UI/HomePage/UIHomePage", Cache= true });}public T Show<T>() where T : class{Type type = typeof(T);if (UIResources.ContainsKey(type)){UIElement info = UIResources[type];if (info.Instance != null){info.Instance.SetActive(true);}else{if (Resources.Load(info.Resources) == null){return default;}info.Instance = GameObject.Instantiate(ResourcesManager.Instance.LoadAsset<GameObject>(info.Resources));}return info.Instance.GetComponent<T>();}return default;}public void Close(Type type){if (UIResources.ContainsKey(type)){UIElement info = UIResources[type];if (info.Cache){info.Instance.SetActive(false);}else{GameObject.Destroy(info.Instance);info.Instance = null;}}}public void Close<T>(){Close(typeof(T));}public void CloseIfShow<T>(){Type type = typeof(T);if(UIResources.ContainsKey(type)&& UIResources[type].Instance && UIResources[type].Instance.activeSelf == true){Close(typeof(T));}}}

使用

如果要给组件挂上UIHomePage脚本,则给它继承一下UIWindow

public class AddressPageUI : UIWindow
{

然后在上面的UIManager注册函数即可,

UIResources.Add(typeof(UIHomePage), new UIElement() { Resources = "Prefabs/UI/HomePage/UIHomePage", Cache= true });

加载时

UIManager .Instance.Show<AddressPageUI >();

关闭时

UIManager .Instance.Close<AddressPageUI>();

如果UI上有关闭确定等按钮,直接管理基类中的函数即可,无需另外编写函数


http://www.ppmy.cn/embedded/128045.html

相关文章

Python 无法安装pybluez 解决办法

直接使用 pip install pybluez 安装会报错 那么可以尝试手动安装 先下载 pybluez-0.30的包&#xff1a; CSDN&#xff1a; https://download.csdn.net/download/rotion135/89884852 Github&#xff1a;https://github.com/pybluez/pybluez 先安装依赖 sudo apt-get install …

Kafka之生产者

本章内容将整理下Kafka体系结构中的生产者相关的一些知识。 1. 生产者客户端 生产者客户端在Kafka的发展历程当中一共有两个重大版本&#xff1a; 一个是基于Scala语言开发的版本&#xff0c;称为Old Producer或Scala版的生产者客户端。一个是Kafka0.9.x版本之后以Java语言开发…

【微服务】微服务注册:构建灵活的服务管理机制

目录 引言一、什么是微服务注册&#xff1f;1.1 服务注册中心的作用1.2 服务注册中心的工作原理1.3 示意图 二、常见的微服务注册中心2.1 各注册中心详细对比 三、微服务注册的实现方式3.1 Spring Cloud Netflix Eureka3.2 Consul3.3 Zookeeper3.4 etcd 四、微服务注册的注意事…

JavaScript 入门

1. HTML、CSS、JavaScript 之间的关系 HTML&#xff1a;网页的结构&#xff08;骨&#xff09; CSS&#xff1a;网页的表现&#xff08;皮&#xff09; JavaScript&#xff1a;网页的行为&#xff08;魂&#xff09; 2. 引入方式 3种引入方式&#xff0c;语法如下&#xff…

【Unity - 屏幕截图】技术要点

在Unity中想要实现全屏截图或者截取某个对象区域的图片都是可以通过下面的函数进行截取 Texture2D/// <summary>/// <para>Reads the pixels from the current render target (the screen, or a RenderTexture), and writes them to the texture.</para>/…

【unity框架开发起步】一些框架开发思维和工具类封装

文章目录 前言一、Editor操作二、快捷导出unity包三、快捷打开存储目录四、封装概率函数五、方法过时六、partial 关键字&#xff0c;拆开合并类七、从数组中随机取⼀个数值并进⾏返回1、实现2、object 类优化3、泛型&#xff0c;结构复⽤利器4、params 关键字优化 八、abstrac…

产品经理或项目经理考PMP,薪资会不会提高?

你要知道一个事情&#xff0c;薪资和证书不是划等号的。没有公司会为你多一个证书而去给你涨薪&#xff0c;人家也不是傻子。想要提高薪资还得看公司的情况&#xff0c;还有你在公司岗位的价值&#xff0c;自己的能力以及工作上做出的成绩&#xff0c;是跟这些挂钩的。找一家前…

成都爱尔李晓峰主任讲解“寒”已至,眼需“养”

温度逐渐走低&#xff0c;寒冷空气的到来带走夏季闷热潮湿&#xff0c;也带走了空气中的水分&#xff0c;环境变得干燥&#xff0c;眼睛水分蒸发加快&#xff0c;十分容易造成眼部不适&#xff0c;干眼患者尤其需要注意&#xff01; 有干眼问题的患者&#xff0c;在这样的天气下…