当前位置: 首页 > news >正文

【Unity】一个UI框架例子

使用框架前置条件:调整脚本运行顺序, Canvas挂载UIManager, Panel挂载对应的UIController、UI控件挂载UIControl。

UIManager:UI管理器,用于处理和管理各个UIController和UIControl的业务逻辑,挂载在Canvas上;

UIController:界面层Controller,用于管理该界面下的所有控件信息;

UIControl:控件层Control,用于管理对应的组件信息。

UIManager:

public class UIManager3 : MonoBehaviour

{

private static UIManager3 instance;

public static UIManager3 Instance { get => instance; }

private Dictionary<string, UIController3> uiControllerList = new();

public virtual void Awake()

{

instance = this;

}

public virtual void OnDestroy()

{

uiControllerList.Clear();

Destroy(this);

}

public void AddToList(string uiControllerName, UIController3 uiController3)

{

if (!uiControllerList.ContainsKey(uiControllerName))

uiControllerList.Add(uiControllerName, uiController3);

}

public void LogoutUIController(string name)

{

if (uiControllerList.ContainsKey(name))

uiControllerList.Remove(name);

}

public void SetControllerActive(string uiControllerName, bool active)

{

foreach(Transform trans in transform)

{

if (trans.name == uiControllerName) trans.gameObject.SetActive(active);

}

}

public UIController3 GetUIController(string name)

{

if (uiControllerList.TryGetValue(name, out UIController3 uiController3))

return uiController3;

return null;

}

public UIControl3 GetUIControl(string controllerName, string controlName)

{

if (uiControllerList.TryGetValue(controllerName, out UIController3 uiController3))

return uiController3.GetUIControl(controlName);

return null;

}

}

UIController:

public abstract class UIController3 : MonoBehaviour

{

private Dictionary<string, UIControl3> uiControlList = new();

public virtual void Start()

{

UIManager3.Instance.AddToList(this.name, this);

foreach(Transform trans in transform)

{

if (trans.gameObject.GetComponent<UIControl>() != null) Destroy(trans.gameObject.GetComponent<UIControl>());

trans.gameObject.AddComponent<UIControl3>();

}

}

public virtual void OnDestroy()

{

UIManager3.Instance.LogoutUIController(this.name);

Destroy(this);

}

public UIControl3 GetUIControl(string name)

{

if (uiControlList.TryGetValue(name, out UIControl3 uiControl3))

return uiControl3;

return null;

}

public void LogoutUIControl(string name)

{

if (uiControlList.ContainsKey(name))

uiControlList.Remove(name);

}

}

UIControl:

public class UIControl3 : MonoBehaviour

{

private UIController3 parentController;

public virtual void Start()

{

if (transform.parent.GetComponentInParent<UIController3>() != null) parentController = transform.parent.GetComponentInParent<UIController3>();

}

public virtual void OnDestroy()

{

parentController.LogoutUIControl(this.name);

Destroy(this);

}

public void SetText(string txt)

{

if (GetComponent<Text>() != null)

GetComponent<Text>().text = txt;

}

public void SetButtonOnClick(UnityAction unityAction)

{

if (GetComponent<Button>() != null)

GetComponent<Button>().onClick.AddListener(unityAction);

}

public void SetSliderEvent(UnityAction<float> unityAction)

{

if (GetComponent<Slider>() != null)

GetComponent<Slider>().onValueChanged.AddListener(unityAction);//unityAction(float), float为Slider改变后的value值

}

public void SetImage(Sprite sprite)

{

if (GetComponent<Image>() != null)

GetComponent<Image>().sprite = sprite;

}

public void SetInpueFieldEvent(UnityAction<string> unityAction)

{

if (GetComponent<InputField>() != null)

GetComponent<InputField>().onValueChanged.AddListener(unityAction);//unityAction(string), string为InputField的整个文本

}

}

LoginPanel(GameObject名称为“Login_Panel”):

public class LoginPanel3 : UIController3

{

public Button UI_LoginBtn;

public override void Start()

{

base.Start();

UI_LoginBtn.GetComponent<Button>().onClick.AddListener(UILoginBtnOnClick);

}

private void UILoginBtnOnClick()

{

UIManager3.Instance.SetControllerActive(this.name, false);

UIManager3.Instance.SetControllerActive("Main_Panel", true);

}

}

MainPanel(GameObject名称为“Main_Panel”):

public class MainPanel3 : UIController3

{

public Button UI_ToMenuBtn;

public Button UI_LogoutBtn;

public override void Start()

{

base.Start();

UI_ToMenuBtn.GetComponent<UIControl3>().SetButtonOnClick(UITOMenuBtnOnClick);

UI_LogoutBtn.GetComponent<UIControl3>().SetButtonOnClick(UILogoutBtnOnClick);

}

private void UITOMenuBtnOnClick()

{

UIManager3.Instance.SetControllerActive(this.name, false);

UIManager3.Instance.SetControllerActive("Menu_Panel", true);

}

private void UILogoutBtnOnClick()

{

UIManager3.Instance.SetControllerActive(this.name, false);

UIManager3.Instance.SetControllerActive("Login_Panel", true);

}

}

MenuPanel(GameObject名称为“Menu_Panel”):

public class MenuPanel3 : UIController3

{

public Button UI_TOMainBtn;

public override void Start()

{

base.Start();

UI_TOMainBtn.GetComponent<UIControl3>().SetButtonOnClick(UITOMainBtnOnClick);

}

private void UITOMainBtnOnClick()

{

UIManager3.Instance.SetControllerActive(this.name, false);

UIManager3.Instance.SetControllerActive("Main_Panel", true);

}

}

相关文章:

  • 【C到Java的深度跃迁:从指针到对象,从过程到生态】第五模块·生态征服篇 —— 第十九章 Spring生态:从main函数到企业级开发
  • Spring IoC容器的设计与实现
  • 数字智慧方案6158丨智慧医疗解决方案精华版(58页PPT)(文末有下载方式)
  • 【音频】Qt6实现MP3播放器
  • Seata服务端同步提交事务核心源码解析
  • 【音频】基础知识
  • AI数字人系统开发:技术架构、应用场景与未来趋势
  • 西式烹饪实训室建设路径
  • 图论---有向图的强连通分量(Tarjan求SCC)
  • 内存安全的攻防战:工具链与语言特性的协同突围
  • 【docker学习笔记】如何删除镜像启动默认命令
  • Spring AI开发跃迁指南(第二章:急速上手3——Advisor核心原理、源码讲解及使用实例)
  • 多线程系列二:Thread类
  • 安装linux下的idea
  • Git 基本操作(一)
  • ARM ASM
  • watch 数组 Vue 3
  • 【AI提示词】决策树专家
  • 【Linux网络】I/O多路转接技术 - poll
  • Dagster资产工厂实战:从Python到YAML配置的高效ETL流程
  • 从“长绳系日”特展看韩天衡求艺之路
  • 图忆|上海车展40年:中国人的梦中情车有哪些变化(上)
  • 新能源车盈利拐点:8家上市车企去年合计净利854亿元,多家扭亏
  • 司法服务保障西部陆海新通道建设,最高法专门发文
  • 制定出台民营经济促进法有何重大意义?全国人大常委会法工委回应
  • 白玉兰奖征片综述丨国产剧集创作的此消彼长