mirror of https://github.com/tuyoogame/YooAsset
update extension sample
parent
b2c9cb3a7e
commit
7a8f344927
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 67ce0f6fc08b0724d95a9f86697bbde3
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,12 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.U2D;
|
||||
|
||||
public class PanelManifest : MonoBehaviour
|
||||
{
|
||||
/// <summary>
|
||||
/// 面板自动引用的图集
|
||||
/// </summary>
|
||||
public List<SpriteAtlas> ReferencesAtlas = new List<SpriteAtlas>();
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: e2537124b11b52a458e01629f6b18f55
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,133 @@
|
|||
#if UNITY_EDITOR
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.U2D;
|
||||
|
||||
public static class UIPanelSettings
|
||||
{
|
||||
/// <summary>
|
||||
/// 面板文件夹GUID
|
||||
/// </summary>
|
||||
private const string UIPanelDirectoryGUID = "12d33f33f3a55224c9c747d7bffa1c68";
|
||||
|
||||
/// <summary>
|
||||
/// 精灵文件夹GUID
|
||||
/// </summary>
|
||||
private const string UISpriteDirectoryGUID = "935d7f20c085cc141a3daf9cacfabfae";
|
||||
|
||||
/// <summary>
|
||||
/// 图集文件夹GUID
|
||||
/// </summary>
|
||||
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<PanelManifest>();
|
||||
if (manifest == null)
|
||||
manifest = go.AddComponent<PanelManifest>();
|
||||
|
||||
RefreshPanelManifest(manifest);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 刷新面板清单
|
||||
/// </summary>
|
||||
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<Image>(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<SpriteAtlas>(atlasAssetPath);
|
||||
if (spriteAtlas == null)
|
||||
{
|
||||
throw new System.Exception($"Not found SpriteAtlas : {atlasAssetPath}");
|
||||
}
|
||||
else
|
||||
{
|
||||
if (manifest.ReferencesAtlas.Contains(spriteAtlas) == false)
|
||||
manifest.ReferencesAtlas.Add(spriteAtlas);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取精灵所属图集
|
||||
/// </summary>
|
||||
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
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 6bed3b2eaa555ec4e9aaa22a888b504c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: eee88a2a10c99aa49b12a0fbff4084f0
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,44 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.U2D;
|
||||
using YooAsset;
|
||||
|
||||
public class SpriteAtlasLoader : MonoBehaviour
|
||||
{
|
||||
private Dictionary<string, SpriteAtlas> _loadedAtlas = new Dictionary<string, SpriteAtlas>(1000);
|
||||
private List<AssetHandle> _loadHandles = new List<AssetHandle>(1000);
|
||||
|
||||
public void Awake()
|
||||
{
|
||||
SpriteAtlasManager.atlasRequested += RequestAtlas;
|
||||
}
|
||||
public void OnDestroy()
|
||||
{
|
||||
foreach (var handle in _loadHandles)
|
||||
{
|
||||
handle.Release();
|
||||
}
|
||||
}
|
||||
|
||||
private void RequestAtlas(string atlasName, Action<SpriteAtlas> callback)
|
||||
{
|
||||
if (_loadedAtlas.TryGetValue(atlasName, out var value))
|
||||
{
|
||||
callback.Invoke(value);
|
||||
}
|
||||
else
|
||||
{
|
||||
var package = YooAssets.GetPackage("DefaultPackage");
|
||||
var loadHandle = package.LoadAssetSync<SpriteAtlas>(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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: a506251ccc863fe4182436d24685c181
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Reference in New Issue