Skip to content

Instantly share code, notes, and snippets.

@jmschrack
Created February 24, 2021 17:08
Show Gist options
  • Save jmschrack/92bfd4261d25f9b993acc07ef8fe4b81 to your computer and use it in GitHub Desktop.
Save jmschrack/92bfd4261d25f9b993acc07ef8fe4b81 to your computer and use it in GitHub Desktop.
Unity Editor: Adds context menu item to create a template Scriptable Wizard
using UnityEngine;
using UnityEditor;
using System;
using System.IO;
using System.Text;
public class CreateScriptableWizard{
[MenuItem("Assets/Create/C# Scriptable Wizard",false,105)]
static void Create(){
//this works for right clicking in the Project window
var path=AssetDatabase.GetAssetPath(Selection.activeObject);
if(!Directory.Exists(path)){
Debug.LogErrorFormat("{0} is not a valid path!",path);
return;
}
path=EditorUtility.SaveFilePanel("Save Scriptable Wizard",path,"MyScriptableWizard.cs","cs");
if(path.Length==0) return;//they clicked cancel
var fname=Path.GetFileNameWithoutExtension(path);
using (StreamWriter outFile = new StreamWriter(path)){
int tabIndex=0;
void Tab(){
for(int i=0;i<tabIndex;++i){
outFile.Write("\t");
}
}
void WriteLine(string line){
Tab();
outFile.WriteLine(line);
}
WriteLine("using UnityEngine;");
WriteLine("using UnityEditor;");
WriteLine("public class "+fname+" : ScriptableWizard {");
++tabIndex;
WriteLine("");
WriteLine("[MenuItem(\"GameObject/"+fname+"\")]");
WriteLine("static void CreateWizard(){");
++tabIndex;
WriteLine("ScriptableWizard.DisplayWizard<"+fname+">(\"Wizard Title Here\",\"Create Button Text Here\",\"Apply Button Text Here\");");
WriteLine("//If you don't want to use the secondary button simply leave it out:");
WriteLine("//ScriptableWizard.DisplayWizard<"+fname+">(\"Wizard Title Here\",\"Create Button Text Here\");");
--tabIndex;
WriteLine("}");
WriteLine("");
WriteLine("// Called when the Create button is clicked. The window is automatically closed after this.");
WriteLine("void OnWizardCreate()");
WriteLine("{");
++tabIndex;
--tabIndex;
WriteLine("}");
WriteLine("");
WriteLine("//Called every Editor frame");
WriteLine("void OnWizardUpdate()");
++tabIndex;
WriteLine("{");
WriteLine("//you can show messages in the Window by assigning to these properties:");
WriteLine("//this.errorString=\"Some Error!\";");
WriteLine("//this.helpString=\"Some Info.\";");
WriteLine("//You can deactivate the Create button by changing this bool");
WriteLine("//this.isValid=false;");
WriteLine("//You can change the ");
--tabIndex;
WriteLine("}");
WriteLine("");
WriteLine("//When the user presses the \"Apply\" button OnWizardOtherButton is called.");
WriteLine("void OnWizardOtherButton()");
WriteLine("{");
WriteLine("}");
--tabIndex;
WriteLine("}");
}
AssetDatabase.Refresh();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment