Add button to Unity3d editor

Report
Question

Please briefly explain why you feel this question should be reported .

Report Cancel

In Unity3D, I can have a property field appear in the editor by simply adding a public property to the class. 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? (I don’t mean a menu in the toolbar at the top as shown in the example code on that page)

Thanks

solved 0
1 Answer 11 views 0

Answer ( 1 )

    2
    December 11, 2016 at 3:20 am

    Please briefly explain why you feel this answer should be reported .

    Report Cancel

    The GUILayout.Button() method displays a button in the inspector when you select the gameObject which are you referencing.

    For example… this code (customButton.cs) adds a button to the inspector in the script “OpenFileButtonScript.cs” which calls its OpenDialog() method.

    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:

    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

    Best answer

Leave an answer

Browse

What is the capital of Egypt ? ( Cairo )

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>