From 9763540f90d49ead686e39cc9c69fb44c0d0dafb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=80=90=E7=A8=8B=E5=BA=8F=E3=80=91=E7=A8=8B=E4=B8=80?= =?UTF-8?q?=E5=B3=B0?= <649669121@qq.com> Date: Tue, 6 Dec 2022 20:24:11 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E9=85=8D=E7=BD=AE=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=EF=BC=8C=E4=BF=AE=E6=94=B9=E6=9C=AC=E5=9C=B0=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Editor/CreateScriptByTemplate.cs | 12 ++--- Editor/LycianthesConfig.cs | 22 +++++++++ Editor/LycianthesConfigWindow.cs | 32 ++++++------ Editor/LycianthesScriptCreator.cs | 15 +++--- Editor/LycianthesScriptWindow.cs | 81 +++++++++++++++++++++---------- 5 files changed, 106 insertions(+), 56 deletions(-) diff --git a/Editor/CreateScriptByTemplate.cs b/Editor/CreateScriptByTemplate.cs index ee6e3e4..9b58533 100644 --- a/Editor/CreateScriptByTemplate.cs +++ b/Editor/CreateScriptByTemplate.cs @@ -20,9 +20,8 @@ namespace Lycianthes public class CreateScriptByTemplate { public static string Version = "0.0.1"; - public static string Author = "Chief"; - public static string Company = "RedPanda"; public static string NameSpace = "RedPanda"; + public static string MoudleInfo = "模块说明"; public static void CreateSript(string resourceFile, string fileName = "/Scripts/", string className = "New_CS_Script") { @@ -111,11 +110,12 @@ namespace Lycianthes { StringBuilder summary = new StringBuilder(); summary.Append("/*\n"); - summary.Append($" *Copyright(C) 2023 by {Company} All rights reserved.\n"); + summary.Append($" *Copyright(C) 2023 by {LycianthesConfig.GetInstance().CompanyName} All rights reserved.\n"); summary.Append($" *Unity版本:{Application.unityVersion} \n"); - summary.Append($" *作者:{Author } \n"); + summary.Append($" *作者:{LycianthesConfig.GetInstance().AuthorName} \n"); summary.Append($" *创建日期: {System.DateTime.Now.ToString("yyyy-MM-dd")} \n"); - summary.Append(" *模块说明:\n"); + if (LycianthesConfig.GetInstance().UseModuleInfo) + summary.Append($" *模块说明:{MoudleInfo}\n"); summary.Append($" *版本: {Version}\n"); summary.Append("*/"); text = Regex.Replace(text, "#SUMMARY#", summary.ToString()); @@ -123,4 +123,4 @@ namespace Lycianthes } } -} +} \ No newline at end of file diff --git a/Editor/LycianthesConfig.cs b/Editor/LycianthesConfig.cs index c32567a..6bad9dd 100644 --- a/Editor/LycianthesConfig.cs +++ b/Editor/LycianthesConfig.cs @@ -24,6 +24,28 @@ namespace Lycianthes public static LycianthesConfig FromJson(string json) { return JsonUtility.FromJson(json); } + /// + /// 本地配置; + /// + public const string KEY_LYCIANTHES_CONFIG = "KEY_LYCIANTHES_CONFIG"; + private static LycianthesConfig instance; + + public static LycianthesConfig GetInstance() + { + if (instance != null) return instance; + + if (!PlayerPrefs.HasKey(KEY_LYCIANTHES_CONFIG)) + { + instance = new LycianthesConfig(); ; + return instance; + } + + var jsonStr = PlayerPrefs.GetString(KEY_LYCIANTHES_CONFIG); + instance = FromJson(jsonStr); + return instance; + } + + [LabelText("作者"), LabelWidth(60)] public string AuthorName; diff --git a/Editor/LycianthesConfigWindow.cs b/Editor/LycianthesConfigWindow.cs index 0581a35..384803a 100644 --- a/Editor/LycianthesConfigWindow.cs +++ b/Editor/LycianthesConfigWindow.cs @@ -21,12 +21,6 @@ namespace Lycianthes /// public class LycianthesConfigWindow : OdinEditorWindow { - - /// - /// 本地配置; - /// - public const string KEY_LYCIANTHES_CONFIG = "KEY_LYCIANTHES_CONFIG"; - /// /// 上次选择的默认脚本模板文件夹 /// @@ -39,13 +33,7 @@ namespace Lycianthes window.position = GUIHelper.GetEditorWindowRect().AlignCenter(500, 375); window.titleContent = new GUIContent("本地配置"); - if (PlayerPrefs.HasKey(KEY_LYCIANTHES_CONFIG)) - { - var jsonStr = PlayerPrefs.GetString(KEY_LYCIANTHES_CONFIG); - window.LocalConfig = LycianthesConfig.FromJson(jsonStr); - } - else - window.LocalConfig = new LycianthesConfig(); + window.LocalConfig = LycianthesConfig.GetInstance(); if (PlayerPrefs.HasKey(KEY_LYCIANTHES_LAST_TEMPLATE_FOLDER)) window.ScriptTemplateFolder = PlayerPrefs.GetString(KEY_LYCIANTHES_LAST_TEMPLATE_FOLDER); @@ -62,7 +50,7 @@ namespace Lycianthes public void Save() { if (LocalConfig == null) return; - PlayerPrefs.SetString(KEY_LYCIANTHES_CONFIG, LocalConfig.GetJsonString()); + PlayerPrefs.SetString(LycianthesConfig.KEY_LYCIANTHES_CONFIG, LocalConfig.GetJsonString()); } [Title("脚本模板创建")] @@ -75,7 +63,7 @@ namespace Lycianthes /// /// 是否有合法的脚本模板文件夹 /// - private bool HasScriptTemplateFolder { get { return !string.IsNullOrEmpty(ScriptTemplateFolder); } } + private bool HasScriptTemplateFolder { get { return !string.IsNullOrEmpty(ScriptTemplateFolder); } } [LabelText("脚本模板文件名")] [PropertyOrder(30)] @@ -98,8 +86,20 @@ namespace Lycianthes return; } - PlayerPrefs.SetString(KEY_LYCIANTHES_LAST_TEMPLATE_FOLDER, ScriptTemplateFolder); + string path = $"{ScriptTemplateFolder}/{ScriptTemplateAssetName}.asset"; + //先加载一下是否有当前文件; + var template = AssetDatabase.LoadAssetAtPath(path); + if (template != null) + { + EditorUtility.DisplayDialog("Lycianthes配置", $"已经有此模板:{ScriptTemplateAssetName}\n不能重复创建!", "确定"); + return; + } + template = CreateInstance(); + AssetDatabase.CreateAsset(template, path); + Debug.Log($"已生成模板:{path} : {template.name}"); + //缓存本次保存的文件夹 + PlayerPrefs.SetString(KEY_LYCIANTHES_LAST_TEMPLATE_FOLDER, ScriptTemplateFolder); } } diff --git a/Editor/LycianthesScriptCreator.cs b/Editor/LycianthesScriptCreator.cs index a5d9791..e6e43c6 100644 --- a/Editor/LycianthesScriptCreator.cs +++ b/Editor/LycianthesScriptCreator.cs @@ -61,15 +61,14 @@ namespace Lycianthes #endregion - - [LabelText("公司名")] - public string Company = "RedPanda"; - [LabelText("版本")] public string Version = "1.0.0"; [LabelText("用户名")] - public string AuthorName = "Chief"; + [ShowIf("ShowMoudleInfo")] + public string MoudleInfo = "模块说明"; + + private bool ShowMoudleInfo => LycianthesConfig.GetInstance().UseModuleInfo; [LabelText("命名空间")] public string NameSpace = "RedPanda"; @@ -98,8 +97,7 @@ namespace Lycianthes string path = AssetDatabase.GetAssetPath(temp.Template); //创建脚本 CreateScriptByTemplate.Version = Version; - CreateScriptByTemplate.Author = AuthorName; - CreateScriptByTemplate.Company = Company; + CreateScriptByTemplate.MoudleInfo = MoudleInfo; CreateScriptByTemplate.NameSpace = NameSpace; //删除前面的/Asset string curPath = temp.DefaultFolder.Substring(6); @@ -130,8 +128,7 @@ namespace Lycianthes string path = AssetDatabase.GetAssetPath(temp.Template); //创建脚本 CreateScriptByTemplate.Version = Version; - CreateScriptByTemplate.Author = AuthorName; - CreateScriptByTemplate.Company = Company; + CreateScriptByTemplate.MoudleInfo = MoudleInfo; CreateScriptByTemplate.NameSpace = NameSpace; //删除前面的/Asset CreateScriptByTemplate.CreateScriptNoChosse(path, curPath, name); diff --git a/Editor/LycianthesScriptWindow.cs b/Editor/LycianthesScriptWindow.cs index 9bf4a2c..e1dd5c5 100644 --- a/Editor/LycianthesScriptWindow.cs +++ b/Editor/LycianthesScriptWindow.cs @@ -23,8 +23,8 @@ namespace Lycianthes /// public class LycianthesScriptWindow : OdinEditorWindow { - public const string KEY_USER_NAME = "KEY_SCRIPTCREATOR_USER_NAME"; public const string KEY_USER_NAMESPACE = "KEY_SCRIPTCREATOR_USER_NAMESPACE"; + public const string KEY_USER_MOUDLE_INFO = "KEY_SCRIPTCREATOR_USER_MOUDLE_INFO"; public const float LABEL_WIDTH = 60; [MenuItem("Assets/Create/创建.CS脚本", false, 81)] @@ -36,22 +36,17 @@ namespace Lycianthes //缓存的配置文件位置 window.creator = AssetDatabase.LoadAssetAtPath(window.assetPath); - //缓存的作者名; - if (PlayerPrefs.HasKey(KEY_USER_NAME)) - window.UserName = PlayerPrefs.GetString(KEY_USER_NAME); if (PlayerPrefs.HasKey(KEY_USER_NAMESPACE)) window.NameSpace = PlayerPrefs.GetString(KEY_USER_NAMESPACE); //当前的文件夹; window.TargetPath = GetCurrentSelectFolderPath(); window.Version = PlayerSettings.bundleVersion; - window.CompanyName = PlayerSettings.companyName; window.creator.ResetOtherTemplate(); AutoLoadTemplateGroup(); } private static void AutoLoadTemplateGroup() { - var guids = AssetDatabase.FindAssets("t:LycianthesTemplateGroup"); foreach (var guid in guids) { @@ -91,27 +86,29 @@ namespace Lycianthes [LabelWidth(LABEL_WIDTH)] [Title("配置信息")] [ShowInInspector] - private string assetPath => AssetPath; + [PropertyOrder(0)] + private string assetPath + { + get { return AssetPath; } + set { } + } /// /// 资源位置 /// [LabelText("配置目录"), ReadOnly] [LabelWidth(LABEL_WIDTH)] + [PropertyOrder(1)] + [HideReferenceObjectPicker] public LycianthesScriptCreator creator; - /// - /// 公司名,读取的PlayerSetting; - /// - [LabelText("公司名")] - [LabelWidth(LABEL_WIDTH)] - public string CompanyName = "RedPanda"; - /// /// 命名空间; /// + [Title("脚本说明配置", "本地配置菜单: Tools/Lycianthes/本地设置", TitleAlignment = TitleAlignments.Split)] [LabelText("命名空间"), OnValueChanged("OnChangeUserSetting")] [LabelWidth(LABEL_WIDTH)] + [PropertyOrder(10)] public string NameSpace = "RedPanda"; /// @@ -119,44 +116,80 @@ namespace Lycianthes /// [LabelText("版本号")] [LabelWidth(LABEL_WIDTH)] + [PropertyOrder(10)] public string Version = "1.0.0"; - [LabelText("用户名"), OnValueChanged("OnChangeUserSetting")] + /// + /// 公司名,读取的PlayerSetting; + /// + [LabelText("模板信息")] + [LabelWidth(LABEL_WIDTH)] + [ShowIf("ShowMoudleInfo")] + [PropertyOrder(10)] + [OnValueChanged("OnChangeUserSetting")] + public string MoudelInfo = "模板信息"; + private bool ShowMoudleInfo => LycianthesConfig.GetInstance().UseModuleInfo; + + [LabelText("公司名")] [LabelWidth(LABEL_WIDTH)] - public string UserName = ""; + [PropertyOrder(10)] + [ShowInInspector, DisplayAsString] + public string CompanyName + { + get { return LycianthesConfig.GetInstance().CompanyName; } + set { } + } + + [LabelText("用户名")] + [LabelWidth(LABEL_WIDTH)] + [ShowInInspector, DisplayAsString] + [PropertyOrder(10)] + public string AuthorName + { + get { return LycianthesConfig.GetInstance().AuthorName; } + set { } + } private void OnChangeUserSetting() { - PlayerPrefs.SetString(KEY_USER_NAME, UserName); PlayerPrefs.SetString(KEY_USER_NAMESPACE, NameSpace); + + if (ShowMoudleInfo) + PlayerPrefs.SetString(KEY_USER_MOUDLE_INFO, MoudelInfo); } [HideLabel, Title("脚本类型"), OnValueChanged("OnScriptTypeChanged")] [ValueDropdown("GetScriptKey")] + [PropertyOrder(20)] public string scriptType = "CSharpCommon"; private List GetScriptKey() { return creator.GetScriptTemplateKey(); } [Title("保存位置"), Space(15)] + [PropertyOrder(20)] [LabelWidth(LABEL_WIDTH), LabelText("批量创建")] public bool IsList = false; [LabelText("脚本名"), OnValueChanged("OnScriptNameChagned")] + [PropertyOrder(20)] [LabelWidth(LABEL_WIDTH)] [HideIf("IsList")] public string ScriptName = ""; [LabelText("脚本列表"), LabelWidth(LABEL_WIDTH), ShowIf("IsList")] + [PropertyOrder(20)] public List ListScriptName = new List(); [TabGroup("在当前文件夹创建")] [LabelWidth(LABEL_WIDTH)] [LabelText("创建位置"), FolderPath] + [PropertyOrder(20)] public string TargetPath = ""; [TabGroup("在默认文件夹创建")] [LabelWidth(LABEL_WIDTH)] [LabelText("默认位置"), FolderPath] + [PropertyOrder(20)] public string DefaultPath = ""; /// @@ -188,11 +221,13 @@ namespace Lycianthes [Button("开始创建"), HideIf("IsList")] [TabGroup("在当前文件夹创建")] + [PropertyOrder(20)] public void CreateAtTargetPath() { CreateScript(ScriptName, TargetPath); } [Button("开始创建"), HideIf("IsList")] [TabGroup("在默认文件夹创建")] + [PropertyOrder(20)] public void CreateAtDefaultPath() { CreateScript(ScriptName, DefaultPath); } @@ -204,15 +239,8 @@ namespace Lycianthes return; } - if (string.IsNullOrEmpty(UserName)) - { - EditorUtility.DisplayDialog("警告", $"创建脚本 {scriptType}时出错:\n没有输入用户名!", "确定"); - return; - } - creator.Version = Version; - creator.AuthorName = UserName; - creator.Company = CompanyName; + creator.MoudleInfo = MoudelInfo; creator.NameSpace = NameSpace; //删除掉前面的Asset creator.CreateNewScript(scriptType, path.Substring(6), scriptName); @@ -223,6 +251,7 @@ namespace Lycianthes /// [Button("开始创建"), ShowIf("IsList")] [TabGroup("在当前文件夹创建")] + [PropertyOrder(20)] public void CreateListScriptAtTarget() { int length = ListScriptName.Count; @@ -235,6 +264,7 @@ namespace Lycianthes /// [Button("开始创建"), ShowIf("IsList")] [TabGroup("在默认文件夹创建")] + [PropertyOrder(20)] public void CreateListAtDefault() { int length = ListScriptName.Count; @@ -246,6 +276,7 @@ namespace Lycianthes /// 刷新编辑器; /// [PropertySpace(15), Button("刷新")] + [PropertyOrder(30)] public void Refresh() { AssetDatabase.Refresh(); } } -- GitLab