// The directory for the asset must have already been created IEnumerator <Null> PrecacheCoroutine(AssetGetter request) { string assetId = request.Asset.Id; var cr = request.GetAssetCoroutine(); while (true) { try { bool result = cr.MoveNext(); if (!result) { break; } } catch (VrAssetServiceException e) { ControllerConsoleScript.m_Instance.AddNewLine(e.Message); Debug.LogException(e); yield break; } yield return(cr.Current); } while (!request.IsReady) { yield return(null); } request.Asset.WriteToDisk(); string path = Path.Combine(GetCacheDirectoryForAsset(assetId), request.Asset.RootFilePath); m_ModelsByAssetId[assetId] = new Model(Model.Location.PolyAsset(assetId, path)); }
/// Request loading a model with a given Poly Asset ID. /// Pass the reason the Model is being pulled into memory, for logging purposes. /// /// Upon completion, the asset will: /// - be in "failed download" state (don't know how to check for this) /// - be in "download succeeded, load failed" state (check Model.Error != null) /// - be in "download succeeded, load succeeded" state (check Model.m_Valid) /// /// The intent is that this method will ignore previous failures and try again. /// If you don't want to retry a failed load-into-memory, you should check Model.Error first. /// If you aren't trying to do a hot-reload, you should check Model.m_Valid first. public void RequestModelLoad(string assetId, string reason) { // Don't attempt to load models which are already loading. if (IsLoading(assetId)) { return; } if (m_ModelsByAssetId.ContainsKey(assetId)) { // Already downloaded. // It may be in memory already, but it's safe to ask for it to be brought in again. // That way we get the behavior of "ignore a failed load-into-memory" m_RequestLoadQueue.Add(new ModelLoadRequest(m_ModelsByAssetId[assetId], reason)); m_IsLoadingMemo.Add(assetId); } else { // Not downloaded yet. // Kick off a download; when done the load will and arrange for the download-complete to kick off the // load-into-memory work. string assetDir = GetCacheDirectoryForAsset(assetId); try { // For the case that the folder exists, but the files were removed. if (!Directory.Exists(assetDir)) { Directory.CreateDirectory(assetDir); } } catch (UnauthorizedAccessException) { Debug.LogError("Cannot create directory for online asset download."); } // Then request the asset from Poly. AssetGetter request = VrAssetService.m_Instance.GetAsset( assetId, VrAssetFormat.GLTF2, reason); StartCoroutine(request.GetAssetCoroutine()); m_ActiveRequests.Add(request); m_IsLoadingMemo.Add(assetId); } }