- ☀️博客主页:CSDN博客主页
- 💨本文由 萌萌的小木屋 原创,首发于 CSDN💢
- 🔥学习专栏推荐:面试汇总
- ❗️游戏框架专栏推荐:游戏实用框架专栏
- ⛅️点赞 👍 收藏 ⭐留言 📝,如有错误请指正
📆 未来很长,值得我们全力奔赴更美好的生活✨
------------------❤️分割线❤️-------------------------
目的:自己工作期间凭当前水准自己写的代码框架,持续更新中,方便以后自己使用,现在开源,需要自取
需求:Addressable这个官方插件很好用,但是还是需要自己在外层封装一个简单的管理器,用来管理资源的卸载时机,下面就是我工作中常用要的一些方法,会继续扩展
类
AddressablesMgr
方法
LoadAsset:加载GameObject
LoadAndIns:加载并初始化GameObject
LoadAsset<T>:加载泛型资源
Release:释放某一个资源
ReleaseAll:释放所有资源
源代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MFramework;
using UnityEngine.AddressableAssets;
using UnityEngine.Events;
using UnityEngine.ResourceManagement.AsyncOperations;
using System.Threading.Tasks;
using Migu.Atlas;
namespace Migu.Atlas
{
public class AddressablesMgr : TMonoSingleton<AddressablesMgr>, IManager
{
private Dictionary<string,AsyncOperationHandle> m_AllResDic = new Dictionary<string, AsyncOperationHandle>();
public void OnInit()
{
//m_AllAssets = new Dictionary<string, object>();
}
/// <summary>
/// ͬ�����أ����ͷ�
/// </summary>
/// <param name="assetName"></param>
/// <returns></returns>
public GameObject LoadAsset(string assetName)
{
if (!m_AllResDic.ContainsKey(assetName))
{
AsyncOperationHandle asyncOperationHandle = Addressables.LoadAssetAsync<GameObject>(assetName);
asyncOperationHandle.WaitForCompletion();
return (GameObject)asyncOperationHandle.Result;
}
else
{
return (GameObject)m_AllResDic[assetName].Result;
}
}
/// <summary>
/// ͬ�����أ�����ʼ��
/// </summary>
/// <param name="assetName"></param>
/// <param name="parent"></param>
/// <returns></returns>
public GameObject LoadAndIns(string assetName, Transform parent = null)
{
GameObject obj = LoadAsset(assetName);
return Instantiate(obj, parent);
}
public T LoadAsset<T>(string assetName)
{
if (!m_AllResDic.ContainsKey(assetName))
{
AsyncOperationHandle asyncOperationHandle = Addressables.LoadAssetAsync<T>(assetName);
asyncOperationHandle.WaitForCompletion();
return (T)asyncOperationHandle.Result;
}
else
{
return (T)m_AllResDic[assetName].Result;
}
}
public void Release(string name)
{
if (m_AllResDic.ContainsKey(name)) Addressables.Release(name);
else Debug.LogError($"未找到需要释放的资源{name}");
}
public void ReleaseAll()
{
foreach (var item in m_AllResDic)
{
Addressables.Release(item);
}
m_AllResDic.Clear();
}
}
}
GitCode地址
有用点个Fork啊
更新记录
2023-5-30 更新了基础常用的Api