美文网首页
unity5的AssetBundle批量打包的小技巧

unity5的AssetBundle批量打包的小技巧

作者: jojo911 | 来源:发表于2018-10-09 16:29 被阅读0次

游戏因为要做微端的功能,包要尽可能的小,所以资源需要拆的很细,以场景为例,场景上存在大量物件,所有物件都是Prefab,加载场景时只有一个基础场景,所有物件都是动态加载。

unity5编辑器上的UI可以设置assetBundleName,不过个人觉得很麻烦。

替代方案是可以把所有的资源都放到一个list,然后调用BuildPipeline.BuildAssetBundles,如果一个贴图有在list里,

那么所有用到该贴图的Prefab就不会包含该贴图,

从而不会造成AssetBundle资源变大。达到最小包的目的。

代码如下:

public static void BuildAllPrefab()
{
    List textureList = new List();
    List materialList = new List();
    List shaderList = new List();
    List prefabPathList = new List(); //所有Prefab的路径列表,这里就不写相关代码了

    for (int i = 0; i < prefabPathList.Count; ++i)
    {
        string path = prefabPathList[i];
        string fileName = Path.GetFileNameWithoutExtension(path);
        if (string.IsNullOrEmpty(fileName)) continue;
        string[] tmpdependencies = AssetDatabase.GetDependencies(new string[] { path });
        foreach (string tmpdependencie in tmpdependencies)
        {
            string tmp = tmpdependencie.ToLower();
            string mame = Path.GetFileNameWithoutExtension(tmp).ToLower();
            if (tmp.Contains(".tga") || tmp.Contains(".png") || tmp.Contains(".jpg"))
            {
                if (!textureList.Contains(tmp))
                {
                    textureList.Add(tmp);
                }
            }
            else if (tmp.Contains(".shader"))
            {
                if (!shaderList.Contains(tmp))
                {
                    shaderList.Add(tmp);
                }
            }

            else if (tmp.Contains(".mat"))
            {
                if (!materialList.Contains(tmp))
                {
                    materialList.Add(tmp);
                }
            }
        }
    }

    List buildInfoList = new List();
    string Extension = "";
    BuildTarget target = BuildTarget.StandaloneWindows;

    foreach (string path in textureList)
    {
        buildInfoList.Add(GetDependencieAssetBundleInfo(path, ".x" + Extension));
    }

    foreach (string path in shaderList)
    {
        buildInfoList.Add(GetDependencieAssetBundleInfo(path, ".x" + Extension));
    }

    foreach (string path in materialList)
    {
        buildInfoList.Add(GetDependencieAssetBundleInfo(path, ".x" + Extension));
    }

    foreach (string path in prefabPathList)
    {
        buildInfoList.Add(GetDependencieAssetBundleInfo(path, ".x" + Extension));
    }

    if (buildInfoList.Count > 0)
    {
        ToolCommon.CreatePath("assetbundles/assets/scenes/");
        if (BuildPipeline.BuildAssetBundles("assetbundles/assets/scenes/", buildInfoList.ToArray(), BuildAssetBundleOptions.None, target))
        {
            //            byte[] bytes = AssetsEncrypts.ReadFileToByte(dest);
            //            AssetsEncrypts.EncryptBytes(bytes);
            //            AssetsEncrypts.WriteByteToFile(bytes, dest);
        }
        else
        {
            Debug.LogError("Build Scene Dynamic Error!");
        }
    }
}

public static AssetBundleBuild GetDependencieAssetBundleInfo(string path, string Extension)
{
    string dependenciePath = "";
    string fileName = Path.GetFileNameWithoutExtension(path);

    if (path.Contains(".tga") || path.Contains(".png") || path.Contains(".jpg"))
    {
        dependenciePath = "texture/" + fileName + Extension;
    }
    else if (path.Contains(".shader"))
    {
        dependenciePath = "shader/" + fileName + Extension;
    }
    else if (path.Contains(".mat"))
    {
        dependenciePath = "fbx/materials/" + fileName + Extension;
    }
    else if (path.Contains(".fbx"))
    {
        dependenciePath = "fbx/" + fileName + Extension;
    }
    else if (path.Contains(".prefab"))
    {
        dependenciePath = "prefab/" + fileName + Extension;
    }
    else
    {
        dependenciePath = ToolCommon.GetWindowPath(path, Extension);
        dependenciePath = dependenciePath.Replace("assetbundles/assets/scene/", "");
    }

    AssetBundleBuild buildInfo = new AssetBundleBuild();
    buildInfo.assetBundleName = dependenciePath;
    buildInfo.assetNames = new string[] { path };
    return buildInfo;
}

相关文章

网友评论

      本文标题:unity5的AssetBundle批量打包的小技巧

      本文链接:https://www.haomeiwen.com/subject/sjcyaftx.html