资源打包Assetbundle

来源:互联网 发布:美图还原软件 编辑:程序博客网 时间:2024/06/05 07:02

在开发的过程中,我们需要加载外部资源,将先将资源打包为*.assetbundle文件,然后程序中可以之间使用。

  • 打包Assetbundle
    /// <summary>    /// 将选中的预制分别打包    /// </summary>    [MenuItem("DevTools/Build AssetBundles")]    static void CreateAssetBundleThemelves()    {    // 获取要打包的对象(在Project视图中)        Object[] selects = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);        // 遍历选中的对象        foreach (Object obj in selects)        {            // 文件的后缀名是assetbundle和unity都可以            string targetPath = Application.dataPath + "/StreamingAssets/" + obj.name + ".assetbundle";            BuildPipeline.BuildAssetBundle(obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies);        }        // 刷新编辑器(不写的话要手动刷新,否则打包的资源不能及时在Project视图内显示)        AssetDatabase.Refresh();    }

DevTools/Build AssetBundles会出现在Unity3d的菜单中,如下:

这里写图片描述

  • 加载AssetBundle
    WWW www = new WWW("file://" + url);    // 上面导出的是什么类型,这里对应的就是什么类型。    GameObject prefab = (GameObject)www.assetBundle.mainAsset;    www.assetBundle.Unload(false);    // 创建预制体    GameObject obj = (GameObject)Instantiate(prefab, positon, rotation);

写的简单,有不清楚的可以随时交流、学习。

0 0