Unity进阶-ui框架学习笔记

文章目录

Unity进阶-ui框架学习笔记

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

  • 最上面的管理层(canvas)
csharp 复制代码
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

  • panel的控制层
csharp 复制代码
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下面的组件层
csharp 复制代码
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);
        }
    }

    //Slider
    public void AddSliderEvent(UnityAction<float> action){
        Slider control = GetComponent<Slider>();
        if (control != null) {
            control.onValueChanged.AddListener(action);
        }
    }

    //Button
    public void AddButtonClickEvent(UnityAction action) {
        Button control = GetComponent<Button>();
        if (control != null) {
            control.onClick.AddListener(action);
        }
    }
}
  • 使用

    csharp 复制代码
    UImanager.Instance.GetUIControl("score", "scores").ChangetText("分数:" +  score);
相关推荐
MartinYeung52 小时前
[论文学习]OSReward:为跨平台计算机使用奖励模型建立标准化评估
人工智能·学习
dong1326972 小时前
Agent Skills学习笔记
笔记·学习
for_ever_love__2 小时前
iOS:网络请求再学习
网络·学习·ios·objective-c
世人万千丶3 小时前
鸿蒙Crash高级捕获与异常监控:全局异常兜底/崩溃栈解析/符号表还原/智能聚类/闭环修复
学习·机器学习·华为·数据挖掘·harmonyos·鸿蒙·聚类
Jul1en_3 小时前
【Claude Code Compact】源码级别的学习上下文压缩
java·前端·学习·github·ai编程
fengci.3 小时前
Microweber CMS 未授权路径穿越漏洞(CVE-2026-65694)
android·开发语言·前端·学习·php
m4Rk_4 小时前
【论文阅读】Agent 记忆机制(28):MEM1——将记忆压缩融入推理,实现近似恒定上下文的长程智能体
论文阅读·人工智能·学习·开源·github
一只小菜鸡..4 小时前
南京大学 操作系统 (JYY) 学习笔记:持久化的黑魔法——从磁铁、刻坑到闪存电荷
服务器·笔记·学习
℡枫叶℡5 小时前
Unity - 图集大小与内存占用
unity·图集内存
Embedded-Xin5 小时前
Rust学习——Cargo工具
linux·学习·架构·rust·嵌入式