- Hệ thống Addressable cho MJ5 sử dụng để build Assets trong Unity (Images, Audio, Prefabs, Scenes)
- Addressables Version 1.21.19
- Cấu trúc Groups:
- Remote Groups: Groups sẽ được đẩy lên cdn và download trong quá trình mở Unity
- Local Groups: Groups sẽ được build cùng với bản build.
- Remote Groups
- Path lưu trữ: {UnityEngine.Application.persistentDataPath}/Addressables
- Cấu trúc đặt tên group: bắt đầu với GROUPNAME_
- Các groups có cùng prefix sẽ zip lại cùng với nhau.
Lưu ý: remote groups chỉ được dependence với các groups có cùng prefix hoặc local groups.
- Script quản lý Addressable:
namespace Monkey.Support.Addressable
{
public interface IAddressableService
{
/// <summary>
/// Download Addressable Group
/// </summary>
/// <param name="groupName">Group Name</param>
/// <param name="callback">Callback when Download finished</param>
/// <param name="progress">Callback the progress</param>
/// <param name="total_retry">No need to change the value</param>
/// <returns></returns>
IEnumerator DownloadGroup(string groupName, Action<bool> callback, Action<float> progress = null, int total_retry = 0);
bool IsNeedDownload(string groupName);
void LoadAssets<T>(string asset_name, Action<AsyncOperationHandle<T>> callback) where T : UnityEngine.Object;
bool HasKey(string key);
/// <summary>
/// Check when Addressable service ready
/// </summary>
/// <returns></returns>
bool IsServiceActive();
}
}
- Cách download group:
- Kiểm tra xem group có cần download hay không
IAddressableService addressable = SupportSystem.Instance[SupportSystem.ServiceIDs.AddressableService] as IAddressableService;
bool isNeedDownload = addressable.IsNeedDownload("GROUP NAME");
- Download Group
IAddressableService addressable = SupportSystem.Instance[SupportSystem.ServiceIDs.AddressableService] as IAddressableService;
CoroutineRunner.instance.RunCoroutine(addressable.DownloadGroup("GROUP NAME", isDone => {
//Callback khi download thành công
}, progress => {
//Callback download progress
}));
- Build Addressables
Script quản lý build Addressables: RNBuild.cs
Method: static void BuildAddressable(string buildPlatform)
Hàm build addressables thực hiện 3 bước:
- Clean up
- Build Addressable
- Zip and upload to cdn
- Các lỗi thường xảy ra
Lỗi không load được dependencies
OperationException : GroupOperation failed because one of its dependencies failed
RemoteProviderException : Invalid path in AssetBundleProvider:
cách kiểm tra:
- Kiểm tra xem group đã được download hay chưa.
- Kiểm tra xem có assets nào depend đến remote group khác không.
mở file buildlayout.txt trong folder: MonkeyX\Library\com.unity.addressables
Kiểm tra Bundle Dependencies và Expanded Bundle Dependencies