123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- public class AssetBundleLoaderTest : MonoBehaviour
- {
-
- public GameObject cube;
- const int version = 2;
- List<Object> loadedObjects = new List<Object>();
- IEnumerator DownloadAndCache(int level)
- {
- string url = AssetBundleUtils.GetAssetBundlesURL() + "/Level" + level + "_Backgrounds.unity3d";
- AVDebug.Log("Loading asset bundle from " + url);
- // Wait for the Caching system to be ready
- while (!Caching.ready)
- {
- yield return null;
- }
- // Load the AssetBundle file from Cache if it exists with the same version or download and store it in the cache
- using (WWW www = WWW.LoadFromCacheOrDownload (url, version))
- {
- while (!www.isDone)
- {
- //AVDebug.Log (www.progress);
- yield return null;
- }
- if (www.error != null)
- {
- throw new System.Exception("WWW download had an error:" + www.error);
- }
- //Clear anything from the previous loaded asset bundle
- foreach (Object loadedObj in loadedObjects)
- {
- AVDebug.Log("Destroying " + loadedObj);
- DestroyImmediate(loadedObj, true);
- }
- loadedObjects.Clear();
- AssetBundle bundle = www.assetBundle;
-
- // Load the object asynchronously
- AssetBundleRequest request = bundle.LoadAssetAsync("texture", typeof(Texture2D));
-
- // Wait for completion
- yield return request;
-
- // Get the reference to the loaded object
- Texture2D obj = request.asset as Texture2D;
- loadedObjects.Add(obj);
- cube.GetComponent<Renderer>().material.mainTexture = obj;
- // Unload the AssetBundles compressed contents to conserve memory
- bundle.Unload(false);
- // Frees the memory from the web stream
- www.Dispose();
- }
- }
- void OnGUI()
- {
- if (GUILayout.Button("Bundle 1"))
- {
- StartCoroutine(DownloadAndCache(1));
- }
-
- if (GUILayout.Button ("Bundle 2"))
- {
- StartCoroutine(DownloadAndCache(2));
- }
- // enforce the fact that we can't purchase products until we retrieve the product data
- //#if UNITY_IPHONE
- // if (_products != null && _products.Count > 0)
- // {
- // if (GUILayout.Button("Purchase Skin 1"))
- // {
- // StoreKitBinding.purchaseProduct("Skin001", 1);
- // }
- // if (GUILayout.Button("Purchase Skin 2"))
- // {
- // StoreKitBinding.purchaseProduct("Skin002", 1);
- // }
- // }
- //#endif
- }
-
- #if UNITY_IPHONE
- #region TESTING FOR HOSTED CONTENT WITH APPLE
- //private List<StoreKitProduct> _products;
-
- void Start()
- {
- //var productIdentifiers = new string[] { "Skin001", "Skin002"};
- //StoreKitBinding.requestProductData(productIdentifiers);
-
- // this is used when you want to validate receipts on your own server or when dealing with iTunes hosted content
- // you must manually call StoreKitBinding.finishPendingTransactions when the download/validation is complete
- //StoreKitManager.autoConfirmTransactions = false;
-
- //StoreKitManager.paymentQueueUpdatedDownloadsEvent += DownloadUpdates;
-
-
- // you cannot make any purchases until you have retrieved the products from the server with the requestProductData method
- // we will store the products locally so that we will know what is purchaseable and when we can purchase the products
- //StoreKitManager.productListReceivedEvent += allProducts =>
- //{
- // AVDebug.Log("received total products: " + allProducts.Count);
- // _products = allProducts;
- //};
- }
- //void DownloadUpdates(List<StoreKitDownload> updates)
- //{
- // foreach (var d in updates)
- // {
- // if (d.downloadState == StoreKitDownloadState.Finished)
- // {
- // AVDebug.Log(d.contentURL + " %" + d.progress);
- // if (!string.IsNullOrEmpty(d.contentURL))
- // {
- // StartCoroutine(LoadFromHostedContent(d.contentURL));
- // }
- // StoreKitBinding.finishPendingTransactions();
- // }
- // }
- //}
- IEnumerator LoadFromHostedContent(string url)
- {
- url += "Contents/Level1_Backgrounds.unity3d";
- // Load the AssetBundle file from Cache if it exists with the same version or download and store it in the cache
- using (WWW www = WWW.LoadFromCacheOrDownload (url, version))
- {
- while (!www.isDone)
- {
- //AVDebug.Log (www.progress);
- yield return null;
- }
- if (www.error != null)
- {
- AVDebug.LogError("WWW download had an error:" + www.error);
- yield break;
- }
- AVDebug.Log("Done!!!!");
- AssetBundle bundle = www.assetBundle;
- // Load the object asynchronously
- AssetBundleRequest request = bundle.LoadAssetAsync("texture0", typeof(Texture2D));
- // Wait for completion
- yield return request;
- // Get the reference to the loaded object
- Texture2D obj = request.asset as Texture2D;
- loadedObjects.Add(obj);
- cube.GetComponent<Renderer>().material.mainTexture = obj;
- // Unload the AssetBundles compressed contents to conserve memory
- bundle.Unload(false);
- // Frees the memory from the web stream
- www.Dispose();
- }
- }
- #endregion
- #endif
- }
|