AssetBundleLoaderTest.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. public class AssetBundleLoaderTest : MonoBehaviour
  6. {
  7. public GameObject cube;
  8. const int version = 2;
  9. List<Object> loadedObjects = new List<Object>();
  10. IEnumerator DownloadAndCache(int level)
  11. {
  12. string url = AssetBundleUtils.GetAssetBundlesURL() + "/Level" + level + "_Backgrounds.unity3d";
  13. AVDebug.Log("Loading asset bundle from " + url);
  14. // Wait for the Caching system to be ready
  15. while (!Caching.ready)
  16. {
  17. yield return null;
  18. }
  19. // Load the AssetBundle file from Cache if it exists with the same version or download and store it in the cache
  20. using (WWW www = WWW.LoadFromCacheOrDownload (url, version))
  21. {
  22. while (!www.isDone)
  23. {
  24. //AVDebug.Log (www.progress);
  25. yield return null;
  26. }
  27. if (www.error != null)
  28. {
  29. throw new System.Exception("WWW download had an error:" + www.error);
  30. }
  31. //Clear anything from the previous loaded asset bundle
  32. foreach (Object loadedObj in loadedObjects)
  33. {
  34. AVDebug.Log("Destroying " + loadedObj);
  35. DestroyImmediate(loadedObj, true);
  36. }
  37. loadedObjects.Clear();
  38. AssetBundle bundle = www.assetBundle;
  39. // Load the object asynchronously
  40. AssetBundleRequest request = bundle.LoadAssetAsync("texture", typeof(Texture2D));
  41. // Wait for completion
  42. yield return request;
  43. // Get the reference to the loaded object
  44. Texture2D obj = request.asset as Texture2D;
  45. loadedObjects.Add(obj);
  46. cube.GetComponent<Renderer>().material.mainTexture = obj;
  47. // Unload the AssetBundles compressed contents to conserve memory
  48. bundle.Unload(false);
  49. // Frees the memory from the web stream
  50. www.Dispose();
  51. }
  52. }
  53. void OnGUI()
  54. {
  55. if (GUILayout.Button("Bundle 1"))
  56. {
  57. StartCoroutine(DownloadAndCache(1));
  58. }
  59. if (GUILayout.Button ("Bundle 2"))
  60. {
  61. StartCoroutine(DownloadAndCache(2));
  62. }
  63. // enforce the fact that we can't purchase products until we retrieve the product data
  64. //#if UNITY_IPHONE
  65. // if (_products != null && _products.Count > 0)
  66. // {
  67. // if (GUILayout.Button("Purchase Skin 1"))
  68. // {
  69. // StoreKitBinding.purchaseProduct("Skin001", 1);
  70. // }
  71. // if (GUILayout.Button("Purchase Skin 2"))
  72. // {
  73. // StoreKitBinding.purchaseProduct("Skin002", 1);
  74. // }
  75. // }
  76. //#endif
  77. }
  78. #if UNITY_IPHONE
  79. #region TESTING FOR HOSTED CONTENT WITH APPLE
  80. //private List<StoreKitProduct> _products;
  81. void Start()
  82. {
  83. //var productIdentifiers = new string[] { "Skin001", "Skin002"};
  84. //StoreKitBinding.requestProductData(productIdentifiers);
  85. // this is used when you want to validate receipts on your own server or when dealing with iTunes hosted content
  86. // you must manually call StoreKitBinding.finishPendingTransactions when the download/validation is complete
  87. //StoreKitManager.autoConfirmTransactions = false;
  88. //StoreKitManager.paymentQueueUpdatedDownloadsEvent += DownloadUpdates;
  89. // you cannot make any purchases until you have retrieved the products from the server with the requestProductData method
  90. // we will store the products locally so that we will know what is purchaseable and when we can purchase the products
  91. //StoreKitManager.productListReceivedEvent += allProducts =>
  92. //{
  93. // AVDebug.Log("received total products: " + allProducts.Count);
  94. // _products = allProducts;
  95. //};
  96. }
  97. //void DownloadUpdates(List<StoreKitDownload> updates)
  98. //{
  99. // foreach (var d in updates)
  100. // {
  101. // if (d.downloadState == StoreKitDownloadState.Finished)
  102. // {
  103. // AVDebug.Log(d.contentURL + " %" + d.progress);
  104. // if (!string.IsNullOrEmpty(d.contentURL))
  105. // {
  106. // StartCoroutine(LoadFromHostedContent(d.contentURL));
  107. // }
  108. // StoreKitBinding.finishPendingTransactions();
  109. // }
  110. // }
  111. //}
  112. IEnumerator LoadFromHostedContent(string url)
  113. {
  114. url += "Contents/Level1_Backgrounds.unity3d";
  115. // Load the AssetBundle file from Cache if it exists with the same version or download and store it in the cache
  116. using (WWW www = WWW.LoadFromCacheOrDownload (url, version))
  117. {
  118. while (!www.isDone)
  119. {
  120. //AVDebug.Log (www.progress);
  121. yield return null;
  122. }
  123. if (www.error != null)
  124. {
  125. AVDebug.LogError("WWW download had an error:" + www.error);
  126. yield break;
  127. }
  128. AVDebug.Log("Done!!!!");
  129. AssetBundle bundle = www.assetBundle;
  130. // Load the object asynchronously
  131. AssetBundleRequest request = bundle.LoadAssetAsync("texture0", typeof(Texture2D));
  132. // Wait for completion
  133. yield return request;
  134. // Get the reference to the loaded object
  135. Texture2D obj = request.asset as Texture2D;
  136. loadedObjects.Add(obj);
  137. cube.GetComponent<Renderer>().material.mainTexture = obj;
  138. // Unload the AssetBundles compressed contents to conserve memory
  139. bundle.Unload(false);
  140. // Frees the memory from the web stream
  141. www.Dispose();
  142. }
  143. }
  144. #endregion
  145. #endif
  146. }