This commit is contained in:
2025-07-24 12:50:20 +08:00
parent 91fd6d30a1
commit 5a05761d81
6 changed files with 176 additions and 11 deletions

View File

@@ -21,6 +21,11 @@ namespace CreatGame
UIManager.Instance.OpenView<UI.UIMainView>(UILayer.Main);
}
public void Destroy()
{
AssetBundle.AssetBundleManager.Instance.ReleaseAll();
}
}
}

View File

@@ -5,6 +5,7 @@ using UnityEngine.AddressableAssets;
using UnityEngine.AddressableAssets.ResourceLocators;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.ResourceManagement.ResourceLocations;
using UnityEngine.U2D;
namespace CreatGame.AssetBundle
{
@@ -15,7 +16,14 @@ namespace CreatGame.AssetBundle
public string assetBundleName;
public GameObject assetBundle;
}
private Dictionary<string, AssetBundleData> assetBundles;
/// <summary>
///
/// </summary>
private Dictionary<string, AssetBundleData> m_AssetBundles;
/// <summary>
///
/// </summary>
private Dictionary<string, SpriteAtlas> m_SpriteAtlasCache;
/// <summary>
/// 是否初始化完成
/// </summary>
@@ -26,7 +34,10 @@ namespace CreatGame.AssetBundle
public AssetBundleManager()
{
IsInitializeAsync = false;
assetBundles = new Dictionary<string, AssetBundleData>();
m_AssetBundles = new Dictionary<string, AssetBundleData>();
m_SpriteAtlasCache = new Dictionary<string, SpriteAtlas>();
Addressables.InitializeAsync();
}
@@ -78,7 +89,7 @@ namespace CreatGame.AssetBundle
/// <param name="callback"></param>
public void LoadGameObjectAsync(string assetBundleName, Action<GameObject> callback)
{
if (assetBundles.TryGetValue(assetBundleName, out var bundle))
if (m_AssetBundles.TryGetValue(assetBundleName, out var bundle))
{
callback.Invoke(GameObject.Instantiate(bundle.assetBundle));
return;
@@ -105,7 +116,7 @@ namespace CreatGame.AssetBundle
/// <param name="assetBundleName"></param>
public GameObject LoadGameObject(string assetBundleName)
{
if (assetBundles.TryGetValue(assetBundleName, out var bundle))
if (m_AssetBundles.TryGetValue(assetBundleName, out var bundle))
{
return GameObject.Instantiate(bundle.assetBundle);
}
@@ -132,8 +143,71 @@ namespace CreatGame.AssetBundle
private AssetBundleData CacheAssetBundles(string bundleName, GameObject bundle = null)
{
var data = new AssetBundleData(){assetBundleName = bundleName, assetBundle = bundle};
assetBundles.Add(bundleName, data);
m_AssetBundles.Add(bundleName, data);
return data;
}
/// <summary>
/// 同步加载图集
/// </summary>
/// <param name="atlasName"></param>
/// <returns></returns>
public SpriteAtlas LoadSpriteAtlas(string atlasName)
{
if (m_SpriteAtlasCache.TryGetValue(atlasName, out var atlas))
{
return atlas;
}
var handle = Addressables.LoadAssetAsync<SpriteAtlas>(atlasName);
handle.WaitForCompletion();
if (handle.Status == AsyncOperationStatus.Succeeded)
{
atlas = handle.Result;
Addressables.Release(handle);
m_SpriteAtlasCache.Add(atlasName, atlas);
return atlas;
}
Debug.LogError($"图集加载失败 atlasName = {atlasName} Status = {handle.Status}");
return null;
}
/// <summary>
/// 异步加载图集
/// </summary>
/// <param name="assetBundleName"></param>
/// <param name="callback"></param>
public void LoadSpriteAsync(string atlasName, Action<SpriteAtlas> callback)
{
if (m_SpriteAtlasCache.TryGetValue(atlasName, out var atlas))
{
callback.Invoke(atlas);
return;
}
Addressables.LoadAssetAsync<SpriteAtlas>(atlasName).Completed += (handle) =>
{
if (handle.Status == AsyncOperationStatus.Succeeded)
{
atlas = handle.Result;
Addressables.Release(handle);
m_SpriteAtlasCache.Add(atlasName, atlas);
}
else
{
Debug.LogError($"图集加载失败 atlasName = {atlasName} Status = {handle.Status}");
callback?.Invoke(null);
}
};
}
/// <summary>
/// 释放所有的资源
/// </summary>
public void ReleaseAll()
{
foreach (var mAssetBundle in m_AssetBundles)
{
GameObject.Destroy(mAssetBundle.Value.assetBundle);
}
m_AssetBundles.Clear();
}
}
}

View File

@@ -0,0 +1,76 @@
using UnityEngine;
using CreatGame.AssetBundle;
using System.Collections.Generic;
using UnityEngine.U2D;
namespace CreatGame.UI
{
/// <summary>
/// ui上的资源加载器
/// </summary>
public class UILoader
{
private List<GameObject> m_GameObjectCache = new List<GameObject>();
private List<Sprite> m_SpriteCache = new List<Sprite>();
/// <summary>
/// 同步资源加载
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public GameObject LoadGameObject(string name)
{
var gameObj = AssetBundleManager.Instance.LoadGameObject(name);
if (gameObj == null)
{
return null;
}
m_GameObjectCache.Add(gameObj);
return gameObj;
}
/// <summary>
/// 异步加载预制件
/// </summary>
/// <param name="name"></param>
/// <param name="callback"></param>
public void LoadGameObjectAsync(string name, System.Action<GameObject> callback)
{
AssetBundleManager.Instance.LoadGameObjectAsync(name, (obj) =>
{
if (obj != null)
{
m_GameObjectCache.Add(obj);
}
callback(obj);
});
}
/// <summary>
///
/// </summary>
/// <param name="atlasName"></param>
/// <param name="spriteName"></param>
/// <returns></returns>
public Sprite LoadSprite(string atlasName, string spriteName)
{
var spriteAtlas = AssetBundleManager.Instance.LoadSpriteAtlas(atlasName);
if (spriteAtlas == null)
{
return null;
}
return spriteAtlas.GetSprite(spriteName);
}
public void DisposeGameObjectCache()
{
var count = m_GameObjectCache.Count;
for (int i = 0; i < count; i++)
{
var obj = m_GameObjectCache[0];
m_GameObjectCache.RemoveAt(0);
GameObject.Destroy(obj);
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: dd77ddfde9dc430eb31244ee82bc2147
timeCreated: 1753323201

View File

@@ -2,7 +2,7 @@
namespace CreatGame.UI
{
public class UIViewBase
public class UIViewBase : UILoader
{
/// <summary>
/// 预制件的Addressables地址
@@ -16,7 +16,7 @@ namespace CreatGame.UI
/// <summary>
/// 导出脚本
/// </summary>
protected UIViewExport MViewExport;
protected UIViewExport m_ViewExport;
/// <summary>
/// 是否加载完成
/// </summary>
@@ -27,7 +27,7 @@ namespace CreatGame.UI
public virtual void PreLoad(GameObject viewObject)
{
m_ViewObject = viewObject;
MViewExport = viewObject.GetComponent<UIViewExport>();
m_ViewExport = viewObject.GetComponent<UIViewExport>();
IsPreLoad = true;
}
/// <summary>
@@ -42,16 +42,17 @@ namespace CreatGame.UI
/// </summary>
public virtual void CloseView()
{
DisposeGameObjectCache();
GameObject.Destroy(m_ViewObject);
}
protected GameObject GetGameObject(string name)
{
for (int i = 0; i < MViewExport.entries.Count; i++)
for (int i = 0; i < m_ViewExport.entries.Count; i++)
{
if (MViewExport.entries[i].key == name)
if (m_ViewExport.entries[i].key == name)
{
return MViewExport.entries[i].prefab;
return m_ViewExport.entries[i].prefab;
}
}

View File

@@ -1,3 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
@@ -17,4 +18,9 @@ public class GameStar : MonoBehaviour
{
}
private void OnDestroy()
{
CreatGame.GameLogic.Instance.Destroy();
}
}