Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- IEnumerator DownloadAndCache(string bundleURL)
- {
- // Wait for the Caching system to be ready
- while (!Caching.ready)
- {
- LandingPage.SetActive(true);
- yield return null;
- }
- // if you want to always load from server, can clear cache first
- // Caching.CleanCache();
- // get current bundle hash from server, random value added to avoid caching
- UnityWebRequest www = UnityWebRequest.Get(manifestURL);
- Debug.Log("Loading manifest:" + manifestURL);
- LandingPage.SetActive(true);
- // wait for load to finish
- yield return www.SendWebRequest();
- // if received error, exit
- if (www.isNetworkError == true)
- {
- tamago.SetActive(false);
- ErrorMenu.SetActive(true);
- www.Dispose();
- www = null;
- yield break;
- }
- else
- {
- }
- // 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(bundleURL, 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': " + bundleURL + ".manifest");
- yield break;
- }
- // now download the actual bundle, with hashString parameter it uses cached version if available
- www = UnityWebRequestAssetBundle.GetAssetBundle(bundleURL, hashString, 0);
- LandingPage.SetActive(true);
- // wait for load to finish
- yield return www.SendWebRequest();
- if (www.error != null)
- {
- tamago.SetActive(false);
- ErrorMenu.SetActive(true);
- Debug.LogError("www error: " + www.error);
- www.Dispose();
- www = null;
- yield break;
- }
- else
- {
- LandingPage.SetActive(false);
- }
- // get bundle from downloadhandler
- Bundle = ((DownloadHandlerAssetBundle)www.downloadHandler).assetBundle;
- foreach (var item in Bundle.LoadAllAssets(typeof(GameObject)))
- {
- if (item.name == "face")
- faces.Add(item);
- if (item.name == "hair")
- hairs.Add(item);
- if (item.name == "body")
- bodies.Add(item);
- }
- InstantiatePet();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement