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

Unity进阶-ui框架学习笔记

文章目录

  • Unity进阶-ui框架学习笔记

Unity进阶-ui框架学习笔记

笔记来源课程:https://study.163.com/course/courseMain.htm?courseId=1212756805&_trace_c_p_k2_=8c8d7393c43b400d89ae94ab037586fc

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qvYhS3e7-1690019993960)(QQ%E6%88%AA%E5%9B%BE20230722085931.png)]

  • 最上面的管理层(canvas)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-x9NEL12X-1690019993961)(QQ%E6%88%AA%E5%9B%BE20230722093841.png)]

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class UImanager : MyrSingletonBase<UImanager>
{//下层控制器的字典public Dictionary<string, UIController> UIControllerDic = new Dictionary<string, UIController>();void Start(){}//设置页面激活状态public void SetActive(string controllerName, bool active){transform.Find(controllerName).gameObject.SetActive(active);}//获取页面上的子控件public UIControl GetUIControl(string controllerName, string controlName){//这个字典里是否存在该名称的组件if (UIControllerDic.ContainsKey(controllerName)) {//它下面的字典里是否存在对应组件if (UIControllerDic[controllerName].UIControlDic.ContainsKey(controlName)) {return UIControllerDic[controllerName].UIControlDic[controlName];}}return null;}}

调整下运行顺序,让他快于controller

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-L7BTY0Xp-1690019993962)(../AppData/Roaming/Typora/typora-user-images/image-20230722171921737.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-smOfAPvE-1690019993962)(../AppData/Roaming/Typora/typora-user-images/image-20230722171854565.png)]

  • panel的控制层

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uukx7lHB-1690019993964)(QQ%E6%88%AA%E5%9B%BE20230722094215.png)]

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class UIController : MonoBehaviour
{//下层控制器的字典public Dictionary<string, UIControl> UIControlDic = new Dictionary<string, UIControl>();void Awake() {//添加到UI控制器的字典里UImanager.Instance.UIControllerDic.Add(transform.name, this);//给子控件加上UIcontrol脚本foreach (Transform tran in transform) {if (tran.gameObject.GetComponent<UIControl>() == null) {tran.gameObject.AddComponent<UIControl>();}}}}
  • panel下面的组件层

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qaDs5oQV-1690019993965)(QQ%E6%88%AA%E5%9B%BE20230722094906.png)]

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;public class UIControl : MonoBehaviour
{//父控制器public UIController controller;private void Awake() {//将自身添加到父级控制器上if (transform.parent != null) {controller = transform.GetComponentInParent<UIController>();if (controller != null) {controller.UIControlDic.Add(transform.name, this);}} }///<summary>/// 各个组件对应的函数///</summary>//更改文本public void ChangetText(string str) {if (GetComponent<Text>() != null) {GetComponent<Text>().text = str;}}//更改图片public void ChangeImage(Sprite sprite) {if(GetComponent<Image>() != null) {GetComponent<Image>().sprite = sprite;}}//输入public void AddInputFieldEvent(UnityAction<string> action){InputField control = GetComponent<InputField>();if (control != null) {control.onValueChanged.AddListener(action);}}//Sliderpublic void AddSliderEvent(UnityAction<float> action){Slider control = GetComponent<Slider>();if (control != null) {control.onValueChanged.AddListener(action);}}//Buttonpublic void AddButtonClickEvent(UnityAction action) {Button control = GetComponent<Button>();if (control != null) {control.onClick.AddListener(action);}}
}
  • 使用

    UImanager.Instance.GetUIControl("score", "scores").ChangetText("分数:" +  score);
    
http://www.lryc.cn/news/96791.html

相关文章:

  • Django实现接口自动化平台(十四)测试用例模块Testcases序列化器及视图【持续更新中】
  • 如何高效实现文件传输:小文件采用零拷贝、大文件采用异步io+直接io
  • Docker运行MySQL5.7
  • -jar和 javaagent命令冲突吗?
  • LLC和MAC子层的应用
  • 【MySQL】之复合查询
  • Vue系列第五篇:Vue2(Element UI) + Go(gin框架) + nginx开发登录页面及其校验登录功能
  • u盘里的数据丢失怎么恢复 u盘数据丢失怎么恢复
  • Mysql-约束
  • 数据结构问答7
  • [Spark] 大纲
  • 【NLP】使用 Keras 保存和加载深度学习模型
  • 视频标注是什么?和图像数据标注的区别?
  • 【Android知识笔记】UI体系(一)
  • SpringBoot 整合Docker Compose
  • SpringBoot整合Elasticsearch
  • 【R3F】0.9添加 shadow
  • 【JavaEE初阶】HTTP请求的构造及HTTPS
  • 探索和实践:基于Python的TD-PSOLA语音处理算法应用与优化
  • Linux 下centos 查看 -std 是否支持 C17
  • 【算法训练营】字符串转成整数
  • 入局元宇宙,所谓的无限可能到底在哪里?
  • 为什么 SSH(安全终端)的端口号是 22 !!
  • k8s Label 2
  • layui踩坑记录之form表单下的button按钮默认自动提交
  • 2-vi和vim的使用
  • 微服务——Nacos配置管理
  • 如何为WordPress博客网站配置免费域名HTTPS证书
  • React之内置的高阶组件
  • 【kubernetes系列】flannel之vxlan模式原理