//创建资源索引
    public static void DumpResourceFile()
    {
        ////创建所有资源Asset列表
        XmlDocument doc  = new XmlDocument();
        XmlElement  root = doc.CreateElement("AllResources");

        XmlElement resource = doc.CreateElement("Resources");

        root.AppendChild(resource);
        foreach (string res in mResources)
        {
            string ex   = BuildCommon.getFileSuffix(res);
            string path = res.Replace("Assets/", "");
            path = path.Replace("." + ex, "");

            XmlElement ele = doc.CreateElement("file");
            ele.SetAttribute("name", path);
            ele.SetAttribute("type", ex);
            resource.AppendChild(ele);
        }

        //创建所有需要打包的Level列表
        XmlElement sceneRes = doc.CreateElement("Level");

        root.AppendChild(sceneRes);
        foreach (string scene in mScenes)
        {
            XmlElement ele = doc.CreateElement("file");

            string path = scene.Replace("Assets/", "");
            path = path.Replace(".unity", "");

            ele.SetAttribute("name", path);
            ele.SetAttribute("type", "unity");
            sceneRes.AppendChild(ele);
        }
        doc.AppendChild(root);
        BuildCommon.CheckFolder(BuildCommon.getPath(ResourceCommon.assetbundleFilePath));
        doc.Save(ResourceCommon.assetbundleFilePath + "Resource.bytes");

        Debug.Log("CreateResourceCfg success!!!");
    }
    public static void BuildResource()
    {
        AssetDatabase.Refresh();

        //执行依赖性打包

        //资源最大等级
        int maxLevel = allLevelAssets.Count;

        if (maxLevel == 0)
        {
            return;
        }

        //从最低等级开始打包
        for (int level = 1; level <= maxLevel; level++)
        {
            BuildPipeline.PushAssetDependencies();

            //获取不同等级的aaset
            Dictionary <string, AssetUnit> levelAssets = allLevelAssets[level];

            //遍历该等级的所有asset打包
            foreach (KeyValuePair <string, AssetUnit> pair in levelAssets)
            {
                //根据路径获取asset资源
                Object asset = AssetDatabase.LoadMainAssetAtPath(pair.Value.mPath);
                if (null == asset)
                {
                    DebugEx.LogError("load " + pair.Value.mPath + " failed!!!", "BuildResource", true);
                }

                //生成打包保存路径
                string savePath = pair.Value.mPath.Insert(6, assetFilePath) + ResourceCommon.assetbundleFileSuffix;
                BuildCommon.CheckFolder(BuildCommon.getPath(savePath));

                //打包名称去Asset



                //普通资源
                if (pair.Value.mSuffix != "unity")//普通资源
                {
                    string assetName = pair.Value.mPath.Replace("Assets/", "");

                    //资源打包
                    if (!BuildPipeline.BuildAssetBundleExplicitAssetNames(
                            new Object[] { asset }, new string[] { assetName }, savePath, options, buildPlatform))
                    {
                        DebugEx.LogError("build assetbundle " + savePath + " failed!!!", "BuildResource", true);
                    }
                }
                //场景资源,没有依赖场景的
                else
                {
                    AssetDatabase.Refresh();
                    BuildPipeline.PushAssetDependencies();
                    string error = BuildPipeline.BuildStreamedSceneAssetBundle(new string[] { pair.Value.mPath }, savePath, buildPlatform);
                    if (error != "")
                    {
                        DebugEx.LogError(error, "BuildResource", true);
                    }
                    BuildPipeline.PopAssetDependencies();

                    Debug.Log("scene path" + pair.Value.mPath);
                    //pair.Value.mPath
                }
            }
        }

        //popdepency依赖
        for (int level = 1; level <= maxLevel; level++)
        {
            BuildPipeline.PopAssetDependencies();
        }
    }
    //创建Asset资源信息
    public static void DumpAssetInfoFile()
    {
        if (allLevelAssets.Count == 0)
        {
            return;
        }

        ////创建所有资源Asset列表
        XmlDocument doc  = new XmlDocument();
        XmlElement  root = doc.CreateElement("AllAssets");

        //遍历所有Asset数据
        for (int level = 1; level <= allLevelAssets.Count; level++)
        {
            Dictionary <string, AssetUnit> levelAssets = allLevelAssets[level];
            foreach (KeyValuePair <string, AssetUnit> asset in levelAssets)
            {
                string    assetName = asset.Key;
                AssetUnit assetUnit = asset.Value;

                XmlElement ele = doc.CreateElement("Asset");

                //设置路径名称
                assetName = assetName.Replace("Assets/", "");
                ele.SetAttribute("name", assetName);

                //设置asset索引
                ele.SetAttribute("index", assetUnit.mIndex.ToString());

                //设置等级
                ele.SetAttribute("level", level.ToString());

                List <AssetUnit> sortDepencys = new List <AssetUnit>();
                //获取AssetUnit所有依赖,并排序
                List <string> depencys = assetUnit.mAllDependencies;
                foreach (string depency in depencys)
                {
                    AssetUnit depencyUnit = GetAssetUnit(depency);
                    sortDepencys.Add(depencyUnit);
                }

                //排序
                sortDepencys.Sort(SortAssetUnit);
                //保存依赖索引
                string depencystr = "";
                for (int i = 0; i < sortDepencys.Count; i++)
                {
                    AssetUnit unit = sortDepencys[i];

                    if (i != sortDepencys.Count - 1)
                    {
                        depencystr += unit.mIndex + ",";
                    }
                    else
                    {
                        depencystr += unit.mIndex;
                    }
                }
                ele.SetAttribute("depency", depencystr.ToString());

                root.AppendChild(ele);
            }
        }

        doc.AppendChild(root);
        BuildCommon.CheckFolder(BuildCommon.getPath(ResourceCommon.assetbundleFilePath));
        doc.Save(ResourceCommon.assetbundleFilePath + "AssetInfo.bytes");

        Debug.Log("CreateAssetInfo success!!!");
    }
