using System.Collections; using System.Collections.Generic; using UnityEditor.ProjectWindowCallback; using UnityEngine; using UnityEditor; using System; using System.IO; using System.Text; using System.Text.RegularExpressions; public class CreateUIClassFile { static string templatePath = "Assets/Editor/ScriptTemplate/UIMonobehaviorScriptTemplate.txt"; [MenuItem("Assets/Create/Custom C# Script/UIBehaviour", false, 4)] public static void CreateUIClass() { var newConfigPath = GetSelectedPathOrFallback() + "/NewUIBehaviour.cs"; AssetDatabase.DeleteAsset(newConfigPath); ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance(), newConfigPath, null, templatePath); } public static string GetSelectedPathOrFallback() { string path = "Assets"; foreach (UnityEngine.Object obj in Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets)) { path = AssetDatabase.GetAssetPath(obj); if (!string.IsNullOrEmpty(path) && File.Exists(path)) { path = Path.GetDirectoryName(path); break; } } return path; } } class UIClassTemplate : EndNameEditAction { public override void Action(int instanceId, string pathName, string resourceFile) { UnityEngine.Object o = CreateScriptAssetFromTemplate(pathName, resourceFile); ProjectWindowUtil.ShowCreatedAsset(o); } internal static UnityEngine.Object CreateScriptAssetFromTemplate(string pathName, string resourceFile) { string fullPath = Path.GetFullPath(pathName); StreamReader streamReader = new StreamReader(resourceFile); string text = streamReader.ReadToEnd(); streamReader.Close(); string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(pathName); text = Regex.Replace(text, "#ClassName*#", fileNameWithoutExtension); text = Regex.Replace(text, "#DateTime#", System.DateTime.Now.ToLongDateString()); bool encoderShouldEmitUTF8Identifier = true; bool throwOnInvalidBytes = false; UTF8Encoding encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier, throwOnInvalidBytes); bool append = false; StreamWriter streamWriter = new StreamWriter(fullPath, append, encoding); streamWriter.Write(text); streamWriter.Close(); AssetDatabase.ImportAsset(pathName); return AssetDatabase.LoadAssetAtPath(pathName, typeof(UnityEngine.Object)); } } public class CreateUIWindowClassFile { static string templatePath = "Assets/Editor/ScriptTemplate/WindowTemplate.txt"; [MenuItem("Assets/Create/Custom C# Script/Window", false, 5)] public static void CreateUIClass() { var newConfigPath = GetSelectedPathOrFallback() + "/NewWindow.cs"; AssetDatabase.DeleteAsset(newConfigPath); ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance(), newConfigPath, null, templatePath); } public static string GetSelectedPathOrFallback() { string path = "Assets"; foreach (UnityEngine.Object obj in Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets)) { path = AssetDatabase.GetAssetPath(obj); if (!string.IsNullOrEmpty(path) && File.Exists(path)) { path = Path.GetDirectoryName(path); break; } } return path; } } class UIWindowTemplate : EndNameEditAction { public override void Action(int instanceId, string pathName, string resourceFile) { UnityEngine.Object o = CreateScriptAssetFromTemplate(pathName, resourceFile); ProjectWindowUtil.ShowCreatedAsset(o); } internal static UnityEngine.Object CreateScriptAssetFromTemplate(string pathName, string resourceFile) { string fullPath = Path.GetFullPath(pathName); StreamReader streamReader = new StreamReader(resourceFile); string text = streamReader.ReadToEnd(); streamReader.Close(); string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(pathName); text = Regex.Replace(text, "#ClassName*#", fileNameWithoutExtension); text = Regex.Replace(text, "#DateTime#", System.DateTime.Now.ToLongDateString()); bool encoderShouldEmitUTF8Identifier = true; bool throwOnInvalidBytes = false; UTF8Encoding encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier, throwOnInvalidBytes); bool append = false; StreamWriter streamWriter = new StreamWriter(fullPath, append, encoding); streamWriter.Write(text); streamWriter.Close(); AssetDatabase.ImportAsset(pathName); return AssetDatabase.LoadAssetAtPath(pathName, typeof(UnityEngine.Object)); } } public class CreateDTCClassFile { static string templatePath = "Assets/Editor/ScriptTemplate/DataToCotrollerTemplate.txt"; [MenuItem("Assets/Create/Custom C# Script/DTC", false, 6)] public static void CreateUIClass() { var newConfigPath = GetSelectedPathOrFallback() + "/NewDTC.cs"; AssetDatabase.DeleteAsset(newConfigPath); ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance(), newConfigPath, null, templatePath); } public static string GetSelectedPathOrFallback() { string path = "Assets"; foreach (UnityEngine.Object obj in Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets)) { path = AssetDatabase.GetAssetPath(obj); if (!string.IsNullOrEmpty(path) && File.Exists(path)) { path = Path.GetDirectoryName(path); break; } } return path; } } class DTCTemplate : EndNameEditAction { public override void Action(int instanceId, string pathName, string resourceFile) { UnityEngine.Object o = CreateScriptAssetFromTemplate(pathName, resourceFile); AddPackageRigster(pathName); ProjectWindowUtil.ShowCreatedAsset(o); } internal static UnityEngine.Object CreateScriptAssetFromTemplate(string pathName, string resourceFile) { string fullPath = Path.GetFullPath(pathName); StreamReader streamReader = new StreamReader(resourceFile); string text = streamReader.ReadToEnd(); streamReader.Close(); string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(pathName); text = Regex.Replace(text, "#ClassName*#", fileNameWithoutExtension); text = Regex.Replace(text, "#DateTime#", System.DateTime.Now.ToLongDateString()); text = Regex.Replace(text, "#ServerPackage#", fileNameWithoutExtension.Replace("DTC", "")); bool encoderShouldEmitUTF8Identifier = true; bool throwOnInvalidBytes = false; UTF8Encoding encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier, throwOnInvalidBytes); bool append = false; StreamWriter streamWriter = new StreamWriter(fullPath, append, encoding); streamWriter.Write(text); streamWriter.Close(); AssetDatabase.ImportAsset(pathName); return AssetDatabase.LoadAssetAtPath(pathName, typeof(UnityEngine.Object)); } internal static void AddPackageRigster(string _pathName) { string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(_pathName); string packageName = fileNameWithoutExtension.Replace("DTC", "H"); string add = string.Format("Register(typeof({0}), typeof({1}));", packageName, fileNameWithoutExtension); string path = Application.dataPath + "/Scripts/Core/GameEngine/DataToCtl/PackageRegedit.cs"; var text = File.ReadAllText(path); if (!text.Contains(add)) { text = text.Replace("登记相应的数据体及对应的数据转逻辑类\r\n", "登记相应的数据体及对应的数据转逻辑类\r\n" + "\t\t" + add + "\r\n"); } bool encoderShouldEmitUTF8Identifier = true; bool throwOnInvalidBytes = false; UTF8Encoding encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier, throwOnInvalidBytes); bool append = false; StreamWriter streamWriter = new StreamWriter(path, append, encoding); streamWriter.Write(text); streamWriter.Close(); AssetDatabase.ImportAsset(path); } static string templatePath = "Assets/Editor/ScriptTemplate/ShaderPattern.txt"; [MenuItem("Assets/Create/Shader/VF Shader", false, 4)] public static void CreateUIClass() { var newConfigPath = GetSelectedPathOrFallback() + "/VF Shader.Shader"; AssetDatabase.DeleteAsset(newConfigPath); ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance(), newConfigPath, null, templatePath); } public static string GetSelectedPathOrFallback() { string path = "Assets"; foreach (UnityEngine.Object obj in Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets)) { path = AssetDatabase.GetAssetPath(obj); if (!string.IsNullOrEmpty(path) && File.Exists(path)) { path = Path.GetDirectoryName(path); break; } } return path; } } class VFShaderTemplate : EndNameEditAction { public override void Action(int instanceId, string pathName, string resourceFile) { UnityEngine.Object o = CreateScriptAssetFromTemplate(pathName, resourceFile); ProjectWindowUtil.ShowCreatedAsset(o); } internal static UnityEngine.Object CreateScriptAssetFromTemplate(string pathName, string resourceFile) { string fullPath = Path.GetFullPath(pathName); StreamReader streamReader = new StreamReader(resourceFile); string text = streamReader.ReadToEnd(); streamReader.Close(); string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(pathName); var names = fileNameWithoutExtension.Split('_'); text = Regex.Replace(text, "#NAME*#", names.Length > 0 ? names[0] : string.Empty); text = Regex.Replace(text, "#Character#", names.Length > 1 ? names[1] : string.Empty); bool encoderShouldEmitUTF8Identifier = true; bool throwOnInvalidBytes = false; UTF8Encoding encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier, throwOnInvalidBytes); bool append = false; StreamWriter streamWriter = new StreamWriter(fullPath, append, encoding); streamWriter.Write(text); streamWriter.Close(); AssetDatabase.ImportAsset(pathName); return AssetDatabase.LoadAssetAtPath(pathName, typeof(UnityEngine.Object)); } }