Unity5.0 AssetBundle 实验

来源:互联网 发布:网络IP盗取 编辑:程序博客网 时间:2024/05/17 05:05

在unity5中,选中资源一键打包,创建一个简单的脚本,一句代码就行:

 BuildPipeline.BuildAssetBundles(Application.dataPath + "/AssetBundle", BuildAssetBundleOptions.UncompressedAssetBundle);


资源打包好后,在我们的AssetBundle文件夹中,有好多manifest文件,可以供我们来查看是否有依赖关系,哈希ID,和CRC信息


接下来,我们需要写一个脚本来加载,首先加载有依赖的项,然后再加载被依赖的项,保证这个顺序正确,代码如下:

 void OnGUI()
    {
        if (GUILayout.Button("LoadAssetbundle"))
        {
            //Find the path of your mainfest file
            AssetBundle manifestBundle = AssetBundle.CreateFromFile(Application.dataPath +
                "/AssetBundle/AssetBundle");

            if (manifestBundle != null)
            {
                //Load it at first
                AssetBundleManifest manifest = (AssetBundleManifest)manifestBundle.LoadAsset
                    ("AssetBundleManifest");
                //Choose one you want to add
                string[] cubedepends = manifest.GetAllDependencies("cubezero");
                AssetBundle[] dependsAssetbundle = new AssetBundle[cubedepends.Length];
                //Add all childs
                for (int index = 0; index < cubedepends.Length; index++)
                {
                    dependsAssetbundle[index] = AssetBundle.CreateFromFile(Application.dataPath
                        + "/Assetbundle/" + cubedepends[index]);
                }  
            }
            //Instantiate the prefab
            AssetBundle cubeBundle = AssetBundle.CreateFromFile(Application.dataPath + "/Assetbundle/cubezero");       
            GameObject cube = cubeBundle.LoadAsset("Cube0") as GameObject;   
            if (cube != null)
            {
                Instantiate(cube);
            }
        }
    }


这样就能把打包好的东西从场景中加载出来了,还是很有意思的,哈哈。

很棒的参考文章 :http://gad.qq.com/college/articledetail/43

0 0
原创粉丝点击