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);
相关推荐
xuhaoyu_cpp_java4 小时前
项目学习(三)分页查询
java·经验分享·笔记·学习
小宋加油啊6 小时前
机械臂抓取物体 PVN3D算法调研学习
学习·算法·3d
Xzh04236 小时前
AI Agent 学习路线(Java 后端方向)
java·人工智能·学习
做cv的小昊7 小时前
计算机图形学:【Games101】学习笔记08——光线追踪(辐射度量学、渲染方程与全局光照、蒙特卡洛积分与路径追踪)
图像处理·笔记·学习·计算机视觉·游戏引擎·图形渲染·概率论
星恒随风7 小时前
C++ 类和对象入门(五):初始化列表、explicit 和 static 成员详解
开发语言·c++·笔记·学习·状态模式
狼哥16867 小时前
蛋糕美食元服务_我的实现指南
ui·harmonyos
狼哥16868 小时前
蛋糕美食元服务_美食实现指南
ui·harmonyos
RReality8 小时前
【Unity UGUI】血条 / 进度条(HP Bar)
ui·unity·游戏引擎·图形渲染
sensen_kiss9 小时前
CPT304 SoftwareEngineeringII 软件工程 2 Pt.8 软件测试 (Software Testing)(上)
学习·软件工程
力学与人工智能9 小时前
PPT分享 | 洛桑联邦理工学院魏震:深度几何学习在工业设计优化中的应用
学习·优化·工业设计·深度几何学习·洛桑联邦理工学院