I have a server that I don’t have access to (can’t modify structure). I want to load the list of asset bundles from the server, cache and load them in runtime. What is the best way to do this?
Example:
//[
// {
// "id": "01"
// "name": "bundle_01",
// "bundleUri": "https://cdn.example.net/assetbundles/bundle_01",
// "manifestUri": "https://cdn.example.net/assetbundles/bundle_01.manifest",
// "updatedOn": "2020-06-07T15:00:00.000000",
// }
//]
public class CustomBundleData
{
public string name;
public string bundleUri;
public string manifestUri;
public DateTime updatedOn;
}
public class Loader : MonoBehaviour
{
private void Start()
{
const string url = "http://example.com/bundles.json";
StartCoroutine(LoadBundleList(url));
}
private IEnumerator LoadBundleList(string url)
{
var request = UnityWebRequest.Get(url);
yield return request.SendWebRequest();
string json = request.downloadHandler.text;
var list = JsonConvert.DeserializeObject<List<CustomBundleData>>(json);
foreach (CustomBundleData data in list)
{
// if not cached or cache invalid (by 'data.updatedOn'/manifest)
// download
// cache
//
// load bundle
}
}
private void LoadAsset<T>(string key, Action<T> onSuccess, Action onError)
{
Addressables.LoadAssetAsync<T>(key)
.Completed += (AsyncOperationHandle<T> asset) =>
{
if (asset.Status == AsyncOperationStatus.Succeeded)
{
onSuccess(asset.Result);
return;
}
onError();
};
}
}