12.使用自定义控件

12.使用自定义控件


12.1 知识点

在场景内添加一个控件根部Root,在添加一个测试界面空物体,用来管理测试界面

给测试界面添加自定义控件预设体,调整样式

创建测试面板脚本,创建控件变量,拖拽子物体赋值,在脚本里可以添加点击事件

代码

public class CustomGUITestPanel : MonoBehaviour
{
    //测试按钮
    public CustomGUIButton testCustomGUIButton;
    public CustomGUILabel testCustomGUILabel;
    public CustomGUIToggle testCustomGUIToggle;
    public CustomGUIToggleGroup testCustomGUIToggleGroup;
    public CustomGUIInput testCustomGUIInput;
    public CustomGUISlider testCustomGUISlider;
    public CustomGUITexture testCustomGUITexture;


    void Start()
    {
        testCustomGUIButton.clickEvent += () => { Debug.Log("点击测试按钮"); };

        testCustomGUIToggle.changeValue += (isSel) =>
        {
            Debug.Log($"testCustomGUIToggle isSel:{isSel}");
        };

        for (int i = 0;i < testCustomGUIToggleGroup.toggles.Length; i++)
        {
            int index = i;
            testCustomGUIToggleGroup.toggles[i].changeValue += (isSel) =>
            {
                Debug.Log($"testCustomGUIToggle{index} isSel:{isSel}");
            };
        }

        testCustomGUIInput.textChange += (text) =>
        {
            Debug.Log($"testCustomGUIInput text:{text}");
        };

        testCustomGUISlider.changeValue += (value) =>
        {
            Debug.Log($"testCustomGUISlider value:{value}");
        };
    }
}

效果


12.2 知识点代码

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

public class CustomGUITestPanel : MonoBehaviour
{
    //测试按钮
    public CustomGUIButton testCustomGUIButton;
    public CustomGUILabel testCustomGUILabel;
    public CustomGUIToggle testCustomGUIToggle;
    public CustomGUIToggleGroup testCustomGUIToggleGroup;
    public CustomGUIInput testCustomGUIInput;
    public CustomGUISlider testCustomGUISlider;
    public CustomGUITexture testCustomGUITexture;


    void Start()
    {
        testCustomGUIButton.clickEvent += () => { Debug.Log("点击测试按钮"); };

        testCustomGUIToggle.changeValue += (isSel) =>
        {
            Debug.Log($"testCustomGUIToggle isSel:{isSel}");
        };

        for (int i = 0;i < testCustomGUIToggleGroup.toggles.Length; i++)
        {
            int index = i;
            testCustomGUIToggleGroup.toggles[i].changeValue += (isSel) =>
            {
                Debug.Log($"testCustomGUIToggle{index} isSel:{isSel}");
            };
        }

        testCustomGUIInput.textChange += (text) =>
        {
            Debug.Log($"testCustomGUIInput text:{text}");
        };

        testCustomGUISlider.changeValue += (value) =>
        {
            Debug.Log($"testCustomGUISlider value:{value}");
        };
    }
}


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 785293209@qq.com

×

喜欢就点赞,疼爱就打赏