示例#1
0
    public static bool BuildAssetBundle(string path, BuildTarget target, List <GameConfig> games, ref List <AssetBundleInfo> abList)
    {
        try
        {
            string gamesDir = path + "games/";
            if (!Directory.Exists(gamesDir))
            {
                Directory.CreateDirectory(gamesDir);
            }

            if (EditorUserBuildSettings.activeBuildTarget != target)
            {
                EditorUserBuildSettings.SwitchActiveBuildTarget(target);
            }

            foreach (var game in games)
            {
                if (!game.Packed)
                {
                    continue;
                }

                var abPath = Array.Find(game.AssetBundles, s => s.Contains(GlobalConst.Res.SceneFileExt));
                if (string.IsNullOrEmpty(abPath))
                {
                    continue;
                }

                string name            = Path.GetFileNameWithoutExtension(abPath);
                string destDirFileName = path + abPath;
                string destDirPath     = Path.GetDirectoryName(destDirFileName);
                if (!Directory.Exists(destDirPath))
                {
                    Directory.CreateDirectory(destDirPath);
                }

                /*if (abList.Exists(item => string.Compare(item.Name, name, true) == 0))
                 * {
                 *  Debug.Log("the scene was already build. FileName:" + name);
                 *  continue;
                 * }*/

                Debug.Log("build scene from :" + game.SceneName + " dest:" + abPath);

                string res = BuildPipeline.BuildPlayer(
                    new string[] { game.SceneName },
                    destDirFileName,
                    target,
                    BuildOptions.BuildAdditionalStreamedScenes);

                if (res.Length > 0)
                {
                    Debug.LogError(res);
                    return(false);
                }

                name = abPath.Replace(Path.GetExtension(abPath), "");

                var data_type = new AssetBundleInfo();
                //data_type.ID = "";
                data_type.Name     = name;
                data_type.FileName = abPath;
                data_type.Hash     = MD5Util.GetFileMD5(destDirFileName);
                data_type.Version  = GameVersion.GetProductVersion(game.Version).ToString();

                var oldItem = abList.Find(item => { return(string.Compare(item.Name, name, true) == 0); });
                if (oldItem != null)
                {
                    abList.Remove(oldItem);
                }

                Debug.Log("add builded scene:" + data_type + " name:" + name);
                abList.Add(data_type);
            }

            var manifest = BuildAssetBundle(path, target, games);
            if (manifest == null)
            {
                Debug.Log("null manifest");
                return(true);
            }

            string[] assetBundles = manifest.GetAllAssetBundles();
            foreach (var ab in assetBundles)
            {
                string name = ab.Replace(Path.GetExtension(ab), "");

                var data_type = new AssetBundleInfo();
                //data_type.ID = "";
                data_type.Name     = name;
                data_type.FileName = ab;
                data_type.Hash     = MD5Util.GetFileMD5(path + ab);
                data_type.Version  = "0";

                var oldItem = abList.Find(item => { return(string.Compare(item.Name, name, true) == 0); });
                if (oldItem != null)
                {
                    abList.Remove(oldItem);

                    if (string.Compare(oldItem.Hash, data_type.Hash, true) != 0)
                    {
                        data_type.UpdateVersion();
                    }
                }

                Debug.Log("add builded ab:" + data_type + " name:" + name);
                abList.Add(data_type);
            }

            Debug.Log("assetbundle build finish");
            return(true);
        }
        catch (Exception e)
        {
            Debug.LogException(e);
            return(false);
        }
    }