From ae2d67d23d15a11db2ecb08f2ab68a222d662907 Mon Sep 17 00:00:00 2001 From: max Date: Sun, 27 Dec 2020 18:09:11 +0100 Subject: [PATCH] AssetBuilder Menu Added editor menus to control, generate and clear the generated assets. --- .../Scripts/Editor/VA_AnimationBookEditor.cs | 2 +- Editor/Scripts/VA_AssetBuilder.cs | 167 ++++++++++++------ 2 files changed, 113 insertions(+), 56 deletions(-) diff --git a/Editor/Scripts/Editor/VA_AnimationBookEditor.cs b/Editor/Scripts/Editor/VA_AnimationBookEditor.cs index cb05e32..c09825e 100644 --- a/Editor/Scripts/Editor/VA_AnimationBookEditor.cs +++ b/Editor/Scripts/Editor/VA_AnimationBookEditor.cs @@ -208,7 +208,7 @@ namespace TAO.VertexAnimation.Editor { if (GUILayout.Button("build assets", EditorStyles.miniButtonLeft)) { - VA_AssetBuilder.GeneratePlayData(); + VA_AssetBuilder.GenerateBuildData(); } if (GUILayout.Button("clear assets", EditorStyles.miniButtonRight)) diff --git a/Editor/Scripts/VA_AssetBuilder.cs b/Editor/Scripts/VA_AssetBuilder.cs index 7595c08..d2378f6 100644 --- a/Editor/Scripts/VA_AssetBuilder.cs +++ b/Editor/Scripts/VA_AssetBuilder.cs @@ -10,46 +10,99 @@ namespace TAO.VertexAnimation.Editor [InitializeOnLoad] public class VA_AssetBuilder : IPreprocessBuildWithReport, IPostprocessBuildWithReport { + private const string parentFolder = "Assets"; + private const string childFolder = "VA_AssetBuilder"; + private const string folderPath = parentFolder + "/" + childFolder; + + #region EditorMenus + private const string buildClearKey = "VA_AssetsBuilderClearBuildAssets"; + private const string editorGenerateKey = "VA_AssetsBuilderGenerateEditorAssets"; + private const string editorClearKey = "VA_AssetsBuilderClearEditorAssets"; + + private const string editorMenuFolder = "TAO/Vertex Animation"; + private const string buildClearMenuName = editorMenuFolder + "/ClearBuildAssets"; + private const string editorGeneratePlayMenuName = editorMenuFolder + "/GenerateEditorAssetsOnStartPlay"; + private const string editorClearPlayMenuName = editorMenuFolder + "/ClearEditorAssetsOnEndPlay"; + + public static bool ClearBuildAssets + { + get { return EditorPrefs.GetBool(buildClearKey, true); } + set { EditorPrefs.SetBool(buildClearKey, value); } + } + + [MenuItem(buildClearMenuName)] + private static void ToggleClearBuildAssetsAction() + { + ClearBuildAssets = !ClearBuildAssets; + } + + [MenuItem(buildClearMenuName, true)] + private static bool ToggleClearBuildAssetsValidate() + { + Menu.SetChecked(buildClearMenuName, ClearBuildAssets); + return true; + } + + public static bool GenerateEditorPlayModeAssets + { + get { return EditorPrefs.GetBool(editorGenerateKey, true); } + set { EditorPrefs.SetBool(editorGenerateKey, value); } + } + + [MenuItem(editorGeneratePlayMenuName)] + private static void ToggleGenerateEditorPlayModeAssetsAction() + { + GenerateEditorPlayModeAssets = !GenerateEditorPlayModeAssets; + } + + [MenuItem(editorGeneratePlayMenuName, true)] + private static bool ToggleGenerateEditorPlayModeAssetsValidate() + { + Menu.SetChecked(editorGeneratePlayMenuName, GenerateEditorPlayModeAssets); + return true; + } + + public static bool ClearEditorPlayModeAssets + { + get { return EditorPrefs.GetBool(editorClearKey, true); } + set { EditorPrefs.SetBool(editorClearKey, value); } + } + + [MenuItem(editorClearPlayMenuName)] + private static void ToggleClearEditorPlayModeAssetsAction() + { + ClearEditorPlayModeAssets = !ClearEditorPlayModeAssets; + } + + [MenuItem(editorClearPlayMenuName, true)] + private static bool ToggleClearEditorPlayModeAssetsValidate() + { + Menu.SetChecked(editorClearPlayMenuName, ClearEditorPlayModeAssets); + return true; + } + #endregion + + #region EditorPlayMode static VA_AssetBuilder() { EditorApplication.playModeStateChanged += OnPlayModeEnter; } - public int callbackOrder => 0; - private static bool deleteGeneratedAssets = true; - - private const string parentFolder = "Assets"; - private const string childFolder = "VA_AssetBuilder"; - private static string FolderPath => string.Format("{0}/{1}", parentFolder, childFolder); - - public void OnPreprocessBuild(BuildReport report) - { - GeneratePlayData(); - Debug.Log("VA_AssetBuilder generated play data."); - } - - public void OnPostprocessBuild(BuildReport report) - { - if(!deleteGeneratedAssets) - { - return; - } - - ClearBuildData(); - Debug.Log("VA_AssetBuilder cleared play data."); - } - private static void OnPlayModeEnter(PlayModeStateChange state) { switch (state) { case PlayModeStateChange.EnteredEditMode: - //ClearBuildData(); - //Debug.Log("VA_AssetBuilder cleared editor data."); + if (ClearEditorPlayModeAssets) + { + ClearBuildData(); + } break; case PlayModeStateChange.ExitingEditMode: - //GeneratePlayData(); - GenerateEditorData(); + if (GenerateEditorPlayModeAssets) + { + GenerateBuildData(); + } Debug.Log("VA_AssetBuilder generated editor data."); break; case PlayModeStateChange.EnteredPlayMode: @@ -60,26 +113,32 @@ namespace TAO.VertexAnimation.Editor break; } } + #endregion - public static void GenerateEditorData() + #region BuildProcess + public int callbackOrder => 0; + + public void OnPreprocessBuild(BuildReport report) { - string filter = string.Format("t:{0}", typeof(VA_AnimationBook).Name); - string[] guids = AssetDatabase.FindAssets(filter); - - foreach (var guid in guids) - { - VA_AnimationBook book = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(guid), typeof(VA_AnimationBook)) as VA_AnimationBook; - - // Generate run time data. - //GenerateTextures(ref book); - - // Assign run time data. - ConvertEditorDataToPlayData(ref book); - } + GenerateBuildData(); + Debug.Log("VA_AssetBuilder generated play data."); } - [MenuItem("Assets/TAO Vertex Animation/Generate Build Data", false, 65)] - public static void GeneratePlayData() + public void OnPostprocessBuild(BuildReport report) + { + if(!ClearBuildAssets) + { + return; + } + + ClearBuildData(); + Debug.Log("VA_AssetBuilder cleared play data."); + } + #endregion + + #region MainFunctions + [MenuItem(editorMenuFolder + "/Generate Build Data", false, 65)] + public static void GenerateBuildData() { string filter = string.Format("t:{0}", typeof(VA_AnimationBook).Name); string[] guids = AssetDatabase.FindAssets(filter); @@ -95,21 +154,16 @@ namespace TAO.VertexAnimation.Editor ConvertEditorDataToPlayData(ref book); // Save them to disk. - if (!AssetDatabase.IsValidFolder(FolderPath)) + if (!AssetDatabase.IsValidFolder(folderPath)) { - deleteGeneratedAssets = true; AssetDatabase.CreateFolder(parentFolder, childFolder); } - else - { - deleteGeneratedAssets = false; - } // Generate run time data. List savedAssets = new List(); foreach (var t in book.editorData.texture2DArray) { - string assetPath = string.Format("{0}/{1}.asset", FolderPath, t.name); + string assetPath = string.Format("{0}/{1}.asset", folderPath, t.name); // Delete existing asset. if (!string.IsNullOrEmpty(AssetDatabase.AssetPathToGUID(assetPath))) @@ -132,7 +186,7 @@ namespace TAO.VertexAnimation.Editor } } - [MenuItem("Assets/TAO Vertex Animation/Clear Build Data", false, 66)] + [MenuItem(editorMenuFolder + "/Clear Build Data", false, 66)] public static void ClearBuildData() { string filter = string.Format("t:{0}", typeof(VA_AnimationBook).Name); @@ -146,14 +200,16 @@ namespace TAO.VertexAnimation.Editor } // Remove generated assets from disk. - if (AssetDatabase.IsValidFolder(FolderPath)) + if (AssetDatabase.IsValidFolder(folderPath)) { - AssetDatabase.DeleteAsset(FolderPath); + AssetDatabase.DeleteAsset(folderPath); } AssetDatabase.SaveAssets(); } + #endregion + #region GenerationHelperFunctions // Assign the texture ID's to the texture entries. private static void ReferenceDuplicates(ref VA_AnimationBook book) { @@ -323,6 +379,7 @@ namespace TAO.VertexAnimation.Editor } book.playData.texture2DArray = book.editorData.texture2DArray; - } + } + #endregion } }