|
| 1 | +using System.IO; |
| 2 | +using UnityEditor; |
| 3 | +using UnityEngine; |
| 4 | +using UnityEditorInternal; |
| 5 | +using System.Text.RegularExpressions; |
| 6 | +using UnityEditor.ProjectWindowCallback; |
| 7 | +using UnityEditor.Experimental; |
| 8 | +namespace Prashant |
| 9 | +{ |
| 10 | + |
| 11 | + public static class CustomMono |
| 12 | + { |
| 13 | + |
| 14 | + [MenuItem("Assets/Create/Scripts/C# Script", false, 0)] |
| 15 | + public static void CreateScript() |
| 16 | + { |
| 17 | + var templatePath = Path.Combine(Application.dataPath + "/prashantsingh/CustomMono/", "MyBehaviourTemplate.cs.txt"); |
| 18 | + // CreateNewCSScriptWithTemplate("NewFileName", templatePath); |
| 19 | + var endNameEditAction = ScriptableObject.CreateInstance<DoCreateAssetWithContent>(); |
| 20 | + endNameEditAction.filecontent = templatePath; |
| 21 | + ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, endNameEditAction, "NewFileName", EditorGUIUtility.IconContent("cs Script Icon").image as Texture2D, null); |
| 22 | + } |
| 23 | + |
| 24 | + public static void CreateScriptWithCustomBase(string _baseName) |
| 25 | + { |
| 26 | + var templatePath = Path.Combine(Application.dataPath + "/prashantsingh/CustomMono/", "MyBehaviourTemplate.cs.txt"); |
| 27 | + var endNameEditAction = ScriptableObject.CreateInstance<DoCreateAssetWithContent>(); |
| 28 | + endNameEditAction.filecontent = templatePath; |
| 29 | + endNameEditAction.baseClassName = _baseName; |
| 30 | + ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, endNameEditAction, "NewFileName", EditorGUIUtility.IconContent("cs Script Icon").image as Texture2D, null); |
| 31 | + } |
| 32 | + |
| 33 | + public static Object CreateScriptAssetFromTemplate(string pathName, string resourceFile, string baseClassName = "") |
| 34 | + { |
| 35 | + string content = File.ReadAllText(resourceFile); |
| 36 | + |
| 37 | + // #NOTRIM# is a special marker that is used to mark the end of a line where we want to leave whitespace. prevent editors auto-stripping it by accident. |
| 38 | + content = content.Replace("#NOTRIM#", ""); |
| 39 | + |
| 40 | + // macro replacement |
| 41 | + string baseFile = Path.GetFileNameWithoutExtension(pathName); |
| 42 | + //Set the namespace now |
| 43 | + content = content.Replace("#NAMESPACENAME#", EditorPrefs.GetString("MyNamespace", "GameName")); |
| 44 | + content = content.Replace("#NAME#", baseFile); |
| 45 | + var m_baseClassName = string.IsNullOrEmpty(baseClassName) ? "MonoBehaviour" : baseClassName; |
| 46 | + content = content.Replace("#BASECLASS#", m_baseClassName); |
| 47 | + content = InsertMethods(content); |
| 48 | + string baseFileNoSpaces = baseFile.Replace(" ", ""); |
| 49 | + content = content.Replace("#SCRIPTNAME#", baseFileNoSpaces); |
| 50 | + |
| 51 | + // if the script name begins with an uppercase character we support a lowercase substitution variant |
| 52 | + if (char.IsUpper(baseFileNoSpaces, 0)) |
| 53 | + { |
| 54 | + baseFileNoSpaces = char.ToLower(baseFileNoSpaces[0]) + baseFileNoSpaces.Substring(1); |
| 55 | + content = content.Replace("#SCRIPTNAME_LOWER#", baseFileNoSpaces); |
| 56 | + } |
| 57 | + else |
| 58 | + { |
| 59 | + // still allow the variant, but change the first character to upper and prefix with "my" |
| 60 | + baseFileNoSpaces = "my" + char.ToUpper(baseFileNoSpaces[0]) + baseFileNoSpaces.Substring(1); |
| 61 | + content = content.Replace("#SCRIPTNAME_LOWER#", baseFileNoSpaces); |
| 62 | + } |
| 63 | + |
| 64 | + return CreateScriptAssetWithContent(pathName, content); |
| 65 | + } |
| 66 | + |
| 67 | + static string InsertMethods(string content) |
| 68 | + { |
| 69 | + // MethodCollection m_col = new MethodCollection(); |
| 70 | + string[] m_methodsList = File.ReadAllText(Application.dataPath + "/prashantsingh/CustomMono/FinalMethodsList.txt").Split(','); |
| 71 | + string completeMethods = ""; |
| 72 | + for (int count = 0; count < m_methodsList.Length; count++) |
| 73 | + { |
| 74 | + completeMethods = completeMethods + |
| 75 | + "\n\t\tvoid " + m_methodsList[count] + " ()\n" + "\t\t{" + |
| 76 | + "\n\t\t\t//do something here\n\t\t}\n"; |
| 77 | + } |
| 78 | + // var completeMethods = ""; |
| 79 | + content = content.Replace("#METHODS#", completeMethods); |
| 80 | + return content; |
| 81 | + } |
| 82 | + |
| 83 | + public static Object CreateScriptAssetWithContent(string pathName, string templateContent) |
| 84 | + { |
| 85 | + templateContent = SetLineEndings(templateContent, EditorSettings.lineEndingsForNewScripts); |
| 86 | + |
| 87 | + string fullPath = Path.GetFullPath(pathName); |
| 88 | + |
| 89 | + // utf8-bom encoding was added for case 510374 in 2012. i think this was the wrong solution. BOM's are |
| 90 | + // problematic for diff tools, naive readers and writers (of which we have many!), and generally not |
| 91 | + // something most people think about. you wouldn't believe how many unity source files have BOM's embedded |
| 92 | + // in the middle of them for no reason. copy paste problem? bad tool? unity should instead have been fixed |
| 93 | + // to read all files that have no BOM as utf8 by default, and then we just strip them all, always, from |
| 94 | + // files we control. perhaps we'll do this one day and this next line can be removed. -scobi |
| 95 | + var encoding = new System.Text.UTF8Encoding(/*encoderShouldEmitUTF8Identifier:*/ true); |
| 96 | + |
| 97 | + File.WriteAllText(fullPath + ".cs", templateContent, encoding); |
| 98 | + try |
| 99 | + { |
| 100 | + AssetDatabase.ImportAsset(pathName + ".cs"); |
| 101 | + AssetDatabase.Refresh(); |
| 102 | + } |
| 103 | + catch (System.Exception ex) |
| 104 | + { |
| 105 | + Debug.LogException(ex); |
| 106 | + } |
| 107 | + // Import the asset |
| 108 | + return AssetDatabase.LoadAssetAtPath(pathName, typeof(Object)); |
| 109 | + } |
| 110 | + internal static string SetLineEndings(string content, LineEndingsMode lineEndingsMode) |
| 111 | + { |
| 112 | + const string windowsLineEndings = "\r\n"; |
| 113 | + const string unixLineEndings = "\n"; |
| 114 | + |
| 115 | + string preferredLineEndings; |
| 116 | + |
| 117 | + switch (lineEndingsMode) |
| 118 | + { |
| 119 | + case LineEndingsMode.OSNative: |
| 120 | + if (Application.platform == RuntimePlatform.WindowsEditor) |
| 121 | + preferredLineEndings = windowsLineEndings; |
| 122 | + else |
| 123 | + preferredLineEndings = unixLineEndings; |
| 124 | + break; |
| 125 | + case LineEndingsMode.Unix: |
| 126 | + preferredLineEndings = unixLineEndings; |
| 127 | + break; |
| 128 | + case LineEndingsMode.Windows: |
| 129 | + preferredLineEndings = windowsLineEndings; |
| 130 | + break; |
| 131 | + default: |
| 132 | + preferredLineEndings = unixLineEndings; |
| 133 | + break; |
| 134 | + } |
| 135 | + |
| 136 | + content = Regex.Replace(content, @"\r\n?|\n", preferredLineEndings); |
| 137 | + |
| 138 | + return content; |
| 139 | + } |
| 140 | + |
| 141 | + } |
| 142 | + |
| 143 | + class DoCreateAssetWithContent : EndNameEditAction |
| 144 | + { |
| 145 | + public string filecontent; |
| 146 | + public string baseClassName; |
| 147 | + public override void Action(int instanceId, string pathName, string resourceFile) |
| 148 | + { |
| 149 | + Object o = CustomMono.CreateScriptAssetFromTemplate(pathName, filecontent, baseClassName); |
| 150 | + ProjectWindowUtil.ShowCreatedAsset(o); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments