unity5的AssetBundle批量打包的小技巧

游戏因为要做微端的功能,包要尽可能的小,所以资源需要拆的很细,以场景为例,场景上存在大量物件,所有物件都是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;
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 最近网友通过网站搜索Unity3D在手机及其他平台下占用内存太大. 这里写下关于unity3d对于内存的管理与优化...
    杨树叶的杨阅读 1,214评论 0赞 0
  • 首先附上原文链接:https://unity3d.com/learn/tutorials/topics/best-...
    Bonging阅读 1,320评论 0赞 0
  • 项目第一次对外技术测试落下帷幕,终于有时间来填大世界动态加载这样一个大坑。 从去年11月份开始,在需求改变、制作方...
    压抑的内心阅读 2,659评论 0赞 1
  • 今晚周二参加我们淄博的NLP执行师聚会。听到秀美姐姐的分享,我对保密原则又有了重新的定义和认识,我也坚信我以后也要...
    孙云峰_4e13阅读 554评论 0赞 0
  • 维生素A又称抗干眼病维生素。天热的维生素A又包括维生素A1(视黄醇)和维生素A2(3-脱氢视黄醇)两种。 ...
    离家的小红帽阅读 678评论 0赞 2

友情链接更多精彩内容