Unity5 Assetbundle: unity5assetbundle: unity5assetbundle
Because ULUA is used in the project, and the Unity5 Assetbundle packaging method is used in the ULUA Demo, we still learn the Assetbundle packaging method of version 5.0.
To put it simply, the AB package added in Unity5 is the same as what we usually use. The principle is to create a dependency file for each file and then summarize it into a total dependency file, when the game starts running, it needs to load the total dependency file, and then obtain the dependency from it to load the dependencies when loading Assetbundle.
The Unity5 package Assetbundle API is easy to use.
1. Set an Assetbundle name for each resource to be packaged.
2. Use Code packaging. The following is the packaging code used in the example:
using UnityEngine;using UnityEditor;using System.Collections;public class BuildAssetbundle : Editor{[MenuItem("BuildAssetbundle/BuildAll")]static void Build(){BuildPipeline.BuildAssetBundles(Application.dataPath+"/../Assetbundle",BuildAssetBundleOptions.UncompressedAssetBundle);}}
After packaging, you can get a bunch of Assetbundle files and the same number of Manifest files.
It's very simple and convenient, right.
However, I found a pitfall. The two Material files in the project, Mat1 and Mat2, were packed so big ......
Thinking about Unity as a bunch of bugs, I downloaded the official Demo. This problem was not found in the Demo, but compared with the Material findings.
The image is dragged to Assets by default, and the generated Material and Shader are selected as Stander by default.
The official Unity Demo selects a specific Shader.
I guess after Stander is selected, all Shader is packed in the Assetbundle package.
So pay attention to this.
Then let's look at the loaded code:
Using UnityEngine; using System. collections; public class LoadAssetbundle: MonoBehaviour {void OnGUI () {if (GUILayout. button ("LoadAssetbundle") {// load the Manifest file first; AssetBundle manifestBundle = AssetBundle. createFromFile (Application. dataPath + "/.. /Assetbundle "); if (manifestBundle! = Null) {AssetBundleManifest manifest = (AssetBundleManifest) manifestBundle. loadAsset ("AssetBundleManifest"); // obtain the list of dependent files; string [] cubedepends = manifest. getAllDependencies ("myresources_cube"); AssetBundle [] dependsAssetbundle = new AssetBundle [cubedepends. length]; for (int index = 0; index <cubedepends. length; index ++) {// load all dependent files; dependsAssetbundle [index] = AssetBundle. createFromFile (Application. dataPath + "/.. /Assetb Undle/"+ cubedepends [index]);} // load the file we need; AssetBundle cubeBundle = AssetBundle. createFromFile (Application. dataPath + "/.. /Assetbundle/myresources_cube "); GameObject cube = cubeBundle. loadAsset ("Cube0") as GameObject; if (cube! = Null) {Instantiate (cube );}}}}}
It is easy to use. A picture has a truth.
Of course, it is too troublesome to manually set the Assetbundle name for each resource, so we should use the script to automatically generate the name.
/************************** File name: AutoSetTextureUISprite. cs; * file Description: automatically sets Assetbundle name as folder name_file name. unity3d; * creation date: 2015/05/04; * Author: Chen Peng; * *************************/using UnityEngine; using System. collections; using UnityEditor; public class AutoSetTextureUISprite: AssetPostprocessor {static void OnPostprocessAllAssets (string [] importedAssets, string [] deletedAssets, string [] movedAssets, string [ ] MovedFromAssetPaths) {foreach (var str in importedAssets) {// Debug. Log ("Reimported Asset:" + str); if (! Str. endsWith (". cs ") {AssetImporter importer = AssetImporter. getAtPath (str); importer. assetBundleName = str ;}} foreach (var str in deletedAssets) {// Debug. log ("Deleted Asset:" + str); if (! Str. endsWith (". cs ") {AssetImporter importer = AssetImporter. getAtPath (str); importer. assetBundleName = str ;}}for (var I = 0; I <movedAssets. length; I ++) {// Debug. log ("Moved Asset:" + movedAssets [I] + "from:" + movedFromAssetPaths [I]) ;}}
When the file is dragged into the project or Reimport, AssetbundleName is automatically set as the file path name, and then packaged into the corresponding directory.
Download project:
http://download.csdn.net/detail/cp790621656/8801445