简体   繁体   English

向Unity3d编辑器添加按钮

[英]Add button to Unity3d editor

In Unity3D, I can have a property field appear in the editor by simply adding a public property to the class. 在Unity3D中,我只需在类中添加一个公共属性,就可以在编辑器中显示一个属性字段。 Is it possible to add a button next to this field that opens up a file browser that populates that field? 是否可以在此字段旁边添加一个按钮,打开一个填充该字段的文件浏览器?

I've found EditorUtility.OpenFilePanel but how can I add a button to call that method within the editor? 我找到了EditorUtility.OpenFilePanel但是如何添加一个按钮来在编辑器中调用该方法? (I don't mean a menu in the toolbar at the top as shown in the example code on that page) (我不是指顶部工具栏中的菜单,如该页面上的示例代码所示)

Thanks 谢谢

The GUILayout.Button() method displays a button in the inspector when you select the gameObject which are you referencing. 当您选择要引用的gameObject时,GUILayout.Button()方法在检查器中显示一个按钮。

For example... this code (customButton.cs) adds a button to the inspector in the script "OpenFileButtonScript.cs" which calls its OpenDialog() method. 例如......此代码(customButton.cs)在脚本“OpenFileButtonScript.cs”中向检查器添加一个按钮,该脚本调用其OpenDialog()方法。

using UnityEngine;
using System.Collections;
using UnityEditor;

[CustomEditor(typeof(OpenFileButtonScript))]
public class customButton : Editor
{
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();

        OpenFileButtonScript myScript = (OpenFileButtonScript)target;
        if (GUILayout.Button("Open File"))
        {
            myScript.OpenDialog();
        }
    }

}

This is the "OpenFileButtonScript.cs" script: 这是“OpenFileButtonScript.cs”脚本:

using UnityEngine;
using System.Collections;
using UnityEditor;

public class OpenFileButtonScript : MonoBehaviour {

    public string path;

    public void OpenDialog()
    {
        path = EditorUtility.OpenFilePanel(
                    "Open file",
                    "",
                    "*");
    }
}

I hope this can help you. 我希望这可以帮到你。 For more info about buttons see this videotutorial 有关按钮的更多信息,请参阅此视频教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM