From 90fc00a446aa31e6bce75a5e9c92564da43bac7e Mon Sep 17 00:00:00 2001 From: MrGadget <9826063+MrGadget1024@users.noreply.github.com> Date: Mon, 15 Apr 2024 10:52:58 -0400 Subject: [PATCH] feat(MoveToAssetsFolder): Editor Script Moves ScriptTemplates to Assets (#3805) - Ensures ScriptTemplates folder is in Assets root - Allows ScriptTemplates folder to be included in Asset Store package under Mirror folder - Only runs once when Unity first loads project via SessionState flag --- Assets/ScriptTemplates/Editor.meta | 8 ++++ .../Editor/MoveToAssetsFolder.cs | 42 +++++++++++++++++++ .../Editor/MoveToAssetsFolder.cs.meta | 11 +++++ 3 files changed, 61 insertions(+) create mode 100644 Assets/ScriptTemplates/Editor.meta create mode 100644 Assets/ScriptTemplates/Editor/MoveToAssetsFolder.cs create mode 100644 Assets/ScriptTemplates/Editor/MoveToAssetsFolder.cs.meta diff --git a/Assets/ScriptTemplates/Editor.meta b/Assets/ScriptTemplates/Editor.meta new file mode 100644 index 000000000..c9b29c624 --- /dev/null +++ b/Assets/ScriptTemplates/Editor.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 96993aced46357f4d9dbda89e40eabad +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/ScriptTemplates/Editor/MoveToAssetsFolder.cs b/Assets/ScriptTemplates/Editor/MoveToAssetsFolder.cs new file mode 100644 index 000000000..0b7b78a67 --- /dev/null +++ b/Assets/ScriptTemplates/Editor/MoveToAssetsFolder.cs @@ -0,0 +1,42 @@ +using UnityEditor; +using UnityEngine; + +[InitializeOnLoad] +public class MoveToAssetsFolder +{ + const string FirstTimeKey = "MOVE_SCRIPT_TEMPLATES_HAS_RUN"; + const string targetFolder = "ScriptTemplates"; + const string targetPath = "Assets/ScriptTemplates"; + + static MoveToAssetsFolder() + { + if (!SessionState.GetBool(FirstTimeKey, false)) + { + FindAndMoveScriptTemplatesFolder(); + SessionState.SetBool(FirstTimeKey, true); + } + } + + static void FindAndMoveScriptTemplatesFolder() + { + string[] guids = AssetDatabase.FindAssets(targetFolder, null); + foreach (string guid in guids) + { + string path = AssetDatabase.GUIDToAssetPath(guid); + + // Check if it's a folder and not some random asset + if (AssetDatabase.IsValidFolder(path)) + { + // Ensure exact match of the name and that it's not in the Assets folder already + string folderName = System.IO.Path.GetFileName(path); + if (folderName == targetFolder && !path.StartsWith(targetPath)) + { + AssetDatabase.MoveAsset(path, targetPath); + Debug.LogFormat(LogType.Log, LogOption.NoStacktrace, null, $"Moved {targetFolder} to Assets folder."); + } + } + } + + AssetDatabase.Refresh(); + } +} diff --git a/Assets/ScriptTemplates/Editor/MoveToAssetsFolder.cs.meta b/Assets/ScriptTemplates/Editor/MoveToAssetsFolder.cs.meta new file mode 100644 index 000000000..a37b45947 --- /dev/null +++ b/Assets/ScriptTemplates/Editor/MoveToAssetsFolder.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e383132f744a8c1448807b71fa31f7ef +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: