碧血红天的HomePage

U3d杂谈 — 打AssetBundle资源

在u3d,可以把我们需要用到的资源打入到AssetBundle中使用。这就涉及到AssetBundle包对资源的分类和划分。一般我们想把某一些资源打入到一个包,另一些资源打入到另一个包。我们可以使用BuildPipeline.BuildAssetBundles来根据自己的需求生成AssetBundle包。

平常,我们可以在资源的属性中直接设置AssetBundle的名字,然后使用此函数自动生成。但是我们需要手动改每一个资源的设置,显示的相当麻烦。

BuildPipeline.BuildAssetBundles(assetBundleDirectory, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);

我们可以自己添加要打入的资源,使用

public static AssetBundleManifest BuildAssetBundles(string outputPath, AssetBundleBuild[] builds, BuildAssetBundleOptions assetBundleOptions, BuildTarget targetPlatform);

所以,我们需要创建AssetBundleBuild的打包对象。每一个 AssetBundleBuild就是一个AssetBundle包,里面添加对个Asset资源名字。

AssetBundleBuild build = new AssetBundleBuild();
build.assetBundleName = "common";
build.assetNames = string[2]{"Assets/aaa.png","Assets/bbb.prefab"}
buildList.Add(build);

这里简单制作了一个资源打包管理器。可以通过选中添加AssetBundle资源包,然后添加哪些资源加入其中。

里面生成AssetBundle包的逻辑:

static public void GenAssetBundle(string rootPath)
    {
        if (Directory.Exists(rootPath) == false)
        {
            Directory.CreateDirectory(rootPath);
        }
        GenResourceMap(rootPath);
        List<AssetBundleBuild> buildList = new List<AssetBundleBuild>();
        for (int i = 0; i < _items.Count; ++i)
        {
            AssetBundleBuild build = new AssetBundleBuild();
            build.assetBundleName = _items[i].GetName();
            build.assetNames = _items[i].GetAssetNames();
            buildList.Add(build);
        }
        BuildPipeline.BuildAssetBundles(rootPath, buildList.ToArray(), BuildAssetBundleOptions.None, EditorUserBuildSettings.activeBuildTarget);
    }

4401 评论

  1. The other day, while I was at work, my sister stole my iphone and tested to see if it can survive a twenty five foot drop, just so she can be a youtube sensation. My apple ipad is now broken and she has 83 views. I know this is entirely off topic but I had to share it with someone!