How do I access On Demand Resources on Unity application for IOS?

91 Views Asked by At

I have been trying to access the On Demand Resources in a unity project for IOS. I followed the documentation for IOS app thinning, and wrote the following code to load the bundle into an AssetBundle variable:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.iOS;
using System;

public class AssetBundleIterator : MonoBehaviour
{
    public string bundleName;
    AssetBundle bundle;
    bool loadedBundle;
    OnDemandResourcesRequest bundleRequest;


    void Awake()
    {
        Initialize();
        //StartCoroutine(LoadGameInEditor());
        StartCoroutine(LoadBundle());
    }

    private IEnumerator LoadBundle()
    {
        bundleRequest = OnDemandResources.PreloadAsync( new string[] { bundleName } );
        yield return bundleRequest;
        if (bundleRequest.error != "")
        {
            errorText.text = "ODR request failed: " + bundleRequest.error;
            throw new Exception( "ODR request failed: " + bundleRequest.error );
        }
        string path = bundleRequest.GetResourcePath(bundleName);
        bundle = AssetBundle.LoadFromFile( path + bundleName );
        bundleRequest.Dispose();
        loadedBundle = true;
    }

The asset bundles were assigned their names as their tags.

When I start the application, I cannot access the asset bundle, even though bundleRequest did not throw any exceptions. I should be able to load a file, for example, a sprite, from the asset bundle using this code:

    AssetBundleRequest assetRequest = new AssetBundleRequest();
    try 
    {
        assetRequest = bundle.LoadAssetWithSubAssetsAsync<T>(_assetName);
    } catch 
    {
        Debug.LogWarning("AssetBundle Not Found");
    }
    yield return assetRequest;

But it does not load anything.

1

There are 1 best solutions below

0
On
string path = bundleRequest.GetResourcePath(bundleName);

This returns empty, use:

assetbundle = AssetBundle.LoadFromFile("res://" + bundleName);