Unity Hand Tour < 11 > Resource Pack Assetbundle

Source: Internet
Author: User

Tribute to the original http://blog.csdn.net/janeky/article/details/17652021

In the operation of the hand tour, updating resources is not the least. Resource management The first step is resource packaging. Traditional packaging can be made into a preset prefab of all objects, packaged into a scene. Today we come together to study the official recommended Assetbundle, which is a resource packaging strategy provided by Unity (Pro). With Assetbundle, you can package almost all of your resources, making it easy for client updates to download new resources.

    • Create Assetbundle

1. Create an empty prefab, name the cube, then create a cube and pull it to the newly created prefab
2. Create a new script ExportAssetBundles.cs (code from the official document), saved in the Asset/editor directory

[CSharp]View Plaincopy
  1. Add a menu in the Unity Editor
  2. [MenuItem ("Assets/build assetbundle from Selection")]
  3. static void ExportResourceRGB2 ()
  4. {
  5. //Open the Save panel to get the path selected by the user
  6. string path = Editorutility.savefilepanel ("Save Resource", " ", "New Resource", "Assetbundle");
  7. if (path. Length! = 0)
  8. {
  9. //Select the object to save
  10. object[] selection = selection.getfiltered (typeof (Object), selectionmode.deepassets);
  11. //Packaging
  12. Buildpipeline.buildassetbundle (Selection.activeobject, Selection, Path, buildassetbundleoptions.collectdependencies | Buildassetbundleoptions.completeassets, buildtarget.standalonewindows);
  13. }
  14. }

At this point we will see the build Assetbundle from selection and build Scene below asset
3. Select the preset cube and run build Assetbundle from Selection. This will pop up a save box, name it Cube.unity3d (here for testing convenience, on the C drive. In the actual project, we need to put them on the Web server for all clients to download the update)
4. Create a new scene scene1.unity, place several models on top, then save

5. Select the scene, add a function to package the scene in the previous ExportAssetBundles.cs script, run Assets->build scene, Save as Scene1.unity3d (here for testing convenience, also on C-drive)

[CSharp]View Plaincopy
  1. [MenuItem ("Assets/save Scene")]
  2. static void Exportscene ()
  3. {
  4. //Open the Save panel to get the path selected by the user
  5. string path = Editorutility.savefilepanel ("Save Resource", " ", "New Resource", "Unity3d");
  6. if (path. Length! = 0)
  7. {
  8. //Select the object to save
  9. object[] selection = selection.getfiltered (typeof (Object), selectionmode.deepassets);
  10. string[] scenes = {"assets/scene1.unity"};
  11. //Packaging
  12. Buildpipeline.buildplayer (scenes,path,buildtarget.standalonewindows,buildoptions.buildadditionalstreamedscenes );
  13. }
  14. }

Precautions
The A.assetbundle's save suffix can be assetbundle or Unity3d
B.buildassetbundle to be packaged separately based on different platforms, the Buildtarget parameter specifies the platform, and if not specified, the default Webplayer

    • Load Assetbundle

We demonstrate how to load assetbundle with a simple code, including loading common asset and scenes.

[CSharp]View Plaincopy
  1. Using System;
  2. Using Unityengine;
  3. Using System.Collections;
  4. Public class Load:monobehaviour
  5. {
  6. private string bundleurl = "File:///C:/cube.assetbundle";
  7. private string sceneurl = "File:///C:/scene1.unity3d";
  8. void Start ()
  9. {
  10. //bundleurl = "file//" +application.datapath+ "/cube.assetbundle";
  11. Debug.Log (Bundleurl);
  12. Startcoroutine (Downloadassetandscene ());
  13. }
  14. IEnumerator Downloadassetandscene ()
  15. {
  16. //download assetbundle, load cube
  17. using (www asset = new www (bundleurl))
  18. {
  19. Yield return asset;
  20. Assetbundle bundle = Asset.assetbundle;
  21. Instantiate (bundle.  Load ("Cube"));
  22. Bundle.  Unload (false);
  23. Yield return new Waitforseconds (5);
  24. }
  25. //download scene, load scene
  26. using (www scene = new www (sceneurl))
  27. {
  28. Yield return scene;
  29. Assetbundle bundle = Scene.assetbundle;
  30. Application.loadlevel ("scene1");
  31. }
  32. }
  33. }

Precautions
A.loadfromcacheordownload can specify a version, and if the local version is new, it will not be read from the server
B. If multiple resources are packaged together, we will load specific resources through Bundle.load ()
C. Scripts that are mounted on the model can also be packaged together, but it is guaranteed that the script will exist in the original directory, otherwise it cannot be loaded. on how to update the script, I'll put it in a later chapter.

    • Assetbundle Dependency Relationship

If a public object is dependent on more than one object, there are two ways to pick it up when we package it. One of the more convenient is to package this common object into each object. There are a number of drawbacks: memory is wasted, and the addition of public objects has changed, and each dependent object has to be repackaged. Assetbundle provides packaging of dependencies. We learn through a simple example.

[CSharp]View Plaincopy
  1. Enable cross-references for all following resource bundle files until we call Popassetdependencies
  2. Buildpipeline.pushassetdependencies ();
  3. var options =
  4. buildassetbundleoptions.collectdependencies |
  5. Buildassetbundleoptions.completeassets;
  6. //All subsequent resources will share the contents of this resource bundle, and it is up to you to ensure that the shared resource bundle is loaded before other resources are loaded into the
  7. Buildpipeline.buildassetbundle (
  8. Assetdatabase.loadmainassetatpath ("Assets/artwork/lerpzuv.tif"),
  9. null, "Shared.unity3d", options);
  10. //This file will share these resources, but subsequent resource bundles will not be able to continue sharing it
  11. Buildpipeline.pushassetdependencies ();
  12. Buildpipeline.buildassetbundle (
  13. Assetdatabase.loadmainassetatpath ("ASSETS/ARTWORK/LERPZ.FBX"),
  14. null, "Lerpz.unity3d", options);
  15. Buildpipeline.popassetdependencies ();
  16. This file will share these resources, but subsequent resource bundles will not be able to continue sharing it
  17. Buildpipeline.pushassetdependencies ();
  18. Buildpipeline.buildassetbundle (
  19. Assetdatabase.loadmainassetatpath ("assets/artwork/explosive guitex.prefab"),
  20. null, "Explosive.unity3d", options);
  21. Buildpipeline.popassetdependencies ();
  22. Buildpipeline.popassetdependencies ();

We must ensure that the public objects are loaded first when the program loads. Otherwise, only after the successful loading of individual objects, and then manually added through the program, more cumbersome. In the actual project, because of the team development, the dependencies between objects are usually messy, preferably in the development cycle to set the relevant normative constraints, easy to manage.

    • Summarize

This section of the content of the actual operation, the official documents and the Rain Pine Blog has been more detailed introduction. If you still do not understand the place, you can combine the document and then practical operation. Later chapters, I'll spend more time on the core content: Incremental updates of resources, and updates to code programs.

    • Source

Http://pan.baidu.com/s/1i3BOAPn

    • Resources

1.http://www.xuanyusong.com/
2.http://game.ceeger.com/manual/downloadingassetbundles.html

Unity Hand Tour < 11 > Resource Pack Assetbundle

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.