Unity之Editor-helpBox-Toggle、Button、Label-十一

本文介绍如何在Unity编辑器中使用自定义编辑器脚本来编辑HelpBox、Toggle、Button和Label等UI组件,并展示了如何通过简单的代码实现这些组件的功能交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用Unity编辑器类编辑 helpBox、Toggle、Button、Label







在Editor文件夹下创建脚本

using UnityEngine;
using System.Collections;
using UnityEditor;

[CanEditMultipleObjects]
[CustomEditor(typeof(TestScript))]
public class EditorScript : Editor {

    private TestScript testScript;
    private bool isTrue = true;

    private void OnEnable()
    {
        testScript = (TestScript)target;
    }

    public override void OnInspectorGUI()
    {
        EditorGUILayout.BeginVertical("box");  //开始水平布局, “box” 为一个 方框区域
        EditorGUILayout.LabelField("ALDJLG0"); //不可点的Label
        EditorGUILayout.SelectableLabel("adfdfdfdfdsf");  //可点的Label
        if (GUILayout.Button("Click"))  //创建Button ,当点击按钮时,调用相应方法
        {
            testScript.MyTestA();
        }             
        EditorGUILayout.EndVertical();
        EditorGUILayout.Space();  //空格行

        isTrue = EditorGUILayout.Toggle("isTrigger",isTrue);
        EditorGUILayout.BeginVertical();
        EditorGUI.BeginDisabledGroup(isTrue);  // 如果 isTrue为真,则下方显示在面板上(灰色)不可操作, 为假则可操作
        if (GUILayout.Button("Button"))
        {
            testScript.MyTestA();
        }

        EditorGUI.EndDisabledGroup();
        EditorGUILayout.EndVertical();

        EditorGUILayout.HelpBox("help help help", MessageType.Warning);  //显示帮助框,类型为 警告
        EditorGUILayout.Space();
        EditorGUILayout.HelpBox("aaa aaa aaa", MessageType.Error);   //显示帮助框,类型为 错误
    }
}



TestScript脚本如下,将其绑定在对象上即可

using UnityEngine;
using System.Collections;

public class TestScript : MonoBehaviour {

    public void MyTestA()
    {
        Debug.Log("Click");
    }
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值