Reputation: 441
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
Upvotes: 5
Views: 27271
Reputation: 826
I've had this problem & created this asset to not only call methods, but also test the parameters directly from the inspector simply by adding [ProButton] before the method
https://assetstore.unity.com/packages/tools/utilities/inspector-button-pro-151474
Upvotes: 0
Reputation: 390
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
Upvotes: 10