Quantcast
Channel: Questions in topic: "assetbundles"
Viewing all articles
Browse latest Browse all 312

How can i keep my data loaded from asset bundle in the next loaded level ?

$
0
0
Hi there, I created an assetbundle and i put it in the web, the downlaoding works fine, so the loading of the assets, i wrote a method ( callback) after the asset is completely doawnlaoded i sort it in an array of game objects and it works fine also . THE PROBLEM IS : i marked the class that hold that array of objects from bundle with dontdestroyonloading(true); so when the new level is laoded the class remain in the game object but the data intantiated from the assetbundle.mainasset is gone missing !! if instatiate dosen't keep the objects how should i copy or make the objects remain in the new scene even after destroying the www.assetbundle ? Thanks here's some code if you want to know how i do it : using UnityEngine; using System.Collections; using System; public class CacheLoader : MonoBehaviour { public GameObject webdata; public WebData websatascript; public bool Finished { get; internal set; } public delegate void Callback(WWW www); private string modelbundleurl = "https://blablablanlabla/Weapons.unity3d"; public int version = 1; void Start() { websatascript = webdata.GetComponent(); Downloadbundle(modelbundleurl, version, SortModels); } public void Downloadbundle(string webURL, int version , Callback callback) { Debug.Log("start doanloading y url = " + webURL); StartCoroutine(LoadBundle(webURL, version, callback)); Debug.Log("finish downloading = " + webURL); } public IEnumerator LoadBundle(string modelbundleurl, int version , Callback onDownloadFinich) { using (WWW www = WWW.LoadFromCacheOrDownload(modelbundleurl, version)) { yield return www; Debug.Log("cache ready and finish for this url " + modelbundleurl); Finished = true; onDownloadFinich(www); } } public void SortModels(WWW www) { Debug.Log("start sorting models method " + www); Vector3 pos = new Vector3(0, 0, 0); Quaternion rot = new Quaternion(0f, 0f, 0f, 0f); AssetBundle assetBundle = www.assetBundle; GameObject gameObject = assetBundle.mainAsset as GameObject; Transform[] objectsfrombundle = new Transform[60]; Debug.Log("gameobjects from asset bundle mainasset" + gameObject.transform.childCount); int i = 0; Transform[] tab = new Transform[60]; foreach (Transform bundleobj in gameObject.transform) { tab[i] = (Transform)Instantiate(bundleobj.transform, pos, rot) as Transform; websatascript.objectsfrombundle[i] = tab[i].gameObject as GameObject; websatascript.objectsfrombundle[i].SetActive(false); Debug.Log("filling asset bundle in the array" + websatascript.objectsfrombundle[i].name); i++; } Debug.Log("length of the array" + websatascript.objectsfrombundle.Length); assetBundle.Unload(false); www.Dispose(); } } Thanks

Viewing all articles
Browse latest Browse all 312

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>