自学内容网 自学内容网

untiy 在菜单栏添加自定义按钮 点击按钮弹出一个Unity窗口,并在窗口里添加属性

using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;

public class AutoGenerateWindow : EditorWindow //这是定义一个窗口
{
    public string subjecttName = "科目名字";//科目的名字
    public GameObject modelAsset;//模型
    AutoGenerateWindow()
    {
        this.titleContent = new GUIContent("自动生成");
    }
    private void OnEnable()
    {
    //控制弹出窗口的高度
        this.maxSize = new Vector2(500, 200);
        this.minSize = new Vector2(500, 200);
    }

    [MenuItem("自动生成/打开生成面板")] //这是在添加菜单栏的按钮
    private static void OnOpenGeneateWindow()
    {
        AutoGenerateWindow win = EditorWindow.GetWindow<AutoGenerateWindow>();
        win.Show();//打开这个窗口
    }
    private void OnGUI()
    {
        //添加属性
        GUILayout.Space(10);//每个属性之间上下的空白
        subjecttName = EditorGUILayout.TextField("科目名字", subjecttName);
        modelAsset = (GameObject)EditorGUILayout.ObjectField("模型", modelAsset, typeof(GameObject), false); //最后的bool,true选择scene里的物体,false资源选择器

        //添加按钮
        if (GUILayout.Button("生成"))
        {
            //点击按钮的具体逻辑
        }
    }
}

打开的窗口如下
在这里插入图片描述


原文地址:https://blog.csdn.net/weixin_44568736/article/details/140322174

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!