Unity advanced-ui framework study notes

Unity advanced-ui framework study notes

Note source course: https://study.163.com/course/courseMain.htm?courseId=1212756805&_trace_c_p_k2_=8c8d7393c43b400d89ae94ab037586fc

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-qvYhS3e7-1690019993960)(QQ%E6%88%AA%E5%9B%BE20230722085931.png)]

  • The top management layer (canvas)

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (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;
    }

}


Adjust the running order to make it faster than the controller

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-L7BTY0Xp-1690019993962)(../AppData/Roaming/Typora/typora-user-images/image-20230722171921737.png)]

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-smOfAPvE-1690019993962)(../AppData/Roaming/Typora/typora-user-images/image-20230722171854565.png)]

  • The control layer of the panel

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (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>();
            }
        }
    }
  
}

  • The component layer below the panel

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (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);
        }
    }

    //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);
        }
    }
}


  • use

    UImanager.Instance.GetUIControl("score", "scores").ChangetText("分数:" +  score);
    

Guess you like

Origin blog.csdn.net/abaidaye/article/details/131870927