diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/PandelMonitor.meta b/Assets/YooAsset/Samples~/Extension Sample/Runtime/PandelMonitor.meta new file mode 100644 index 00000000..98676419 --- /dev/null +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/PandelMonitor.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 67ce0f6fc08b0724d95a9f86697bbde3 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/PandelMonitor/PanelManifest.cs b/Assets/YooAsset/Samples~/Extension Sample/Runtime/PandelMonitor/PanelManifest.cs new file mode 100644 index 00000000..517cb07e --- /dev/null +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/PandelMonitor/PanelManifest.cs @@ -0,0 +1,12 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.U2D; + +public class PanelManifest : MonoBehaviour +{ + /// + /// 面板自动引用的图集 + /// + public List ReferencesAtlas = new List(); +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/PandelMonitor/PanelManifest.cs.meta b/Assets/YooAsset/Samples~/Extension Sample/Runtime/PandelMonitor/PanelManifest.cs.meta new file mode 100644 index 00000000..4f86ead2 --- /dev/null +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/PandelMonitor/PanelManifest.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e2537124b11b52a458e01629f6b18f55 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/PandelMonitor/PanelMonitor.cs b/Assets/YooAsset/Samples~/Extension Sample/Runtime/PandelMonitor/PanelMonitor.cs new file mode 100644 index 00000000..83adc5e8 --- /dev/null +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/PandelMonitor/PanelMonitor.cs @@ -0,0 +1,133 @@ +#if UNITY_EDITOR +using System.IO; +using UnityEngine; +using UnityEngine.UI; +using UnityEngine.U2D; + +public static class UIPanelSettings +{ + /// + /// 面板文件夹GUID + /// + private const string UIPanelDirectoryGUID = "12d33f33f3a55224c9c747d7bffa1c68"; + + /// + /// 精灵文件夹GUID + /// + private const string UISpriteDirectoryGUID = "935d7f20c085cc141a3daf9cacfabfae"; + + /// + /// 图集文件夹GUID + /// + private const string UIAtlasDirectoryGUID = "c355c783476322b4cacac98c5e1b46d8"; + + public static string GetPanelDirecotry() + { + string result = UnityEditor.AssetDatabase.GUIDToAssetPath(UIPanelDirectoryGUID); + if (string.IsNullOrEmpty(result)) + { + throw new System.Exception($"Can not found panel direcotry : {UIPanelDirectoryGUID}"); + } + return result; + } + public static string GetSpriteDirecotry() + { + string result = UnityEditor.AssetDatabase.GUIDToAssetPath(UISpriteDirectoryGUID); + if (string.IsNullOrEmpty(result)) + { + throw new System.Exception($"Can not found sprite direcotry : {UISpriteDirectoryGUID}"); + } + return result; + } + public static string GetAtlasDirecotry() + { + string result = UnityEditor.AssetDatabase.GUIDToAssetPath(UIAtlasDirectoryGUID); + if (string.IsNullOrEmpty(result)) + { + throw new System.Exception($"Can not found atlas direcotry : {UIAtlasDirectoryGUID}"); + } + return result; + } +} + +public class UIPanelMonitor : UnityEditor.Editor +{ + [UnityEditor.InitializeOnLoadMethod] + static void StartInitializeOnLoadMethod() + { + UnityEditor.SceneManagement.PrefabStage.prefabSaving += OnPrefabSaving; + } + + static void OnPrefabSaving(GameObject go) + { + UnityEditor.SceneManagement.PrefabStage stage = UnityEditor.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage(); + if (stage != null) + { + string panelDirectory = UIPanelSettings.GetPanelDirecotry(); + if (stage.assetPath.StartsWith(panelDirectory)) + { + PanelManifest manifest = go.GetComponent(); + if (manifest == null) + manifest = go.AddComponent(); + + RefreshPanelManifest(manifest); + } + } + } + + /// + /// 刷新面板清单 + /// + public static void RefreshPanelManifest(PanelManifest manifest) + { + manifest.ReferencesAtlas.Clear(); + + string spriteDirectory = UIPanelSettings.GetSpriteDirecotry(); + string altasDirectory = UIPanelSettings.GetAtlasDirecotry(); + + // 获取依赖的图集名称 + Transform root = manifest.transform; + Image[] allImage = root.GetComponentsInChildren(true); + for (int i = 0; i < allImage.Length; i++) + { + Image image = allImage[i]; + if (image.sprite == null) + continue; + + // 文件路径 + string spriteAssetPath = UnityEditor.AssetDatabase.GetAssetPath(image.sprite); + + // 跳过系统内置资源 + if (spriteAssetPath.Contains("_builtin_")) + continue; + + // 跳过非图集精灵 + if (spriteAssetPath.StartsWith(spriteDirectory) == false) + continue; + + string atlasAssetPath = GetAtlasPath(altasDirectory, spriteAssetPath); + SpriteAtlas spriteAtlas = UnityEditor.AssetDatabase.LoadAssetAtPath(atlasAssetPath); + if (spriteAtlas == null) + { + throw new System.Exception($"Not found SpriteAtlas : {atlasAssetPath}"); + } + else + { + if (manifest.ReferencesAtlas.Contains(spriteAtlas) == false) + manifest.ReferencesAtlas.Add(spriteAtlas); + } + } + } + + /// + /// 获取精灵所属图集 + /// + private static string GetAtlasPath(string atlasDirectory, string assetPath) + { + string directory = Path.GetDirectoryName(assetPath); + DirectoryInfo directoryInfo = new DirectoryInfo(directory); + string atlasName = directoryInfo.Name; + return $"{atlasDirectory}/{atlasName}.spriteatlas"; + } +} +#endif \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/PandelMonitor/PanelMonitor.cs.meta b/Assets/YooAsset/Samples~/Extension Sample/Runtime/PandelMonitor/PanelMonitor.cs.meta new file mode 100644 index 00000000..716a4d98 --- /dev/null +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/PandelMonitor/PanelMonitor.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6bed3b2eaa555ec4e9aaa22a888b504c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/SpriteAtlasLoader.meta b/Assets/YooAsset/Samples~/Extension Sample/Runtime/SpriteAtlasLoader.meta new file mode 100644 index 00000000..4040b650 --- /dev/null +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/SpriteAtlasLoader.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: eee88a2a10c99aa49b12a0fbff4084f0 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/SpriteAtlasLoader/SpriteAtlasLoader.cs b/Assets/YooAsset/Samples~/Extension Sample/Runtime/SpriteAtlasLoader/SpriteAtlasLoader.cs new file mode 100644 index 00000000..18940c73 --- /dev/null +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/SpriteAtlasLoader/SpriteAtlasLoader.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.U2D; +using YooAsset; + +public class SpriteAtlasLoader : MonoBehaviour +{ + private Dictionary _loadedAtlas = new Dictionary(1000); + private List _loadHandles = new List(1000); + + public void Awake() + { + SpriteAtlasManager.atlasRequested += RequestAtlas; + } + public void OnDestroy() + { + foreach (var handle in _loadHandles) + { + handle.Release(); + } + } + + private void RequestAtlas(string atlasName, Action callback) + { + if (_loadedAtlas.TryGetValue(atlasName, out var value)) + { + callback.Invoke(value); + } + else + { + var package = YooAssets.GetPackage("DefaultPackage"); + var loadHandle = package.LoadAssetSync(atlasName); + if (loadHandle.Status != EOperationStatus.Succeed) + { + Debug.LogWarning($"Failed to load sprite atlas : {atlasName} ! {loadHandle.LastError}"); + return; + } + + _loadHandles.Add(loadHandle); + callback.Invoke(loadHandle.AssetObject as SpriteAtlas); + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/SpriteAtlasLoader/SpriteAtlasLoader.cs.meta b/Assets/YooAsset/Samples~/Extension Sample/Runtime/SpriteAtlasLoader/SpriteAtlasLoader.cs.meta new file mode 100644 index 00000000..1a5e3a1e --- /dev/null +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/SpriteAtlasLoader/SpriteAtlasLoader.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a506251ccc863fe4182436d24685c181 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: