IEnumerator CreateFinalObject(ModelsParents modelsParents) { AssetBundleRequest request = modelsParents.Bundle.LoadAssetAsync(modelsParents.Name); yield return(request); GameObject gameObject = request.asset as GameObject; finalObjects = new FinalObjects(); finalObjects.Model = gameObject; finalObjects.Parent = modelsParents.Parent; finalObjects.ModelShowText = modelsParents.ModelShowText; finalObjects.Model.SetActive(false); //Show the Model & Text Instantiate(finalObjects.Model, finalObjects.Parent); finalObjects.Parent.transform .Find(GetFullModelName(finalObjects.Model.name)).gameObject.SetActive(true); modelShowText.text = finalObjects.ModelShowText; grayPanel.SetActive(false); buttonPanel.SetActive(true); }
IEnumerator DownloadModelOrGetFromCache(int pos, Models modelsList) { Debug.Log(pos); // Wait for the Caching system to be ready while (!Caching.ready) { yield return(null); } float maxProgress = 1; float progress = 0; // get current bundle hash from server, random value added to avoid caching UnityWebRequest www = UnityWebRequest.Get(modelsList.body[pos].model_url + ".manifest?r=" + (Random.value * 9999999)); Debug.Log("Loading manifest:" + modelsList.body[pos].model_url + ".manifest"); // wait for load to finish yield return(www.SendWebRequest()); // if received error, exit if (www.isNetworkError == true) { Debug.LogError("www error: " + www.error); www.Dispose(); www = null; yield break; } // create empty hash string Hash128 hashString = (default(Hash128)); // new Hash128(0, 0, 0, 0); // check if received data contains 'ManifestFileVersion' if (www.downloadHandler.text.Contains("ManifestFileVersion")) { // extract hash string from the received data, TODO should add some error checking here var hashRow = www.downloadHandler.text.ToString().Split("\n".ToCharArray())[5]; hashString = Hash128.Parse(hashRow.Split(':')[1].Trim()); if (hashString.isValid == true) { // we can check if there is cached version or not if (Caching.IsVersionCached(modelsList.body[pos].model_url, hashString) == true) { Debug.Log("Bundle with this hash is already cached!"); } else { Debug.Log("No cached version founded for this hash.."); } } else { // invalid loaded hash, just try loading latest bundle Debug.LogError("Invalid hash:" + hashString); yield break; } } else { Debug.LogError("Manifest doesn't contain string 'ManifestFileVersion': " + modelsList.body[pos].model_url + ".manifest"); yield break; } // now download the actual bundle, with hashString parameter it uses cached version if available www = UnityWebRequestAssetBundle.GetAssetBundle( modelsList.body[pos].model_url + "?r=" + (Random.value * 9999999), hashString, 0); // wait for load to finish www.SendWebRequest(); if (www.error != null) { Debug.LogError("www error: " + www.error); www.Dispose(); www = null; yield break; } else { //To remember the last progress float lastProgress = progress; //informationBox.text = "Downloading resources..."; while (!www.isDone) { //Calculate the current progress progress = lastProgress + www.downloadProgress; //Get a percentage float progressPercentage = (progress / maxProgress) * 100; Debug.Log("Downloaded: " + progressPercentage + "%"); yield return(new WaitForSeconds(0.1f)); //slider.value = Mathf.Clamp01(progress / maxProgress); } bundle = DownloadHandlerAssetBundle.GetContent(www); Debug.Log("Download Completed."); //Add the model and parent combination to list for later instantiation ModelsParents modelsParents = new ModelsParents(); //Downloaded Asset Bundle modelsParents.Bundle = bundle; //Asset Bundle Name modelsParents.Name = modelsList.body[pos].prefab_name; //GameObject for the model to be placed in modelsParents.Parent = parent; //Model Show Text modelsParents.ModelShowText = modelsList.body[pos].model_show_text; StartCoroutine(CreateFinalObject(modelsParents)); } }