AssetBundle 1 (Building And Loading)

1. Build

using UnityEngine;
using System.Collections;
using UnityEditor;

public class BuildBundle {

    private const string VARIANT = "ab";
    [MenuItem("Test/Build Asset Bundles")]
    static void BuildABs() {
	    // Create the array of bundle build details.

	    AssetBundleBuild[] buildMap = new AssetBundleBuild[2];

        // prfab
	    buildMap[0].assetBundleName = "111";
	    string[] assetNames = new string[1];
        assetNames[0] = "Assets/res/Prefabs/char/307005/307005.prefab";
        buildMap[0].assetNames = assetNames;

        //texture
        buildMap[1].assetBundleName = "222";
        assetNames = new string[1];
        assetNames[0] = "Assets/res/Models/char/307005/Materials/307005_001.tga";
        buildMap[1].assetNames = assetNames;

        string outputPath = Application.dataPath + "/ABs";

        BuildAssetBundleOptions op  = BuildAssetBundleOptions.ChunkBasedCompression; // LZ4
        //BuildAssetBundleOptions op = BuildAssetBundleOptions.UncompressedAssetBundle;
        BuildPipeline.BuildAssetBundles(outputPath, buildMap, op);
        AssetDatabase.Refresh();
    }
}


2.

Load

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class LoadAB : MonoBehaviour {

	// Use this for initialization
	void Start () {

        string ABDir = Application.dataPath + "/ABs/";

        string mainManifestPath = ABDir + "ABs";
        var rootBundle = AssetBundle.LoadFromFile(mainManifestPath);
        AssetBundleManifest manifest = rootBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
        
        string prefabBundleName = "111";
        //dependBundle
        List<AssetBundle> dependBundleList = new List<AssetBundle>();
        string[] dependPath = manifest.GetAllDependencies(prefabBundleName);
        for (int i = 0; i < dependPath.Length; i++)
        {
            Debug.Log(dependPath[i]);
            var dependBundle = AssetBundle.LoadFromFile(ABDir + dependPath[i]);
            dependBundleList.Add(dependBundle);
        }

        var myLoadedAssetBundle = AssetBundle.LoadFromFile(ABDir + prefabBundleName);
        if (myLoadedAssetBundle == null)
        {
            Debug.Log("Failed to load AssetBundle!");
            return;
        }

        string prefabAssetPath = "Assets/res/Prefabs/char/307005/307005.prefab";
        var prefab = myLoadedAssetBundle.LoadAsset<Object>(prefabAssetPath);
        Instantiate(prefab);

        // unload
        for (int i = 0; i < dependBundleList.Count; i++)
        {
            dependBundleList[i].Unload(false);
        }
        myLoadedAssetBundle.Unload(false);
	}
}


Node:


Each AssetBundle will have an associated file with a .manifest extension. This manifest file is a text file that you can open with any text editor. It provides information such as the file CRC and asset dependencies. The AssetBundle in the example above has a manifest file that looks like this:


ManifestFileVersion: 0
CRC: 4036177110
Hashes:
  AssetFileHash:
    serializedVersion: 2
    Hash: 2adb3efdc80c7f64bf065717f6e98867
  TypeTreeHash:
    serializedVersion: 2
    Hash: 8e187a830a7454c6df7fc214f8624bac
HashAppended: 0
ClassTypes:
- Class: 1
  Script: {instanceID: 0}
- Class: 4
  Script: {instanceID: 0}
- Class: 21
  Script: {instanceID: 0}
- Class: 28
  Script: {instanceID: 0}
- Class: 43
  Script: {instanceID: 0}
- Class: 48
  Script: {instanceID: 0}
- Class: 74
  Script: {instanceID: 0}
- Class: 90
  Script: {instanceID: 0}
- Class: 91
  Script: {instanceID: 0}
- Class: 95
  Script: {instanceID: 0}
- Class: 136
  Script: {instanceID: 0}
- Class: 137
  Script: {instanceID: 0}
Assets:
- Assets/res/Prefabs/char/307005/307005.prefab
Dependencies:
- D:/U3D Pro/2017_1_4_Study_AssetBundle/Assets/ABs/222




In addition to these, there are another two files created: Another AssetBundle and another manifest file. These two are always created whenever AssetBundles are created. They are created for each folder that AssetBundles are created in, thus if you always create AssetBundles in the same place, you will only get two extra files. The additional manifest file - in this example AssetBundles.manifest - can be used in much the same way as other manifest files but will show information on how AssetBundles relate and depend on each other. In this case, since we only have a single AssetBundle, it has no other dependencies.


ManifestFileVersion: 0
CRC: 2131931493
AssetBundleManifest:
  AssetBundleInfos:
    Info_0:
      Name: 111
      Dependencies:
        Dependency_0: 222
    Info_1:
      Name: 222
      Dependencies: {}


Compress:


BuildAssetBundleOptions.ChunkBasedCompression

-- Use chunk-based LZ4 compression when creating the AssetBundle.



LZ4 Format

Unity also supports LZ4 compression, which results in larger compressed file sizes, but does not require the entire bundle to be decompressed before use. LZ4 is a “chunk-based” algorithm, and therefore when objects are loaded from an LZ4-compressed bundle, only the corresponding chunks for that object are decompressed. This occurs on-the-fly, meaning there are no wait times for the entire bundle to be decompressed before use. The LZ4 Format was introduced in Unity 5.3 and was unavailable in prior versions.






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值