public AssetBundleDownloadFromODROperation(string assetBundleName) : base(assetBundleName) { // Work around Xcode crash when opening Resources tab when a // resource name contains slash character request = OnDemandResources.PreloadAsync(new string[] { assetBundleName.Replace('/', '>') }); }
protected override void FinishDownload() { error = request.error; if (error != null) { return; } var path = "res://" + assetBundleName; var bundle = AssetBundle.CreateFromFile(path); if (bundle == null) { error = string.Format("Failed to load {0}", path); request.Dispose(); } else { assetBundle = new LoadedAssetBundle(bundle); // At the time of unload request is already set to null, so capture it to local variable. var localRequest = request; // Dispose of request only when bundle is unloaded to keep the ODR pin alive. assetBundle.unload += () => { localRequest.Dispose(); }; } request = null; }
/// <summary> /// Disposes of the adaptor. /// </summary> public void Dispose() { Debug.Log("OnDemandResourcesAdaptor.Dispose()"); #if UNITY_IOS || UNITY_TVOS if (m_OnDemandResourcesRequest != null) { m_OnDemandResourcesRequest.Dispose(); m_OnDemandResourcesRequest = null; } #endif if (m_Coroutine != null) { if (AssetBundleLoader.Instance) { AssetBundleLoader.Instance.StopCoroutine(m_Coroutine); } m_Coroutine = null; } m_AssetBundle = null; m_AssetBundleName = null; m_Error = null; m_KeepWaiting = false; }
private IEnumerator LoadAsset(string resourceName, Action <AssetBundle> onResourceLoadedCallback) { // Create the request OnDemandResourcesRequest request = OnDemandResources.PreloadAsync(new string[] { "resource_tag" }); // Wait until request is completed yield return(request); // Check for errors if (request.error != null) { throw new Exception("ODR request failed: " + request.error); } // Get path to the resource and use it. Note that at the moment the only API // that can load ODR or sliced resources is AssetBundle.CreateFromFile() string path = "res://" + resourceName; AssetBundle bundle = AssetBundle.CreateFromFile(path); onResourceLoadedCallback(bundle); // Call Dispose() when resource is no longer needed. This will release a pin on ODR resource. request.Dispose(); }
protected override void FinishDownload() { this.Error = this.request.error; if (this.Error != null) { return; } var path = "res://" + this.AssetBundleName; var bundle = UnityEngine.AssetBundle.LoadFromFile(path); if (bundle == null) { this.Error = string.Format("Failed to load {0}", path); this.request.Dispose(); } else { this.AssetBundle = new LoadedAssetBundle(bundle); // At the time of unload request is already set to null, so capture it to local variable. var localRequest = this.request; // Dispose of request only when bundle is unloaded to keep the ODR pin alive. this.AssetBundle.Unload += () => { localRequest.Dispose(); }; } this.request = null; }
public IEnumerator LoadAsset(string resourceName, string odrTag) { Debug.Log("TTPOnDemandsResources::IosImpl:LoadAsset:resourceName=" + resourceName); // Create the request OnDemandResourcesRequest request = OnDemandResources.PreloadAsync(new string[] { odrTag }); while (!request.isDone) { Debug.Log("TTPOnDemandsResources::IosImpl:LoadAsset:Request:" + resourceName + " - " + (request.progress * 100) + "%"); yield return(new WaitForFixedUpdate()); } // Wait until request is completed //yield return request; // Check for errors if (!string.IsNullOrEmpty(request.error)) { Debug.LogError("TTPOnDemandsResources::IosImpl:LoadAsset: " + request.error); if (Delegate != null) { Delegate.OnODRFailed("{\"bundleName\":\"" + resourceName + "\", \"errorCode\":" + request.error + "}"); } yield break; } var path = request.GetResourcePath(resourceName); Debug.Log("TTPOnDemandsResources::IosImpl:LoadAsset:path=" + path); bool isBundleDownloaded = true; AssetBundle bundle = AssetBundle.LoadFromFile("res://" + resourceName); if (bundle == null) { Debug.LogError("TTPOnDemandsResources::IosImpl:LoadAsset: Bundle is null in res after request " + resourceName); bundle = AssetBundle.LoadFromFile("odr://" + resourceName); if (bundle == null) { Debug.LogError("TTPOnDemandsResources::IosImpl:LoadAsset: Bundle is null in odr after request " + resourceName); isBundleDownloaded = false; if (Delegate != null) { Delegate.OnODRFailed("{\"bundleName\":\"" + resourceName + "\", \"errorCode\":Bundle is null after request}"); } } } if (isBundleDownloaded) { if (Delegate != null) { Delegate.OnODRInstalled("{\"bundleName\":\"" + resourceName + "\"}"); } } request.Dispose(); }
public override bool Update() { if (done) { return(false); } if (request == null || request.isDone) { if (request == null || string.IsNullOrEmpty(request.error)) { FinishDownload(); done = true; return(false); } request.Dispose(); request = OnDemandResources.PreloadAsync(new string[] { assetBundleName.Replace('/', '>') }); } return(true); }
/// <summary> /// Loads the asset bundle. /// </summary> private IEnumerator OnDemandResourcesAdaptorImpl(string assetBundleName, System.Func <string, string> getAssetTagFunc) { if (string.IsNullOrEmpty(assetBundleName)) { throw new System.ArgumentNullException("assetBundleName"); } m_AssetBundle = null; m_AssetBundleName = assetBundleName; m_Error = null; m_KeepWaiting = true; #if UNITY_IOS || UNITY_TVOS string assetTag; if (getAssetTagFunc != null) { assetTag = getAssetTagFunc(assetBundleName); } else { assetTag = assetBundleName; } Debug.LogFormat("[OnDemandResourcesAdaptor] Requesting ODR Tag : {0}", assetTag); m_OnDemandResourcesRequest = OnDemandResources.PreloadAsync(new string[] { assetTag }); while (!m_OnDemandResourcesRequest.isDone) { m_Progress = m_OnDemandResourcesRequest.progress; if (!string.IsNullOrEmpty(m_Error)) { m_AssetBundle = null; m_KeepWaiting = false; m_OnDemandResourcesRequest.Dispose(); m_OnDemandResourcesRequest = null; m_Progress = -1; yield break; } yield return(null); } if (!string.IsNullOrEmpty(m_OnDemandResourcesRequest.error)) { m_AssetBundle = null; m_Error = m_OnDemandResourcesRequest.error; m_KeepWaiting = false; m_OnDemandResourcesRequest.Dispose(); m_OnDemandResourcesRequest = null; m_Progress = -1; yield break; } // TODO: // The documentation isn't very detailed, but it seems like GetResourcePath // should return the path to the asset bundle. As far as I can tell, it // always returns null. In Unity's demo project, they just prefix the asset // bundle name with "res://" (and don't use GetResourcePath). string assetBundlePath; // assetBundlePath = m_OnDemandResourcesRequest.GetResourcePath(assetBundleName); assetBundlePath = "res://" + assetBundleName; if (string.IsNullOrEmpty(assetBundlePath)) { m_AssetBundle = null; m_Error = string.Format("Could not get resource path for asset bundle: {0}", assetBundleName); m_KeepWaiting = false; m_OnDemandResourcesRequest.Dispose(); m_OnDemandResourcesRequest = null; m_Progress = -1; yield break; } // Skip any loading from the adaptor. if (m_DownloadOnly) { Debug.LogFormat("[OnDemandResourcesAdaptor] Bundle loading skipped : {0}", assetBundleName); m_Error = null; m_KeepWaiting = false; m_Progress = 1; yield break; } Debug.LogFormat("[OnDemandResourcesAdaptor] Load asset bundle : {0}", assetBundleName); AssetBundleCreateRequest assetBundleCreateRequest; assetBundleCreateRequest = AssetBundle.LoadFromFileAsync(assetBundlePath); yield return(assetBundleCreateRequest); if (assetBundleCreateRequest.assetBundle == null) { m_AssetBundle = null; m_Error = string.Format("Could not load asset bundle: {0}", assetBundleName); m_KeepWaiting = false; m_OnDemandResourcesRequest.Dispose(); m_OnDemandResourcesRequest = null; m_Progress = -1; yield break; } m_AssetBundle = assetBundleCreateRequest.assetBundle; m_Error = null; m_KeepWaiting = false; m_Progress = 1; yield break; #else if (m_DownloadOnly) { // suppress unused variable warning on non-iOS platforms } m_Error = string.Format("OnDemandResources is not available on the current platform: {0}", Application.platform); m_KeepWaiting = false; m_Progress = -1; yield break; #endif }
public AssetBundleDownloadFromODROperation(string assetBundleName) : base(assetBundleName) { request = OnDemandResources.PreloadAsync(new string[] { assetBundleName }); }