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);
相关推荐
李小星同志35 分钟前
高级算法设计与分析 学习笔记6 B树
笔记·学习
霜晨月c1 小时前
MFC 使用细节
笔记·学习·mfc
小江湖19941 小时前
元数据保护者,Caesium压缩不丢重要信息
运维·学习·软件需求·改行学it
dot.Net安全矩阵1 小时前
.NET内网实战:通过命令行解密Web.config
前端·学习·安全·web安全·矩阵·.net
微刻时光2 小时前
Redis集群知识及实战
数据库·redis·笔记·学习·程序人生·缓存
潮汐退涨月冷风霜4 小时前
机器学习之非监督学习(四)K-means 聚类算法
学习·算法·机器学习
GoppViper4 小时前
golang学习笔记29——golang 中如何将 GitHub 最新提交的版本设置为 v1.0.0
笔记·git·后端·学习·golang·github·源代码管理
仙魁XAN4 小时前
Unity 设计模式 之 创造型模式-【工厂方法模式】【抽象工厂模式】
unity·设计模式·工厂方法模式·抽象工厂模式
羊小猪~~4 小时前
深度学习基础案例5--VGG16人脸识别(体验学习的痛苦与乐趣)
人工智能·python·深度学习·学习·算法·机器学习·cnn
Charles Ray5 小时前
C++学习笔记 —— 内存分配 new
c++·笔记·学习