Exemplo n.º 4
0
    /// <summary>
    /// 打包资源为AssetBundle
    /// </summary>
    private static void BuildResource()
    {
        AssetDatabase.Refresh();
        int maxLevel = allLevelAssets.Count;

        if (maxLevel == 0)
        {
            return;
        }
        for (int level = 1; level <= maxLevel; level++)
        {
            BuildPipeline.PushAssetDependencies();
            Dictionary <string, AssetUnit> levelAssets = allLevelAssets[level];
            foreach (var pair in levelAssets)
            {
                UnityEngine.Object asset    = AssetDatabase.LoadMainAssetAtPath(pair.Value.mPath);
                string             savePath = "";
                pair.Value.mName = BuildCommon.GetLevelABPathName(pair.Value.mPath);
                if (null == asset)
                {
                    Debug.LogError("Load :" + pair.Value.mPath + "failed");
                }
                Debug.Log(pair.Value.mSuffix);
                if (pair.Value.mSuffix.Equals("shader"))
                {
                    savePath         = "Assets/bin/data/shader/" + BuildCommon.GetLevelABPath(pair.Value.mPath);
                    pair.Value.mPath = "data/shader/" + BuildCommon.GetLevelABPath(pair.Value.mPath);
                }
                else if (pair.Value.mSuffix.Equals("mat"))
                {
                    savePath         = "Assets/bin/data/atlas/" + BuildCommon.GetLevelABPath(pair.Value.mPath);
                    pair.Value.mPath = "data/atlas/" + BuildCommon.GetLevelABPath(pair.Value.mPath);
                }
                else if (pair.Value.mSuffix.Equals("prefab"))
                {
                    if (pair.Value.mName.Contains("atla"))
                    {
                        savePath         = "Assets/bin/data/atlas/" + BuildCommon.GetLevelABPath(pair.Value.mPath);
                        pair.Value.mPath = "data/atlas/" + BuildCommon.GetLevelABPath(pair.Value.mPath);
                    }
                    else
                    {
                        savePath         = "Assets/bin/data/ui/" + BuildCommon.GetLevelABPath(pair.Value.mPath);
                        pair.Value.mPath = "data/ui/" + BuildCommon.GetLevelABPath(pair.Value.mPath);
                    }
                }
                else if (pair.Value.mSuffix.Equals("ttf"))
                {
                    savePath         = "Assets/bin/data/font/" + BuildCommon.GetLevelABPath(pair.Value.mPath);
                    pair.Value.mPath = "data/font/" + BuildCommon.GetLevelABPath(pair.Value.mPath);
                }
                else if (pair.Value.mSuffix.Equals("png") || pair.Value.mSuffix.Equals("psd") || pair.Value.mSuffix.Equals("dds"))
                {
                    savePath         = "Assets/bin/data/texture/" + BuildCommon.GetLevelABPath(pair.Value.mPath);
                    pair.Value.mPath = "data/texture/" + BuildCommon.GetLevelABPath(pair.Value.mPath);
                }
                else if (pair.Value.mSuffix.Equals("mp4"))
                {
                    savePath         = "Assets/bin/data/audio/" + BuildCommon.GetLevelABPath(pair.Value.mPath);
                    pair.Value.mPath = "data/audio/" + BuildCommon.GetLevelABPath(pair.Value.mPath);
                }
                else if (pair.Value.mSuffix.Equals("unity"))
                {
                    //pair.Value.mPath = "data/scene/" + BuildCommon.GetLevelABPath(pair.Value.mPath);
                    savePath = "Assets/bin/data/scene/" + BuildCommon.GetLevelABPath(pair.Value.mPath);
                }
                BuildCommon.CheckFolder(BuildCommon.getPath(savePath));
                //普通资源
                if (pair.Value.mSuffix != "unity")
                {
                    string assetName = pair.Value.mPath.Replace("Assets/", "");
                    uint   ver;
                    if (!BuildPipeline.BuildAssetBundle(asset, null, savePath, out ver, options, buildPlatform))
                    {
                        Debug.LogError("Build Assetbundle:" + savePath + " Failed");
                    }
                }
                //场景资源
                else
                {
                    AssetDatabase.Refresh();
                    BuildPipeline.PushAssetDependencies();
                    string error = BuildPipeline.BuildStreamedSceneAssetBundle(new string[] { pair.Value.mPath }, savePath, buildPlatform);
                    if (!string.IsNullOrEmpty(error))
                    {
                        Debug.LogError(error);
                    }
                    BuildPipeline.PopAssetDependencies();
                }
            }
        }
        for (int level = 1; level <= maxLevel; level++)
        {
            BuildPipeline.PopAssetDependencies();
        }
    }