- Home /
Prompting dialogue box for input in Editor?
I'm trying to create a script for the Unity Editor that creates an empty prefab. I'm using c#.
How can I launch a dialogue or prompt box asking user to input the name of the prefab to be created?
Answer by numberkruncher · Apr 13, 2013 at 12:07 PM
You have two choices, you can create a custom editor window with a text field, or alternatively use the "Save As" window to save the prefab asset.
Using the "Save As" interface
The following example will display the standard OS provided file selector.
string path = EditorUtility.SaveFilePanelInProject("Save Your Prefab", "InitialName.prefab", "prefab", "Please select file name to save prefab to:");
if (!string.IsNullOrEmpty(path)) {
// Actually save your prefab!
}
For further information refer to EditorUtility.SaveFilePanelInProject
Creating a custom editor window
Here is a simple example of how you might create a window to achieve this.
using UnityEngine;
using UnityEditor;
class SavePrefabWindow : EditorWindow {
string prefabName;
void OnGUI() {
prefabName = EditorGUILayout.TextField("Prefab Name", prefabName);
if (GUILayout.Button("Save Prefab")) {
OnClickSavePrefab();
GUIUtility.ExitGUI();
}
}
void OnClickSavePrefab() {
prefabName = prefabName.Trim();
if (string.IsNullOrEmpty(prefabName)) {
EditorUtility.DisplayDialog("Unable to save prefab", "Please specify a valid prefab name.", "Close");
return;
}
// You may also want to check for illegal characters :)
// Save your prefab
Close();
}
}
Your answer
Follow this Question
Related Questions
Creating a custom script that extends other scripts in Editor 1 Answer
I give up. My Foldout creates a very small space when expanded. How do I get rid of it? 0 Answers
Initialising List array for use in a custom Editor 1 Answer
Unity Editor: Vertical scrollbar in flexible Editor Window 1 Answer
Unity Editor: Make a sub window wider than the parent window width. 0 Answers