Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. IEnumerator DownloadAndCache(string bundleURL)
  2. {
  3. // Wait for the Caching system to be ready
  4. while (!Caching.ready)
  5. {
  6. LandingPage.SetActive(true);
  7. yield return null;
  8. }
  9.  
  10. // if you want to always load from server, can clear cache first
  11. // Caching.CleanCache();
  12.  
  13. // get current bundle hash from server, random value added to avoid caching
  14. UnityWebRequest www = UnityWebRequest.Get(manifestURL);
  15. Debug.Log("Loading manifest:" + manifestURL);
  16.  
  17. LandingPage.SetActive(true);
  18. // wait for load to finish
  19. yield return www.SendWebRequest();
  20.  
  21. // if received error, exit
  22. if (www.isNetworkError == true)
  23. {
  24. tamago.SetActive(false);
  25. ErrorMenu.SetActive(true);
  26. www.Dispose();
  27. www = null;
  28. yield break;
  29. }
  30. else
  31. {
  32.  
  33. }
  34.  
  35. // create empty hash string
  36. Hash128 hashString = (default(Hash128));// new Hash128(0, 0, 0, 0);
  37.  
  38. // check if received data contains 'ManifestFileVersion'
  39. if (www.downloadHandler.text.Contains("ManifestFileVersion"))
  40. {
  41. // extract hash string from the received data, TODO should add some error checking here
  42. var hashRow = www.downloadHandler.text.ToString().Split("\n".ToCharArray())[5];
  43. hashString = Hash128.Parse(hashRow.Split(':')[1].Trim());
  44.  
  45. if (hashString.isValid == true)
  46. {
  47. // we can check if there is cached version or not
  48. if (Caching.IsVersionCached(bundleURL, hashString) == true)
  49. {
  50. Debug.Log("Bundle with this hash is already cached!");
  51. }
  52. else
  53. {
  54. Debug.Log("No cached version founded for this hash..");
  55. }
  56. }
  57. else
  58. {
  59. // invalid loaded hash, just try loading latest bundle
  60. Debug.LogError("Invalid hash:" + hashString);
  61. yield break;
  62. }
  63.  
  64. }
  65. else
  66. {
  67. Debug.LogError("Manifest doesn't contain string 'ManifestFileVersion': " + bundleURL + ".manifest");
  68. yield break;
  69. }
  70.  
  71. // now download the actual bundle, with hashString parameter it uses cached version if available
  72. www = UnityWebRequestAssetBundle.GetAssetBundle(bundleURL, hashString, 0);
  73.  
  74. LandingPage.SetActive(true);
  75. // wait for load to finish
  76. yield return www.SendWebRequest();
  77.  
  78. if (www.error != null)
  79. {
  80. tamago.SetActive(false);
  81. ErrorMenu.SetActive(true);
  82. Debug.LogError("www error: " + www.error);
  83. www.Dispose();
  84. www = null;
  85. yield break;
  86. }
  87. else
  88. {
  89. LandingPage.SetActive(false);
  90. }
  91.  
  92. // get bundle from downloadhandler
  93. Bundle = ((DownloadHandlerAssetBundle)www.downloadHandler).assetBundle;
  94.  
  95. foreach (var item in Bundle.LoadAllAssets(typeof(GameObject)))
  96. {
  97. if (item.name == "face")
  98. faces.Add(item);
  99. if (item.name == "hair")
  100. hairs.Add(item);
  101. if (item.name == "body")
  102. bodies.Add(item);
  103. }
  104.  
  105. InstantiatePet();
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement