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?
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?
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.
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
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();
}
}