void OnWizardCreate() { InitProps(); try { AssetDatabase.StartAssetEditing(); string selectedFolder = HandyEditorUtils.GetSelectedFolder(); string scriptFile = selectedFolder + "/" + className + ".cs"; if (File.Exists(scriptFile)) { Debug.LogError("There is already a file with the same name in the selected folder!"); throw new System.InvalidOperationException(); } using (StreamWriter sw = File.CreateText(scriptFile)) { // Start with the default system imports sw.WriteLine("using System.Collections;"); sw.WriteLine("using System.Collections.Generic;"); sw.WriteLine("using UnityEngine;"); int level = 0; if (isEditor) { // If it's an editor script add the corresponding using sw.WriteLine("using UnityEditor;"); } sw.WriteLine(""); if (namespaceName != null && namespaceName.Length > 0) { // Open the namespace sw.WriteLine("namespace {0}", namespaceName); sw.WriteLine("{"); sw.WriteLine(""); level++; } if (parentClass != null && parentClass.Length > 0) { // Open class sw.WriteLine("{0}public class {1} : {2}", Indent(level), className, parentClass); sw.WriteLine("{0}{{", Indent(level)); level++; sw.WriteLine("{0}// TODO: Class auto-generated by HandyEditor", Indent(level)); if ("MonoBehaviour".Equals(parentClass)) { sw.WriteLine(""); CreateMonobehaviourBody(sw, level); } else if (isEditor && parentClass.EndsWith("Editor")) { sw.WriteLine(""); CreateCustomEditorBody(sw, level); } level--; } else { // Open class sw.WriteLine("{0}public class {1} ", Indent(level), className); sw.WriteLine("{0}{{", Indent(level)); level++; sw.WriteLine("{0}// TODO: Class auto-generated by HandyEditor", Indent(level)); level--; } // Close class sw.WriteLine("{0}}}", Indent(level)); if (namespaceName != null && namespaceName.Length > 0) { // Close namespace sw.WriteLine("}"); } } string asmdef = CreateASMDEFIFNeeded(selectedFolder); Debug.Log("Script file created at: " + scriptFile); AssetDatabase.ImportAsset(scriptFile, ImportAssetOptions.ForceUpdate); if (asmdef != null) { Debug.Log("ASMDEF file created at: " + asmdef); AssetDatabase.ImportAsset(asmdef, ImportAssetOptions.ForceUpdate); } } finally { AssetDatabase.StopAssetEditing(); AssetDatabase.SaveAssets(); } }
public static void CreatePlainText() { CreateEmptyText(HandyEditorUtils.GetSelectedFolder(), "NewTextFile", "txt"); }
public static void CreateMarkdownText() { CreateEmptyText(HandyEditorUtils.GetSelectedFolder(), "NewMarkdownFile", "md"); }
public static void CreateJsonText() { CreateEmptyText(HandyEditorUtils.GetSelectedFolder(), "NewJsonFile", "json"); }