From: http://blog.sina.com.cn/s/blog_471132920101gz8z.html
original articles should be reproduced please specify: Reproduced from the Wind Yu Chong Unity3d Tutorial College
assetbundles First Lecture: basic use
Assetbundles is exporting the assets you choose from unity, which uses a unique compression format and the app can read it in real time. This includes any asset type, such as model map audio, or even the entire scene. The compression size basically can achieve the zip effect. Assetbundles is designed to be easily downloaded into the app. If you want to include custom binary data, you'll use the. bytes suffix, and unity will import them as textassets. Note: Assetbundles is not compatible with various unity versions, as unity officials say. The AB, which was tested in Unity4, is used normally in 4, but it does not work properly in 3.5.0. The instructions are not backwards compatible with AB.
Development phase:
(1) Create Assetbundles:
Can't be scene objects's objects useBuildpipeline.buildassetbundleTargetPlatform to specify, cannot use default parameters, default mode is Webplayer
- Using Unityengine;
- Using Unityeditor;
- public class Exportassetbundles {
- [MenuItem ("Assets/build assetbundle from Selection-track dependencies")]
- static void Exportresource () {
- Bring up Save panel
- String path = Editorutility.savefilepanel ("Save Resource", "", "New Resource", "Unity3d");
- if (path. Length! = 0) {
- Build the resource file from the active selection.
- object[] selection = selection.getfiltered (typeof (Object), selectionmode.deepassets);
- Buildpipeline.buildassetbundle (Selection.activeobject, Selection,
- Path, Buildassetbundleoptions.collectdependencies |buildassetbundleoptions.completeassets
- , buildtarget.standalonewindows);
- Selection.objects = Selection;
- }
- }
- [MenuItem ("Assets/build Assetbundle from Selection-no Dependency tracking")]
- static void Exportresourcenotrack () {
- Bring up Save panel
- String path = Editorutility.savefilepanel ("Save Resource", "", "New Resource", "Unity3d");
- if (path. Length! = 0) {
- Build the resource file from the active selection.
- Buildpipeline.buildassetbundle (Selection.activeobject, selection.objects, Path);
- }
- }
- }
BOOL Buildpipeline.buildassetbundleexplicitassetnames (object[] assets, string[] assetname, String pathName, Buildassetbundleoptions assetbundleoptions, Buildtarget targetplatform)
Create bundles and customize names. The length and arrangement of the assetname correspond to the assets. Does not really change the name of the object, but only in the assetbundle.load when there is a unique name
The above code only needs to modify line 11th. Will Buildpipeline.buildassetbundle (Selection.activeobject, Selection,
Path, Buildassetbundleoptions.collectdependencies | Buildassetbundleoptions.completeassets
, buildtarget.standalonewindows); The first parameter in the Mainasset is removed, and after selection is object[] Add string[] Assetname can
- [MenuItem ("Assets/build assetbundle from Selection names-track dependencies")]
- static void Exportresourcewithnames () {
- Bring up Save panel
- String path = Editorutility.savefilepanel ("Save Resource", "", "New Resource", "Unity3d");
- if (path. Length! = 0) {
- Build the resource file from the active selection.
- object[] selection = selection.getfiltered (typeof (Object), selectionmode.deepassets);
- string[] names = new string[selection. Length];
- for (int i =0;i
- {
- Names[i] = i+ "_" + selection[i].name;
- }
- Buildpipeline.buildassetbundleexplicitassetnames (Selection,names,
- Path, Buildassetbundleoptions.collectdependencies |buildassetbundleoptions.completeassets
- , buildtarget.standalonewindows);
- Selection.objects = Selection;
- }
- }
- Using System;
- Using System.IO;
- Using Unityengine;
- public class Loadassetbundle:monobehaviour {
- Private Assetbundle Assetbundle;
- private Assetbundlecreaterequest request;
- void Update () {
- }
- void Ongui ()
- {
- if (GUI. button (new Rect (0,0,100,50), "Load"))
- {
- byte[] bs = file.readallbytes (application.datapath+ "/withnames.unity3d");
- Request = Assetbundle.createfrommemory (BS);
- }
- if (GUI. button (new Rect (0,50,100,50), "Check"))
- {
- if (Request.isdone = = True)
- {
- Assetbundle = Request.assetbundle;
- Unityengine.object obj = assetbundle.load ("0_cube");
- Instantiate (obj);
- }
- }
- }
When the scene is made AB,Buildpipeline.buildstreamedsceneassetbundlePackage one or more scenarios into the asset bundle, which can be used for any platform and always create a single compressed. unity3d file. After downloading, you can use WWW.LoadFromCacheOrDownload to read from the cache.
- Using Unityengine;
- Using Unityeditor;
- public class Buildscene:monobehaviour {
- [MenuItem ("build/buildwebplayerstreamed")]
- static void Buildscenes ()
- {
- string [] levels = new STRING[1];
- Levels[0] = "assets/scene.unity";
- Buildpipeline.buildstreamedsceneassetbundle (Levels, "Assets/mylevel.unity3d", Buildtarget.standaloneosxintel);
- }
- }
Compatibility
Platform Compatibility for Assetbundles |
|
Standalone |
Webplayer |
Ios |
Android |
Editor |
Y |
Y |
Y |
Y |
Standalone |
Y |
Y |
|
|
Webplayer |
Y |
Y |
|
|
Ios |
|
|
Y |
|
Android |
|
|
|
Y |
(2) Upload assetbundles: Decide how to upload based on the server you are using.
Use stage:
(1) Download assetbundles:
1 Assetbundle.createfromfile: (1) can only be used for PC and Mac standalone (2) only support uncompressed: that is, when the build buildoption also add Uncompressedassetbundle. (3) must be an absolute path. Assetbundle Assetbundle = Assetbundle.createfromfile ("D:/mybundle4.unity3d");
2 assetbundle.createfrommemory (BS);
- Using System;
- Using System.IO;
- Using Unityengine;
- public class Loadassetbundle:monobehaviour {
- Private Assetbundle Assetbundle;
- private Assetbundlecreaterequest request;
- void Update () {
- if (request!=null)
- Print (request.progress);
- }
- void Ongui ()
- {
- if (GUI. button (new Rect (0,0,100,50), "Load"))
- {
- byte[] bs = file.readallbytes ("D:/mybundle.unity3d");
- Request = Assetbundle.createfrommemory (BS);
- }
- if (GUI. button (new Rect (0,50,100,50), "Check"))
- {
- if (Request.isdone = = True)
- {
- Print ("Name:" +request.assetbundle.mainasset.name);
- Instantiate (Request.assetBundle.mainAsset);
- }
- }
- }
- }
3 Assetbundle bundle = Www.assetBundle;Note: WWW is also capable of reading local files,You only need to file://in front of the path, such aswww mywww = new www ("file://E://LSY/wamp/www/cat.jpg");
- Using System;
- Using System.IO;
- Using Unityengine;
- Using System.Collections;
- public class Loadassetbundle:monobehaviour {
- Private Assetbundle Assetbundle;
- private string address = "Http://127.0.0.1/AssetBundles/myBundle.unity3d";
- void Ongui ()
- {
- if (GUI. button (new Rect (0,0,100,50), "Load Web"))
- {
- Startcoroutine (Load ());
- }
- }
- IEnumerator Load () {
- Download the file from the URL. It is not being saved in the Cache
- String Assetname= "";
- www www = new www (address);
- yield return www;
- if (www.error! = null)
- throw new Exception ("WWW download had an error:" + www.error);
- Assetbundle bundle = Www.assetBundle;
- if (Assetname = = "")
- Instantiate (Bundle.mainasset);
- Else
- Instantiate (bundle. Load (Assetname));
- Unload the Assetbundles compressed contents to conserve memory
- Bundle. Unload (FALSE);
- }
- }
WWW.LoadFromCacheOrDownload
The download process can be replaced, and ensure that the version is consistent, Webplayer cache limit within 50MB.
With the normal www download is just a code of difference
www www = new www (address);
www www = WWW.LoadFromCacheOrDownload (address,1);
- Using System;
- Using System.IO;
- Using Unityengine;
- Using System.Collections;
- public class Loadassetbundle:monobehaviour {
- Private Assetbundle Assetbundle;
- private string address = "Http://127.0.0.1/AssetBundles/myBundle.unity3d";
- void Ongui ()
- {
- if (GUI. button (new Rect (0,0,100,50), "Load Web"))
- {
- Startcoroutine (Load ());
- }
- }
- IEnumerator Load () {
- String Assetname= "";
- www www = WWW.LoadFromCacheOrDownload (address,1);
- yield return www;
- if (www.error! = null)
- throw new Exception ("WWW download had an error:" + www.error);
- Assetbundle bundle = Www.assetBundle;
- if (Assetname = = "")
- Instantiate (Bundle.mainasset);
- Else
- Instantiate (bundle. Load (Assetname));
- Unload the Assetbundles compressed contents to conserve memory
- Bundle. Unload (FALSE);
- }
- }
(2) Use the resources in Assetbundles:
Whether the bool Assetbundle.contains (string name) bundle contains a asset named name
Object assetbundle.load (string name) reads the asset named name in the bundle
Object Assetbundle.loadall ()
unityengine.object[] Objs = Assetbundle.loadall ();
In this case, Assetbundle.mainasset is gameobject, but does not represent Assetbundle.loadall (); The first data in an array of return values is Gameobject, or obj[0] equals Assetbundle.mainasset
Assetbundle.mainasset
Assetbundle.unload (bool unloadallloadedobjects)
Empty all the resources in the bundle. Frees the associated memory. After emptying, you can no longer create objects from the bundle.
The data unloadallloadedobjects for False:assetbundle will be released without affecting the objects already created in the scene.
Unloadallloadedobjects is true: not only will the data in the Assetbundle be released, the texture material created from the bundle, and so on asset will be emptied. If the scene already uses these objects, the connection will be lost.
Read the scene: When Assetbundle downloaded, direct application.loadlevel ("scene");
- //******************************************************
- Assetbundle.createfrommemory-Loadlevel
- //******************************************************
- Using System;
- Using System.IO;
- Using Unityengine;
- public class Loadassetbundle:monobehaviour {
- Private Assetbundle Assetbundle;
- private Assetbundlecreaterequest request;
- void Update () {
- }
- void Ongui ()
- {
- if (GUI. button (new Rect (0,0,100,50), "Load"))
- {
- byte[] bs = file.readallbytes (application.datapath+ "/mylevel.unity3d");
- Request = Assetbundle.createfrommemory (BS);
- }
- if (GUI. button (new Rect (0,50,100,50), "Check"))
- {
- if (Request.isdone = = True)
- {
- Application.loadlevel ("Scene");
- }
- }
- }
- }
Summary:There are 1 main production AssetbundleBuildpipeline.buildassetbundleNon-Scene 2 buildpipeline.buildstreamedsceneassetbundle scenes using Assetbundle mainly have 1 assetbundle.createfromfile can only be uncompressed format 2 Assetbundle.createfrommemory need to deal with request3 assetbundle bundle = Www.assetBundle; www is divided into 2 kinds(1) www www = new www (address);
(2) www www = WWW.LoadFromCacheOrDownload (ADDRESS,1,CRC);One of the most popular online games is loadfromcacheordownload, because there is a local cache after the first download, and then it is read directly from the local cache. The CRC is used to verify the data.
recommended Loadfromcacheordownload. Createfrommemory is not recommended because it takes a long time to parse the construction of AB structure. CreateFromFile is also not recommended, because it only supports non-compressed formats, so it has more capacity.
When the preform is packaged into Assetbundle: The preform can be scripted, and the specified relationship can be used as usual.
Requirements:
(1) But the script has to be in the project.
(2) The script on the Assetbundle must be consistent with the script in the project.
Otherwise, an error is indicated.
(Turn) "Wind Yu Chong" Unity3d Tutorial assetbundles: the first talk