- Home /
Save a prefab from asset bundle
I am writing a game that has downloadable content using asset bundle.
Once it is downloaded we can load it from cache using WWW.LoadFromCacheOrDownload
Is it the correct way to load it from the cache after downloading all the time?
Can the player clear the cache without removing the game? If the player can clear cache we have to save the content to the device. Is there a way to save the downloaded content to a location on the device?
Is it possible to save something like a prefab to the device ?
Answer by tqkiettk10 · Sep 08, 2016 at 10:27 AM
I think you can download assetbundle and save it into device Sample code: IEnumerator downloadAssetbundle(string url, string path) { WWW www = new WWW(url); //link of bundle file yield return www; System.IO.FileStream cache = new System.IO.FileStream(path, System.IO.FileMode.Create); cache.Write(www.bytes, 0, www.bytes.Length); cache.Flush(); cache.Dispose(); cache.Close(); }
And then you can load this assetbundle from device. This just my idea :) Sorry with my bad english
This is a great idea ...
I am using the example listed here; https://unity3d.com/learn/tutorials/topics/scripting/assetbundles-and-assetbundle-manager
This will return "AssetBundleLoadAssetOperation" object. Don't have access to WWW object.
Is there a way to convert unity object into bytes and save it?
something like GameObject to bytes ...
I've always done it the way @tqkiettk10 describes, using Application.persistentDataPath as the root of the path I'm saving the file to. Note that their code does nothing that's specifically related to asset bundles - it simply downloads a file and saves it locally. Then you can load the asset bundle using the same path.
You can also check to see if the file exists locally, before trying to download it.
@wijesijp me to. when you build assetbundle you get 2 file like asset and asset.manifest. Put it into your host and get link. Use my code to download and store it. example: you upload it in to http://unity3d.com/assetbundle/ folder
class Download$$anonymous$$anager : $$anonymous$$onobehaviour
{
string assetbundleUrl = "http://unity3d.com/assetbundle/";
string assetName = "asset";
string path = "/Assetbundles/";
void Start()
{
path = Application.persistentDataPath + path + Utility.GetplatformName() +"/"; // Utility.GetplatformName() in assetbundles-and-assetbundle-manager tutorial. this just get Current platform name. you need using Assetbundles package
StartCoroutine(downloadAssetbundle(assetbundleUrl , assetName, path));
}
IEnumerator downloadAssetbundle(string url, string assetName, string path)
{
// download asset file
WWW www = new WWW(url + assetName);
yield return www;
System.IO.FileStream cache = new System.IO.FileStream(path + assetName, System.IO.File$$anonymous$$ode.Create);
cache.Write(www.bytes, 0, www.bytes.Length);
//download mainfest file
string mainfestName = assetName + ".mainfest";
www = new WWW(url + mainfestName);
yield return www;
cache = new System.IO.FileStream(path + mainfestName, System.IO.File$$anonymous$$ode.Create);
cache.Write(www.bytes, 0, www.bytes.Length);
cache.Flush();
cache.Dispose();
cache.Close();
}
}
hope this help!
This helps a lot.
https://unity3d.com/learn/tutorials/topics/scripting/assetbundles-and-assetbundle-manager
This example resolve assets bundle dependencies. That is why I was thinking of using it.
I can see how the above code can save the data to local device.
Is it possible for you to list some code that I can use to resolve asset dependencies and get the actual object?
For example we have a prefab with a texture on it ....
O$$anonymous$$ i understand what you are trying to do now!
We can use the AssetBundle$$anonymous$$anager to load an asset that is downloaded to the disk from your first code.
For this to work, we have to download all the dependencies from your first code right?
For example if I have a prefab in my asset bundle. Let's say it has a material and a texture. Downloading prefab and it's manifest is not enough right ?
We have to check the manifest of the prefab and download all the dependencies, the material and the texture.
We have to loop through the prefab's manifest and download the dependencies with your first code?
No, if we have prefab and it contain material and texture apply on it, we just need set assetbundle name to the prefab (not set to material and texture) example we set bundle name of the prefab call "asset" https://unity3d.com/sites/default/files/assetbundlename.png , when we build assetbundle we will get 2 file "asset" (don't have extendsion) and "asset.manifest" all the dependencies will automatic add by unity to asset (we can will the file size to see). Upload it to server. Download and store it.
we do't need say anything. Yes just download "asset" file my downloadAssetbundle function will download asset file and auto download asset.manifest file (just put them in same folder in your hosting). add should add some code to check if "assetfile" was downloaded befor call download (using System.IO you can search it) ... so a variables assetBundleName and assetName in LoadAssetDemoScript will like this
public string assetBundleName = "asset"; public string assetName ="name of your prefab";
Your answer
Follow this Question
Related Questions
WWW::Dispose doesn't work 1 Answer
Save Hash128 from Asset Bundle Manifest 2 Answers
AssetBundle, problem whit the version 0 Answers
LoadFromCacheOrDownload is sensibly slower than WWW, why? 1 Answer