예제 #1
0
    static void BuildScene(string outPath, BuildTarget target)
    {
        List <string> resList   = GetAllResDirs(AppConst.MoonScenePath);
        List <string> buildList = new List <string>();

        if (resList.Count > 0 && resList != null)
        {
            for (int i = 0; i < resList.Count; i++)
            {
                Debug.Log("场景的文件目录===" + resList[i]);
                string[] files = Directory.GetFiles(resList[i]);

                if (files == null || files.Length == 0)
                {
                    continue;
                }
                foreach (string str in files)
                {
                    if (str.EndsWith(".unity"))
                    {
                        Debug.Log("要进打包的场景名字= " + str);
                        buildList.Add(str);
                        string assetName  = str.ToLower();
                        string bundleName = str.Replace('/', '@').Replace("Assets@Res@", "").Replace(".unity", ".unity3d").ToLower();
                        string scenePath  = Path.Combine(outPath, bundleName).ToLower();
                        Debug.Log("要进打包的场景的打包名字= " + scenePath);

                        //一个场景一个包
                        BuildPipeline.BuildPlayer(buildList.ToArray(), scenePath, target, BuildOptions.BuildAdditionalStreamedScenes);
                        AssetBundleMoonInfo info = new AssetBundleMoonInfo();
                        info.assetName = new List <string>();
                        info.deps      = new List <string>();

                        info.bundleName = bundleName;
                        info.assetName.Add(assetName);

                        ResDic.Add(info.bundleName, info);

                        buildList.Clear();
                    }
                }
            }
        }
    }
예제 #2
0
    static void SetAssetBundleName(string fullPath)
    {
        string buildScenePath            = "Assets/Res/Scene";
        Dictionary <string, bool> dirMap = new Dictionary <string, bool>();

        // 遍历所有文件设置bundleName

        string[] files = Directory.GetFiles(fullPath);
        if (files == null || files.Length == 0)
        {
            return;
        }
        // 处理dirBundleName
        string dirBundleName = fullPath.Substring(AppConst.ResPath.Length);

        //fullPath.Substring (AppConst.ResourcesPath.Length);
        if (dirBundleName == "")
        {
            dirBundleName = "res";
        }
        Debug.Log("dirBundleName: " + dirBundleName);
#if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
        dirBundleName  = dirBundleName.Replace("\\", "/");
        buildScenePath = buildScenePath.Replace("\\", "/");
#endif
        dirBundleName = dirBundleName.Replace("/", "@") + AppConst.ExtName;

        foreach (string oneFile in files)
        {
#if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
            buildScenePath = buildScenePath.Replace("\\", "/");
#endif
            //Debug.Log("EndsWith ==  " + Path.GetExtension(oneFile));
            if (oneFile.EndsWith(".meta") ||
                oneFile.EndsWith(".DS_Store") ||
                oneFile.EndsWith(".unity") ||
                oneFile.EndsWith(".lua"))
            {
                continue;
            }
            else if (oneFile.StartsWith(buildScenePath) || oneFile.StartsWith(AppConst.LuaTempDir))
            {
                continue;
            } /*else if (filePath.StartsWith (audioPath)) {
               * continue;
               * }*/

            // 设置bundleName
            AssetImporter importer = AssetImporter.GetAtPath(oneFile);
            // 构建AssetBundle信息 用于xml
            AssetBundleMoonInfo info = new AssetBundleMoonInfo();
            info.assetName = new List <string>();
            info.deps      = new List <string>();

            if (importer != null)
            {
                string ext        = Path.GetExtension(oneFile);
                string bundleName = dirBundleName;
                if (null != ext && ext.Equals(".prefab"))
                {
                    // prefab单个文件打包
                    bundleName = oneFile.Substring(AppConst.ResPath.Length);
                    bundleName = bundleName.Replace("/", "@");
                    if (null != ext)
                    {
                        bundleName = bundleName.Replace(ext, AppConst.ExtName);
                    }
                    else
                    {
                        bundleName += AppConst.ExtName;
                    }
                }
                //这个不知道是什么类型的
                if (null != ext && ext.Equals(".dds"))
                {
                    UnityEngine.Debug.Log("Error image format!!! " + oneFile);
                    continue;
                }
                else
                {
                    bool spritepack = false;
                    if (importer is TextureImporter)
                    {
                        TextureImporter textureImporter = importer as TextureImporter;
                        if (!string.IsNullOrEmpty(textureImporter.spritePackingTag))
                        {
                            // 图集打包
                            bundleName = "spritepack@" + textureImporter.spritePackingTag + AppConst.ExtName;
                            spritepack = true;
                        }
                    }

                    if (!spritepack)
                    {
                        string dir  = Path.GetDirectoryName(oneFile);
                        bool   pack = false;
                        if (dirMap.ContainsKey(dir))
                        {
                            pack = dirMap[dir];
                        }
                        else
                        {
                            pack = !File.Exists(Path.Combine(dir, "split.txt"));
                            dirMap.Add(dir, pack);
                        }

                        if (!pack)
                        {
                            // 当个文件打包
                            bundleName = oneFile.Substring(AppConst.ResPath.Length);
                            bundleName = bundleName.Replace("/", "@");
                            if (null != ext)
                            {
                                bundleName = bundleName.Replace(ext, AppConst.ExtName);
                            }
                            else
                            {
                                bundleName += AppConst.ExtName;
                            }
                        }
                    }
                }
                bundleName = bundleName.ToLower();
                //重新把ab包的名字设置了下
                importer.assetBundleName = bundleName;
                //Debug.Log("assetBundleName :" + bundleName);

                // 存储bundleInfo
                if (ResDic.ContainsKey(bundleName))
                {
                    ResDic[bundleName].assetName.Add(oneFile.ToLower());
                }
                else
                {
                    info.assetName.Add(oneFile.ToLower());
                    info.bundleName = bundleName.ToLower();
                    ResDic.Add(info.bundleName, info);
                }
                Debug.Log("info == " + info.assetName + "   bundleName==" + info.bundleName);
            }
            else
            {
                UnityEngine.Debug.LogWarningFormat("Set AssetName Fail, File:{0}, Msg:Importer is null", oneFile);
            }
        }
    }