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

@@ -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;
}
}