Exemplo n.º 1
0
    public static void Pack()
    {
        // 清理输出目录
        CreateOrClearOutPath();

        // 清理之前设置过的bundleName
        ClearAssetBundleName();

        // 设置bunderName
        bundleMap.Clear();
        List <string> resList = new List <string>();

        for (int i = 0; i < RES_DIRS.Length; i++)
        {
            resList.AddRange(GetAllResDirs(RES_DIRS[i]));
        }
        foreach (string dir in resList)
        {
            setAssetBundleName(dir);
        }

        // 打包
        BuildPipeline.BuildAssetBundles(RES_OUTPUT_PATH, BuildAssetBundleOptions.DeterministicAssetBundle, BuildTarget.StandaloneWindows64);
        AssetDatabase.Refresh();

        // 构建依赖关系
        AssetBundle         assetBundle = AssetBundle.LoadFromFile(FileUtils.getPath("StreamingAssets"));
        AssetBundleManifest mainfest    = assetBundle.LoadAsset <AssetBundleManifest> ("AssetBundleManifest");

        string[] bundleNames = mainfest.GetAllAssetBundles();
        foreach (string bundleName in bundleNames)
        {
            string[] deps = mainfest.GetAllDependencies(bundleName);
            foreach (string dep in deps)
            {
                List <AssetBuildBundleInfo> infoList = null;
                bundleMap.TryGetValue(bundleName, out infoList);
                if (null != infoList)
                {
                    foreach (AssetBuildBundleInfo info in infoList)
                    {
                        info.AddDependence(dep);
                    }
                }
            }
        }

        assetBundle.Unload(true);
        assetBundle = null;

        // 生成XML
        doc = new XMLDocment();
        doc.startObject("files");
        foreach (KeyValuePair <string, AssetBuildBundleInfo> pair in fileMap)
        {
            AssetBuildBundleInfo info = pair.Value;

            doc.startObject("file");
            doc.createElement("bundleName", info.bundleName);
            doc.createElement("fileName", info.fileName);
            doc.createElement("assetName", info.assetName);

            if (null != info.dependencies)
            {
                doc.startObject("deps");
                foreach (string dep in info.dependencies)
                {
                    doc.createElement("dep", dep);
                }
                doc.endObject("deps");
            }
            doc.endObject("file");
        }
        doc.endObject("files");

        FileStream fs = new FileStream(Path.Combine(RES_OUTPUT_PATH, "StreamingAssets.xml"), FileMode.Create);

        byte[] data = System.Text.Encoding.UTF8.GetBytes(doc.ToString());
        fs.Write(data, 0, data.Length);
        fs.Flush();
        fs.Close();


        // 打包后的清理
        ClearOutPath();
    }
Exemplo n.º 2
0
    public static void Pack()
    {
        // 清理输出目录
        CreateOrClearOutPath();

        // 清理之前设置过的bundleName
        ClearAssetBundleName();

        // 设置bunderName
        bundleMap.Clear();
        fileMap.Clear();
        List <string> resList = GetAllResDirs(RES_SRC_PATH);

        foreach (string dir in resList)
        {
            setAssetBundleName(dir);
        }

        // 打包
        BuildPipeline.BuildAssetBundles(RES_OUTPUT_PATH, BuildAssetBundleOptions.DeterministicAssetBundle, BuildTarget.StandaloneWindows64);
        AssetDatabase.Refresh();

        // 构建依赖关系
        if (!System.IO.File.Exists(FileUtils.getPath("StreamingAssets")))
        {
            return;
        }

        AssetBundle         assetBundle = AssetBundle.LoadFromFile(FileUtils.getPath("StreamingAssets"));
        AssetBundleManifest mainfest    = assetBundle.LoadAsset <AssetBundleManifest> ("AssetBundleManifest");

        string[] bundleNames = mainfest.GetAllAssetBundles();
        foreach (string bundleName in bundleNames)
        {
            string[] deps = mainfest.GetAllDependencies(bundleName);
            foreach (string dep in deps)
            {
                List <AssetBuildBundleInfo> infoList = null;
                bundleMap.TryGetValue(bundleName, out infoList);
                if (null != infoList)
                {
                    foreach (AssetBuildBundleInfo info in infoList)
                    {
                        info.AddDependence(dep);
                    }
                }
            }
        }

        // 生成XML
        doc = new XMLDocment();
        doc.startObject("files");
        foreach (KeyValuePair <string, AssetBuildBundleInfo> pair in fileMap)
        {
            AssetBuildBundleInfo info = pair.Value;

            doc.startObject("file");
            doc.createElement("bundleName", info.bundleName);
            doc.createElement("fileName", info.fileName);
            doc.createElement("assetName", info.assetName);

            if (null != info.dependencies)
            {
                doc.startObject("deps");
                foreach (string dep in info.dependencies)
                {
                    doc.createElement("dep", dep);
                }
                doc.endObject("deps");
            }
            doc.endObject("file");
        }
        doc.endObject("files");

        FileStream fs = new FileStream(Path.Combine(RES_OUTPUT_PATH, "StreamingAssets.xml"), FileMode.Create);

        byte[] data = System.Text.Encoding.UTF8.GetBytes(doc.ToString());
        fs.Write(data, 0, data.Length);
        fs.Flush();
        fs.Close();

        if (ResourceManager.GetInstance().ManagerType == ResManagerType.ABB)
        {
            //创建压缩文件的MD5
            CreateMD5File(".unity3d");

            // 打包后的清理
            ClearOutPath(".xxx");
        }
        else if (ResourceManager.GetInstance().ManagerType == ResManagerType.ABB_UPDATE)
        {
            //压缩文件
            CompressFile();

            //创建压缩文件的MD5
            CreateMD5File(".zip");

            //复制到指定目录
            CopyFileForUpdate();

            // 打包后的清理
            ClearOutPath(".unity3d");
        }
    }