"Wind Yu Chong" Unity3d Tutorial assetbundles: the first talk

Source: Internet
Author: User

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
  1. Using Unityengine;
  2. Using Unityeditor;
  3. public class Exportassetbundles {
  4. [MenuItem ("Assets/build assetbundle from Selection-track dependencies")]
  5. static void Exportresource () {
  6. Bring up Save panel
  7. String path = Editorutility.savefilepanel ("Save Resource", "", "New Resource", "Unity3d");
  8. if (path. Length! = 0) {
  9. Build the resource file from the active selection.
  10. object[] selection = selection.getfiltered (typeof (Object), selectionmode.deepassets);
  11. Buildpipeline.buildassetbundle (Selection.activeobject, Selection,
  12. Path, Buildassetbundleoptions.collectdependencies |buildassetbundleoptions.completeassets
  13. , buildtarget.standalonewindows);
  14. Selection.objects = Selection;
  15. }
  16. }
  17. [MenuItem ("Assets/build Assetbundle from Selection-no Dependency tracking")]
  18. static void Exportresourcenotrack () {
  19. Bring up Save panel
  20. String path = Editorutility.savefilepanel ("Save Resource", "", "New Resource", "Unity3d");
  21. if (path. Length! = 0) {
  22. Build the resource file from the active selection.
  23. Buildpipeline.buildassetbundle (Selection.activeobject, selection.objects, Path);
  24. }
  25. }
  26. }
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

  1. [MenuItem ("Assets/build assetbundle from Selection names-track dependencies")]
  2. static void Exportresourcewithnames () {
  3. Bring up Save panel
  4. String path = Editorutility.savefilepanel ("Save Resource", "", "New Resource", "Unity3d");
  5. if (path. Length! = 0) {
  6. Build the resource file from the active selection.
  7. object[] selection = selection.getfiltered (typeof (Object), selectionmode.deepassets);
  8. string[] names = new string[selection. Length];
  9. for (int i =0;i
  10. {
  11. Names[i] = i+ "_" + selection[i].name;
  12. }
  13. Buildpipeline.buildassetbundleexplicitassetnames (Selection,names,
  14. Path, Buildassetbundleoptions.collectdependencies |buildassetbundleoptions.completeassets
  15. , buildtarget.standalonewindows);
  16. Selection.objects = Selection;
  17. }
  18. }

    1. Using System;
    2. Using System.IO;
    3. Using Unityengine;
    4. public class Loadassetbundle:monobehaviour {
    5. Private Assetbundle Assetbundle;
    6. private Assetbundlecreaterequest request;
    7. void Update () {
    8. }
    9. void Ongui ()
    10. {
    11. if (GUI. button (new Rect (0,0,100,50), "Load"))
    12. {
    13. byte[] bs = file.readallbytes (application.datapath+ "/withnames.unity3d");
    14. Request = Assetbundle.createfrommemory (BS);
    15. }
    16. if (GUI. button (new Rect (0,50,100,50), "Check"))
    17. {
    18. if (Request.isdone = = True)
    19. {
    20. Assetbundle = Request.assetbundle;
    21. Unityengine.object obj = assetbundle.load ("0_cube");
    22. Instantiate (obj);
    23. }
    24. }
    25. }

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.
    1. Using Unityengine;
    2. Using Unityeditor;
    3. public class Buildscene:monobehaviour {
    4. [MenuItem ("build/buildwebplayerstreamed")]
    5. static void Buildscenes ()
    6. {
    7. string [] levels = new STRING[1];
    8. Levels[0] = "assets/scene.unity";
    9. Buildpipeline.buildstreamedsceneassetbundle (Levels, "Assets/mylevel.unity3d", Buildtarget.standaloneosxintel);
    10. }
    11. }


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);

  1. Using System;
  2. Using System.IO;
  3. Using Unityengine;
  4. public class Loadassetbundle:monobehaviour {
  5. Private Assetbundle Assetbundle;
  6. private Assetbundlecreaterequest request;
  7. void Update () {
  8. if (request!=null)
  9. Print (request.progress);
  10. }
  11. void Ongui ()
  12. {
  13. if (GUI. button (new Rect (0,0,100,50), "Load"))
  14. {
  15. byte[] bs = file.readallbytes ("D:/mybundle.unity3d");
  16. Request = Assetbundle.createfrommemory (BS);
  17. }
  18. if (GUI. button (new Rect (0,50,100,50), "Check"))
  19. {
  20. if (Request.isdone = = True)
  21. {
  22. Print ("Name:" +request.assetbundle.mainasset.name);
  23. Instantiate (Request.assetBundle.mainAsset);
  24. }
  25. }
  26. }
  27. }

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");
  1. Using System;
  2. Using System.IO;
  3. Using Unityengine;
  4. Using System.Collections;
  5. public class Loadassetbundle:monobehaviour {
  6. Private Assetbundle Assetbundle;
  7. private string address = "Http://127.0.0.1/AssetBundles/myBundle.unity3d";
  8. void Ongui ()
  9. {
  10. if (GUI. button (new Rect (0,0,100,50), "Load Web"))
  11. {
  12. Startcoroutine (Load ());
  13. }
  14. }
  15. IEnumerator Load () {
  16. Download the file from the URL. It is not being saved in the Cache
  17. String Assetname= "";
  18. www www = new www (address);
  19. yield return www;
  20. if (www.error! = null)
  21. throw new Exception ("WWW download had an error:" + www.error);
  22. Assetbundle bundle = Www.assetBundle;
  23. if (Assetname = = "")
  24. Instantiate (Bundle.mainasset);
  25. Else
  26. Instantiate (bundle. Load (Assetname));
  27. Unload the Assetbundles compressed contents to conserve memory
  28. Bundle. Unload (FALSE);
  29. }
  30. }
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);
  1. Using System;
  2. Using System.IO;
  3. Using Unityengine;
  4. Using System.Collections;
  5. public class Loadassetbundle:monobehaviour {
  6. Private Assetbundle Assetbundle;
  7. private string address = "Http://127.0.0.1/AssetBundles/myBundle.unity3d";
  8. void Ongui ()
  9. {
  10. if (GUI. button (new Rect (0,0,100,50), "Load Web"))
  11. {
  12. Startcoroutine (Load ());
  13. }
  14. }
  15. IEnumerator Load () {
  16. String Assetname= "";
  17. www www = WWW.LoadFromCacheOrDownload (address,1);
  18. yield return www;
  19. if (www.error! = null)
  20. throw new Exception ("WWW download had an error:" + www.error);
  21. Assetbundle bundle = Www.assetBundle;
  22. if (Assetname = = "")
  23. Instantiate (Bundle.mainasset);
  24. Else
  25. Instantiate (bundle. Load (Assetname));
  26. Unload the Assetbundles compressed contents to conserve memory
  27. Bundle. Unload (FALSE);
  28. }
  29. }

(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");

  1. //******************************************************
  2. Assetbundle.createfrommemory-Loadlevel
  3. //******************************************************
  4. Using System;
  5. Using System.IO;
  6. Using Unityengine;
  7. public class Loadassetbundle:monobehaviour {
  8. Private Assetbundle Assetbundle;
  9. private Assetbundlecreaterequest request;
  10. void Update () {
  11. }
  12. void Ongui ()
  13. {
  14. if (GUI. button (new Rect (0,0,100,50), "Load"))
  15. {
  16. byte[] bs = file.readallbytes (application.datapath+ "/mylevel.unity3d");
  17. Request = Assetbundle.createfrommemory (BS);
  18. }
  19. if (GUI. button (new Rect (0,50,100,50), "Check"))
  20. {
  21. if (Request.isdone = = True)
  22. {
  23. Application.loadlevel ("Scene");
  24. }
  25. }
  26. }
  27. }
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.