Unity5.0 AssetBundle 实验

在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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瓜皮肖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值