From fdffe4bee464b1dc90379860c4030de1f7bd83c8 Mon Sep 17 00:00:00 2001 From: ChenJie Date: Sat, 7 Sep 2019 16:08:40 +0800 Subject: [PATCH 1/3] fix excel asset path contains \ --- Assets/QuickSheet/ExcelPlugin/Editor/ExcelMachineEditor.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/QuickSheet/ExcelPlugin/Editor/ExcelMachineEditor.cs b/Assets/QuickSheet/ExcelPlugin/Editor/ExcelMachineEditor.cs index 42d4084..d285ed0 100644 --- a/Assets/QuickSheet/ExcelPlugin/Editor/ExcelMachineEditor.cs +++ b/Assets/QuickSheet/ExcelPlugin/Editor/ExcelMachineEditor.cs @@ -291,7 +291,7 @@ protected override void CreateAssetCreationScript(BaseMachine m, ScriptPrescript // path where the .asset file will be created. string path = Path.GetDirectoryName(machine.excelFilePath); path += "/" + machine.WorkSheetName + ".asset"; - sp.assetFilepath = path; + sp.assetFilepath = path.Replace('\\', '/'); sp.assetPostprocessorClass = machine.WorkSheetName + "AssetPostprocessor"; sp.template = GetTemplate("PostProcessor"); From 5fb7e85928d1b422679b5dcc580c554548aa40bc Mon Sep 17 00:00:00 2001 From: ChenJie Date: Sat, 7 Sep 2019 21:51:04 +0800 Subject: [PATCH 2/3] Add QuickSheet Menu 1.add setup select excels menu item 2.add refresh all menu item 3.move select setting menu item 4.make some BaseMachineEditor method public --- Assets/QuickSheet/Editor/BaseMachineEditor.cs | 4 +- Assets/QuickSheet/Editor/QuickSheetMenu.cs | 143 ++++++++++++++++++ .../QuickSheet/Editor/QuickSheetMenu.cs.meta | 3 + .../Editor/Util/SingletonScriptableObject.cs | 12 +- Assets/QuickSheet/Editor/Util/Util.cs | 14 ++ .../ExcelPlugin/Editor/Excel Settings.asset | Bin 4224 -> 544 bytes .../ExcelPlugin/Editor/ExcelMachine.cs | 3 +- .../ExcelPlugin/Editor/ExcelMachineEditor.cs | 2 +- .../ExcelPlugin/Editor/ExcelSettings.cs | 13 -- .../GDataPlugin/Editor/GoogleDataSettings.cs | 13 -- .../GDataPlugin/Editor/GoogleMachineEditor.cs | 2 +- 11 files changed, 171 insertions(+), 38 deletions(-) create mode 100644 Assets/QuickSheet/Editor/QuickSheetMenu.cs create mode 100644 Assets/QuickSheet/Editor/QuickSheetMenu.cs.meta diff --git a/Assets/QuickSheet/Editor/BaseMachineEditor.cs b/Assets/QuickSheet/Editor/BaseMachineEditor.cs index 1838c9b..caf4c9c 100644 --- a/Assets/QuickSheet/Editor/BaseMachineEditor.cs +++ b/Assets/QuickSheet/Editor/BaseMachineEditor.cs @@ -42,7 +42,7 @@ public override void OnInspectorGUI() /// /// It should be overried and implemented in the derived class /// - protected virtual void Import(bool reimport = false) + public virtual void Import(bool reimport = false) { throw new NotImplementedException(); } @@ -66,7 +66,7 @@ protected bool IsValidHeader(string s) /// Generate script files with the given templates. /// Total four files are generated, two for runtime and others for editor. /// - protected virtual ScriptPrescription Generate(BaseMachine m) + public virtual ScriptPrescription Generate(BaseMachine m) { if (m == null) return null; diff --git a/Assets/QuickSheet/Editor/QuickSheetMenu.cs b/Assets/QuickSheet/Editor/QuickSheetMenu.cs new file mode 100644 index 0000000..8c22ba0 --- /dev/null +++ b/Assets/QuickSheet/Editor/QuickSheetMenu.cs @@ -0,0 +1,143 @@ +using System.Collections.Generic; +using System.IO; +using System.Text; +using UnityEditor; +using UnityEngine; + +namespace UnityQuickSheet +{ + public static class QuickSheetMenu + { + /// + /// Select currently exist account setting asset file. + /// + [MenuItem("QuickSheet/Excel Setting")] + public static void SelectExcelSetting() + { + Selection.activeObject = ExcelSettings.Instance; + if (Selection.activeObject == null) + { + Debug.LogError(@"No ExcelSetting.asset file is found. Create setting file first. See the menu at 'Create/QuickSheet/Setting/Excel Setting'."); + } + } + + /// + /// Select currently exist account setting asset file. + /// + [MenuItem("QuickSheet/Google Data Setting")] + public static void SelectGoogleDataSetting() + { + Selection.activeObject = GoogleDataSettings.Instance; + if (Selection.activeObject == null) + { + Debug.LogError("No GoogleDataSettings.asset file is found. Create setting file first."); + } + } + + /// + /// Setup selected excel files + /// 1.Create Machine Asset + /// 2.Reimport Machine + /// 3.Generate Class + /// 4.Reimport Excel file + /// + [MenuItem("QuickSheet/Setup Select Excels")] + public static void SetupSelectExcels() + { + if (Selection.assetGUIDs.Length == 0) + { + EditorUtility.DisplayDialog("Error", "Select excel files to setup!", "OK"); + return; + } + + var selectObjs = new List(); + foreach (var guid in Selection.assetGUIDs) + { + var path = AssetDatabase.GUIDToAssetPath(guid); + if (!IsExcel(path)) continue; + + var excelQuery = new ExcelQuery(path); + var sheets = excelQuery.GetSheetNames(); + for (int i = 0; i < sheets.Length; i++) + { + var machine = ExcelMachine.CreateScriptMachineAsset(); + machine.excelFilePath = path; + machine.SheetNames = sheets; + machine.WorkSheetName = sheets[i]; + machine.CurrentSheetIndex = i; + machine.SpreadSheetName = Path.GetFileName(path); + + ReimportMachine(machine, true); + GenerateMachine(machine, false); + RenameMachineAsset(machine); + + selectObjs.Add(machine); + Debug.LogFormat("Setup finished! file:{0}, Sheet:{1}", machine.SpreadSheetName, sheets[i]); + } + + //reimport excel file to generate data asset and update it + AssetDatabase.ImportAsset(path); + } + + Selection.objects = selectObjs.ToArray(); + AssetDatabase.Refresh(); + } + + /// + /// Refresh all excel files in project + /// refresh when change excel header column + /// + [MenuItem("QuickSheet/Refresh All")] + public static void RefreshAll() + { + foreach (var machine in Util.FindAssetsByType()) + { + ReimportMachine(machine, true); + GenerateMachine(machine, true); + } + + var files = Directory.GetFiles(Application.dataPath, "*.xls?", SearchOption.AllDirectories); + foreach (var file in files) + { + if (!IsExcel(file) || Path.GetFileName(file).StartsWith("~$")) continue; + var relativePath = "Assets" + file.Replace(Application.dataPath, ""); + Debug.Log(relativePath); + AssetDatabase.ImportAsset(relativePath); + } + AssetDatabase.Refresh(); + } + + private static void GenerateMachine(BaseMachine machine, bool onlyDataClass) + { + var editor = Editor.CreateEditor(machine) as BaseMachineEditor; + if (editor == null) return; + machine.onlyCreateDataClass = onlyDataClass; + editor.Generate(machine); + } + + private static void ReimportMachine(BaseMachine machine, bool reimport) + { + var editor = Editor.CreateEditor(machine) as BaseMachineEditor; + if (editor == null) return; + editor.Import(reimport); + } + + private static void RenameMachineAsset(ExcelMachine machine) + { + var name = new StringBuilder(); + name.Append(machine.SpreadSheetName.Split('.')[0]) + .Append("-") + .Append(machine.WorkSheetName) + .Append("-") + .Append("Importer"); + var err = AssetDatabase.RenameAsset(AssetDatabase.GetAssetPath(machine), name.ToString()); + if (!string.IsNullOrEmpty(err)) Debug.LogWarning("Rename failed " + err); + } + + private static bool IsExcel(string path) + { + var ext = Path.GetExtension(path); + return ext == ".xls" || ext == ".xlsx"; + } + } +} \ No newline at end of file diff --git a/Assets/QuickSheet/Editor/QuickSheetMenu.cs.meta b/Assets/QuickSheet/Editor/QuickSheetMenu.cs.meta new file mode 100644 index 0000000..8196dcc --- /dev/null +++ b/Assets/QuickSheet/Editor/QuickSheetMenu.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: cb3e33668eef4724bf74729e7322004a +timeCreated: 1567843872 \ No newline at end of file diff --git a/Assets/QuickSheet/Editor/Util/SingletonScriptableObject.cs b/Assets/QuickSheet/Editor/Util/SingletonScriptableObject.cs index eeb3b9e..5e6c016 100644 --- a/Assets/QuickSheet/Editor/Util/SingletonScriptableObject.cs +++ b/Assets/QuickSheet/Editor/Util/SingletonScriptableObject.cs @@ -27,16 +27,14 @@ public static T Instance _instance = Resources.FindObjectsOfTypeAll().FirstOrDefault(); if (!_instance) { - string filter = "t:" + typeof(T); - var guids = AssetDatabase.FindAssets(filter); - if (guids.Length > 0) + var assets = Util.FindAssetsByType(); + if (assets.Length > 0) { - if (guids.Length > 1) + if (assets.Length > 1) Debug.LogWarningFormat("Multiple {0} assets are found.", typeof(T)); - string assetPath = AssetDatabase.GUIDToAssetPath(guids[0]); - Debug.LogFormat("Using {0}.", assetPath); - _instance = AssetDatabase.LoadAssetAtPath(assetPath); + _instance = assets[0]; + Debug.LogFormat("Using {0}.", AssetDatabase.GetAssetPath(_instance)); } // no found any setting file. else diff --git a/Assets/QuickSheet/Editor/Util/Util.cs b/Assets/QuickSheet/Editor/Util/Util.cs index cfac57d..010faa5 100644 --- a/Assets/QuickSheet/Editor/Util/Util.cs +++ b/Assets/QuickSheet/Editor/Util/Util.cs @@ -8,6 +8,7 @@ using System; using System.Collections; using System.Linq; +using UnityEditor; namespace UnityQuickSheet { @@ -25,5 +26,18 @@ public static class Util "ushort", "using", "virtual", "void", "volatile", "while", }; + public static T[] FindAssetsByType() where T : UnityEngine.Object + { + string filter = "t:" + typeof(T); + var guids = AssetDatabase.FindAssets(filter); + var results = new T[guids.Length]; + for (int i = 0; i < guids.Length; i++) + { + string assetPath = AssetDatabase.GUIDToAssetPath(guids[i]); + results[i] = AssetDatabase.LoadAssetAtPath(assetPath); + } + + return results; + } } } \ No newline at end of file diff --git a/Assets/QuickSheet/ExcelPlugin/Editor/Excel Settings.asset b/Assets/QuickSheet/ExcelPlugin/Editor/Excel Settings.asset index 2fb8150c990fa9c9444aa072f331ff02ef13f513..9fce7af0527814337300f29c692a2bb9c13f5a55 100644 GIT binary patch literal 544 zcmZ{fK~KU!5QXpk6~h5fKv|$A*&6~PiDZrm6ipSLM=aZ7vr zR7G8J4y zK@hwYDfl<^xUr=UX_I>BK?>sa6OmY~JfeDfQv54$LL=YJ- zjg`s^W%<+V^^^iY?ERQrnRrnscyC`{oaua-{H80d$9d$p&{;pIB!+6tdAcMfSMn|| z@dvycNa|Qi&h`m>H)0N*Od=*>;6pE-2=|{ zzYlvgGu&PmrA>?%+f!Wa-)9nHG-SyA6*Y3@J{8b-W;D6IK=YVs-Ip7>{4mby#1I_{GfOBBW zIFMWw^p&807vNR2wmk1Qi_?A-1>s`Hd~Ok;-VNp-Pd9u&8r3CUv%46Cqy1Y+QYrEm D?bN=d diff --git a/Assets/QuickSheet/ExcelPlugin/Editor/ExcelMachine.cs b/Assets/QuickSheet/ExcelPlugin/Editor/ExcelMachine.cs index 6c994a7..3123a43 100644 --- a/Assets/QuickSheet/ExcelPlugin/Editor/ExcelMachine.cs +++ b/Assets/QuickSheet/ExcelPlugin/Editor/ExcelMachine.cs @@ -50,13 +50,14 @@ private void Awake() { /// A menu item which create a 'ExcelMachine' asset file. /// [MenuItem("Assets/Create/QuickSheet/Tools/Excel")] - public static void CreateScriptMachineAsset() + public static ExcelMachine CreateScriptMachineAsset() { ExcelMachine inst = ScriptableObject.CreateInstance(); string path = CustomAssetUtility.GetUniqueAssetPathNameOrFallback(ImportSettingFilename); AssetDatabase.CreateAsset(inst, path); AssetDatabase.SaveAssets(); Selection.activeObject = inst; + return inst; } } } \ No newline at end of file diff --git a/Assets/QuickSheet/ExcelPlugin/Editor/ExcelMachineEditor.cs b/Assets/QuickSheet/ExcelPlugin/Editor/ExcelMachineEditor.cs index d285ed0..0ca224d 100644 --- a/Assets/QuickSheet/ExcelPlugin/Editor/ExcelMachineEditor.cs +++ b/Assets/QuickSheet/ExcelPlugin/Editor/ExcelMachineEditor.cs @@ -189,7 +189,7 @@ Set a folder under the 'Assets' folder! \n /// /// Import the specified excel file and prepare to set type of each cell. /// - protected override void Import(bool reimport = false) + public override void Import(bool reimport = false) { ExcelMachine machine = target as ExcelMachine; diff --git a/Assets/QuickSheet/ExcelPlugin/Editor/ExcelSettings.cs b/Assets/QuickSheet/ExcelPlugin/Editor/ExcelSettings.cs index 6b427bd..c2c1380 100644 --- a/Assets/QuickSheet/ExcelPlugin/Editor/ExcelSettings.cs +++ b/Assets/QuickSheet/ExcelPlugin/Editor/ExcelSettings.cs @@ -32,18 +32,5 @@ public class ExcelSettings : SingletonScriptableObject /// A path where generated editor script files are to be put. /// public string EditorPath = string.Empty; - - /// - /// Select currently exist account setting asset file. - /// - [MenuItem("Edit/Project Settings/QuickSheet/Select Excel Setting")] - public static void Edit() - { - Selection.activeObject = Instance; - if (Selection.activeObject == null) - { - Debug.LogError(@"No ExcelSetting.asset file is found. Create setting file first. See the menu at 'Create/QuickSheet/Setting/Excel Setting'."); - } - } } } diff --git a/Assets/QuickSheet/GDataPlugin/Editor/GoogleDataSettings.cs b/Assets/QuickSheet/GDataPlugin/Editor/GoogleDataSettings.cs index 2946362..20e3b49 100644 --- a/Assets/QuickSheet/GDataPlugin/Editor/GoogleDataSettings.cs +++ b/Assets/QuickSheet/GDataPlugin/Editor/GoogleDataSettings.cs @@ -68,18 +68,5 @@ public struct OAuth2JsonData public string _RefreshToken = ""; public string _AccessToken = ""; - - /// - /// Select currently exist account setting asset file. - /// - [MenuItem("Edit/Project Settings/QuickSheet/Select Google Data Setting")] - public static void Edit() - { - Selection.activeObject = Instance; - if (Selection.activeObject == null) - { - Debug.LogError("No GoogleDataSettings.asset file is found. Create setting file first."); - } - } } } \ No newline at end of file diff --git a/Assets/QuickSheet/GDataPlugin/Editor/GoogleMachineEditor.cs b/Assets/QuickSheet/GDataPlugin/Editor/GoogleMachineEditor.cs index f94546e..70592af 100644 --- a/Assets/QuickSheet/GDataPlugin/Editor/GoogleMachineEditor.cs +++ b/Assets/QuickSheet/GDataPlugin/Editor/GoogleMachineEditor.cs @@ -190,7 +190,7 @@ private void DoCellQuery(OnEachCell onCell) /// /// Connect to the google spreadsheet and retrieves its header columns. /// - protected override void Import(bool reimport = false) + public override void Import(bool reimport = false) { Regex re = new Regex(@"\d+"); From 1d55cd09e486ca01221bb103ef3a0589a0429452 Mon Sep 17 00:00:00 2001 From: ChenJie Date: Sun, 8 Sep 2019 18:23:59 +0800 Subject: [PATCH 3/3] Update setup select excel menu item 1.create generate script directory 2.reimport excel after generate script reload --- Assets/QuickSheet/Editor/BaseMachineEditor.cs | 6 ++++++ Assets/QuickSheet/Editor/QuickSheetMenu.cs | 19 ++++++++++++++++--- .../ExcelPlugin/Editor/ExcelMachineEditor.cs | 3 +-- .../ExcelPlugin/Editor/ExcelSettings.cs | 6 ++++++ 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/Assets/QuickSheet/Editor/BaseMachineEditor.cs b/Assets/QuickSheet/Editor/BaseMachineEditor.cs index caf4c9c..ad36b61 100644 --- a/Assets/QuickSheet/Editor/BaseMachineEditor.cs +++ b/Assets/QuickSheet/Editor/BaseMachineEditor.cs @@ -395,5 +395,11 @@ protected ColumnHeader ParseColumnHeader(string columnheader, int order) return new ColumnHeader { name = cHeader, type = CellType.Undefined, OrderNO = order }; } + + public static void CreateGenerateDirectory(BaseMachine machine) + { + Directory.CreateDirectory(Application.dataPath + Path.DirectorySeparatorChar + machine.RuntimeClassPath); + Directory.CreateDirectory(Application.dataPath + Path.DirectorySeparatorChar + machine.EditorClassPath); + } } } \ No newline at end of file diff --git a/Assets/QuickSheet/Editor/QuickSheetMenu.cs b/Assets/QuickSheet/Editor/QuickSheetMenu.cs index 8c22ba0..003264f 100644 --- a/Assets/QuickSheet/Editor/QuickSheetMenu.cs +++ b/Assets/QuickSheet/Editor/QuickSheetMenu.cs @@ -56,6 +56,8 @@ public static void SetupSelectExcels() var path = AssetDatabase.GUIDToAssetPath(guid); if (!IsExcel(path)) continue; + ExcelSettings.Instance._waitImportPath.Add(path); + var excelQuery = new ExcelQuery(path); var sheets = excelQuery.GetSheetNames(); for (int i = 0; i < sheets.Length; i++) @@ -68,20 +70,31 @@ public static void SetupSelectExcels() machine.SpreadSheetName = Path.GetFileName(path); ReimportMachine(machine, true); + BaseMachineEditor.CreateGenerateDirectory(machine); GenerateMachine(machine, false); RenameMachineAsset(machine); selectObjs.Add(machine); Debug.LogFormat("Setup finished! file:{0}, Sheet:{1}", machine.SpreadSheetName, sheets[i]); } - - //reimport excel file to generate data asset and update it - AssetDatabase.ImportAsset(path); } Selection.objects = selectObjs.ToArray(); AssetDatabase.Refresh(); } + + /// + /// After generate script reload, reimport excel file to generate data asset and update it + /// + [UnityEditor.Callbacks.DidReloadScripts] + private static void OnScriptsReloaded() + { + foreach (var path in ExcelSettings.Instance._waitImportPath) + { + AssetDatabase.ImportAsset(path); + } + ExcelSettings.Instance._waitImportPath.Clear(); + } /// /// Refresh all excel files in project diff --git a/Assets/QuickSheet/ExcelPlugin/Editor/ExcelMachineEditor.cs b/Assets/QuickSheet/ExcelPlugin/Editor/ExcelMachineEditor.cs index 0ca224d..b495258 100644 --- a/Assets/QuickSheet/ExcelPlugin/Editor/ExcelMachineEditor.cs +++ b/Assets/QuickSheet/ExcelPlugin/Editor/ExcelMachineEditor.cs @@ -168,8 +168,7 @@ Set a folder under the 'Assets' folder! \n return; } - Directory.CreateDirectory(Application.dataPath + Path.DirectorySeparatorChar + machine.RuntimeClassPath); - Directory.CreateDirectory(Application.dataPath + Path.DirectorySeparatorChar + machine.EditorClassPath); + CreateGenerateDirectory(machine); ScriptPrescription sp = Generate(machine); if (sp != null) diff --git a/Assets/QuickSheet/ExcelPlugin/Editor/ExcelSettings.cs b/Assets/QuickSheet/ExcelPlugin/Editor/ExcelSettings.cs index c2c1380..6284070 100644 --- a/Assets/QuickSheet/ExcelPlugin/Editor/ExcelSettings.cs +++ b/Assets/QuickSheet/ExcelPlugin/Editor/ExcelSettings.cs @@ -8,6 +8,7 @@ using UnityEngine; using UnityEditor; using System.Collections; +using System.Collections.Generic; using System.IO; namespace UnityQuickSheet @@ -32,5 +33,10 @@ public class ExcelSettings : SingletonScriptableObject /// A path where generated editor script files are to be put. /// public string EditorPath = string.Empty; + + /// + /// record import path for QuickSheetMenu + /// + internal List _waitImportPath = new List(); } }