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.
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
- Add a menu in the Unity Editor
- [MenuItem ("Assets/build assetbundle from Selection")]
- static void ExportResourceRGB2 ()
- {
- //Open the Save panel to get the path selected by the user
- string path = Editorutility.savefilepanel ("Save Resource", " ", "New Resource", "Assetbundle");
- if (path. Length! = 0)
- {
- //Select the object to save
- object[] selection = selection.getfiltered (typeof (Object), selectionmode.deepassets);
- //Packaging
- Buildpipeline.buildassetbundle (Selection.activeobject, Selection, Path, buildassetbundleoptions.collectdependencies | Buildassetbundleoptions.completeassets, buildtarget.standalonewindows);
- }
- }
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
- [MenuItem ("Assets/save Scene")]
- static void Exportscene ()
- {
- //Open the Save panel to get the path selected by the user
- string path = Editorutility.savefilepanel ("Save Resource", " ", "New Resource", "Unity3d");
- if (path. Length! = 0)
- {
- //Select the object to save
- object[] selection = selection.getfiltered (typeof (Object), selectionmode.deepassets);
- string[] scenes = {"assets/scene1.unity"};
- //Packaging
- Buildpipeline.buildplayer (scenes,path,buildtarget.standalonewindows,buildoptions.buildadditionalstreamedscenes );
- }
- }
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
We demonstrate how to load assetbundle with a simple code, including loading common asset and scenes.
[CSharp]View Plaincopy
- Using System;
- Using Unityengine;
- Using System.Collections;
- Public class Load:monobehaviour
- {
- private string bundleurl = "File:///C:/cube.assetbundle";
- private string sceneurl = "File:///C:/scene1.unity3d";
- void Start ()
- {
- //bundleurl = "file//" +application.datapath+ "/cube.assetbundle";
- Debug.Log (Bundleurl);
- Startcoroutine (Downloadassetandscene ());
- }
- IEnumerator Downloadassetandscene ()
- {
- //download assetbundle, load cube
- using (www asset = new www (bundleurl))
- {
- Yield return asset;
- Assetbundle bundle = Asset.assetbundle;
- Instantiate (bundle. Load ("Cube"));
- Bundle. Unload (false);
- Yield return new Waitforseconds (5);
- }
- //download scene, load scene
- using (www scene = new www (sceneurl))
- {
- Yield return scene;
- Assetbundle bundle = Scene.assetbundle;
- Application.loadlevel ("scene1");
- }
- }
- }
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
- Enable cross-references for all following resource bundle files until we call Popassetdependencies
- Buildpipeline.pushassetdependencies ();
- var options =
- buildassetbundleoptions.collectdependencies |
- Buildassetbundleoptions.completeassets;
- //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
- Buildpipeline.buildassetbundle (
- Assetdatabase.loadmainassetatpath ("Assets/artwork/lerpzuv.tif"),
- null, "Shared.unity3d", options);
- //This file will share these resources, but subsequent resource bundles will not be able to continue sharing it
- Buildpipeline.pushassetdependencies ();
- Buildpipeline.buildassetbundle (
- Assetdatabase.loadmainassetatpath ("ASSETS/ARTWORK/LERPZ.FBX"),
- null, "Lerpz.unity3d", options);
- Buildpipeline.popassetdependencies ();
- This file will share these resources, but subsequent resource bundles will not be able to continue sharing it
- Buildpipeline.pushassetdependencies ();
- Buildpipeline.buildassetbundle (
- Assetdatabase.loadmainassetatpath ("assets/artwork/explosive guitex.prefab"),
- null, "Explosive.unity3d", options);
- Buildpipeline.popassetdependencies ();
- 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.
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.
Http://pan.baidu.com/s/1i3BOAPn
1.http://www.xuanyusong.com/
2.http://game.ceeger.com/manual/downloadingassetbundles.html
Unity Hand Tour < 11 > Resource Pack Assetbundle