当前位置:   article > 正文

Unity进阶第四章-UI框架_unity ui框架

unity ui框架

一、消息框架概述

在这里插入图片描述

二、UI框架实现

(1)导入之前的框架,并创建3个新的脚本:UIManager、UIControl、UIController。
在这里插入图片描述

UIManager

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIManager : ManageBase<UIManager>
{
    //控制器字典
    public Dictionary<string, UIController> UIControllerDic = new Dictionary<string, UIController>();
    //設置頁面激活狀態
    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;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

UIControl

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

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);
            }
        }
    }
    //更改文本
    public void ChageText(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 AddButtonClickEvent(UnityAction action)
    {
        Button control = GetComponent<Button>();
        if (control != null)
        {
            control.onClick.AddListener(action);
        }
    }
    //滑动条
    public void AddSliderEvent(UnityAction<float> action)
    {
        Slider control = GetComponent<Slider>();
        if (control!= null){
            control.onValueChanged.AddListener(action);
        }
    }
    //InputField
    public void InputFieldEvent(UnityAction<string> action)
    {
        InputField control = GetComponent<InputField>();
        if (control != null)
        {
            control.onValueChanged.AddListener(action);
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66

UIController

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UIController : MonoBehaviour
{
    public Dictionary<string, UIControl> UIControlDic = new Dictionary<string, UIControl>();
     void Awake()
    {
        //将当前的页面控制器添加到管理器字典中
        UIManager.Instance.UIControllerDic.Add(transform.name, this);
        //给子控件添加UIControl脚本
        foreach(Transform tran in transform)
        {
            if (tran.gameObject.GetComponent<UIControl>() == null)
            {
                tran.gameObject.AddComponent<UIControl>();
            }
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

(2)再创建这些脚本挂载到对应的UI上
在这里插入图片描述
MainControl:
在这里插入图片描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MainController : UIController
{
    // Start is called before the first frame update
    void Start()
    {
        //给按钮添加事件
        UIManager.Instance.GetUIControl(transform.name,"MenuButton").AddButtonClickEvent(ShowMenu);

        
    }
    void ShowMenu()
    {
        UIManager.Instance.SetActive("MenuController", true);
        gameObject.SetActive(false);

    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

StateController
在这里插入图片描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StateController : UIController
{
    // Start is called before the first frame update
    void Start()
    {
        UIManager.Instance.GetUIControl(transform.name, "CloseButton (1)").AddButtonClickEvent(Close);
    }

   void Close()
    {
        UIManager.Instance.SetActive("MenuController", true);
        gameObject.SetActive(false);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

ItemController
在这里插入图片描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ItemController : UIController
{
    // Start is called before the first frame update
    void Start()
    {
        UIManager.Instance.GetUIControl(transform.name, "CloseButton (2)").AddButtonClickEvent(Close);
    }

    // Update is called once per frame
    void Close()
    {
        UIManager.Instance.SetActive("MenuController",true);
        gameObject.SetActive(false);

    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

MenuController
在这里插入图片描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MenuController :UIController
{
    // Start is called before the first frame update
    void Start()
    {
        UIManager.Instance.GetUIControl(transform.name, "StateButton").AddButtonClickEvent(ShowStateController);
        UIManager.Instance.GetUIControl(transform.name, "ItemButton").AddButtonClickEvent(ShowItemController);
        UIManager.Instance.GetUIControl(transform.name, "CloseButton").AddButtonClickEvent(Close);
        UIManager.Instance.GetUIControl(transform.name, "SkillButton").AddButtonClickEvent(ShowSkillController);

    }

    // Update is called once per frame
    void ShowStateController()
    {
        UIManager.Instance.SetActive("StateController", true);
        gameObject.SetActive(false);
    }
    void ShowItemController()
    {
        UIManager.Instance.SetActive("ItemController", true);
        gameObject.SetActive(false);
    }
    void ShowSkillController()
    {
        UIManager.Instance.SetActive("SkillController", true);
        gameObject.SetActive(false);
    }
    void Close()
    {
        UIManager.Instance.SetActive("MainController", true);
        gameObject.SetActive(false);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

SkillController
在这里插入图片描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SkillController : UIController
{
    // Start is called before the first frame update
    void Start()
    {
        UIManager.Instance.GetUIControl(transform.name, "Button (Legacy)").AddButtonClickEvent(Close);

    }

    // Update is called once per frame
    void Close()
    {
        UIManager.Instance.SetActive("MenuController",true);
        gameObject.SetActive(false);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

(3)这样就是一个完整的UI框架了

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/761592
推荐阅读
相关标签
  

闽ICP备14008679号