コード例 #1
0
        static void CopyFiles(string plantform, ResourceDatas resourceList)
        {
            string targetPath = exportPath + "/ExportResources/" + plantform + "/";

            if (!Directory.Exists(targetPath))
            {
                Directory.CreateDirectory(targetPath);
            }

            if (File.Exists(targetPath + "export_names.txt"))
            {
                File.Copy(targetPath + "export_names.txt", targetPath + "_export_names.txt", true);
            }

            string        r1 = targetPath + "_ResourceList_1.ab";
            string        r  = targetPath + "_ResourceList.ab";
            ResourceDatas l1 = BuildHelper.LoadResourceDatas(r1);

            if (l1 == null)
            {
                l1 = new ResourceDatas();
            }
            ResourceDatas l = BuildHelper.LoadResourceDatas(r);

            if (l == null)
            {
                l = new ResourceDatas();
            }
            var e = resourceList.Resources.GetEnumerator();

            while (e.MoveNext())
            {
                var key = e.Current.Key;
                var rd  = e.Current.Value;
                File.Copy(projectPath + rd.Path, targetPath + key + ".ab", true);
                l1.Resources[key] = rd;
                l.Resources[key]  = rd;
            }

            BuildHelper.SaveResourceDatas(r1, l1);
            BuildHelper.SaveResourceDatas(r, l);

            SevenZipHelper.CompressFile(r, targetPath + "ResourceList.ab");

            StreamWriter writer = new StreamWriter(targetPath + "export_names.txt", false, Encoding.Default);

            writer.WriteLine("string,uint,int,int,string,string");
            writer.WriteLine("key,crc,size,type,path,depends");
            foreach (var c in l1.Resources)
            {
                string[] dependsArray = new string[c.Value.Depends.Count];
                for (int i = 0; i < dependsArray.Length; ++i)
                {
                    dependsArray[i] = c.Value.Depends[i];
                }
                string depends = c.Value.Depends.Count == 0 ? "" : string.Join("|", dependsArray);
                writer.WriteLine(string.Format("{0},{1},{2},{3},{4},{5}", c.Key, c.Value.Crc, c.Value.Size, (int)c.Value.Type, c.Value.Path, depends));
            }

            ResourceDatas resources_2 = BuildHelper.LoadResourceDatas(targetPath + "_ResourceList_2.ab");

            if (resources_2 != null)
            {
                foreach (var c in resources_2.Resources)
                {
                    string[] dependsArray = new string[c.Value.Depends.Count];
                    for (int i = 0; i < dependsArray.Length; ++i)
                    {
                        dependsArray[i] = c.Value.Depends[i];
                    }
                    string depends = c.Value.Depends.Count == 0 ? "" : string.Join("|", dependsArray);
                    writer.WriteLine(string.Format("{0},{1},{2},{3},{4},{5}", c.Key, c.Value.Crc, c.Value.Size, (int)c.Value.Type, c.Value.Path, depends));
                }
            }
            writer.Close();

            if (File.Exists(targetPath + "version.txt"))
            {
                string[] strs     = File.ReadAllLines(targetPath + "version.txt");
                string[] vers     = strs[0].Split(' ');
                string   versions = vers[0];
                versions += " ";
                versions += FileHelper.GetFileCrc(targetPath + "_ResourceList.ab");
                versions += " ";
                versions += vers[2];
                byte[] buf = System.Text.Encoding.Default.GetBytes(versions);
                File.WriteAllBytes(targetPath + "version.txt", buf);
            }
        }
コード例 #2
0
    static void Export(BuildTarget target)
    {
#if !RECOURCE_CLIENT
        CopyDll(target);
#endif
        UnityEngine.Debug.Log("Start Export Resources " + target + " " + DateTime.Now);
        //UpdateProgressBar("正在导出 " + GetBuildTargetName(target) + " 平台资源");
        string manifest;
        if (target == BuildTarget.Android)
        {
            exportDir = Application.dataPath + "/../../Builds/ExportResources/Android/";
            manifest  = exportDir + "Android";
        }
        else if (target == BuildTarget.iOS)
        {
            exportDir = Application.dataPath + "/../../Builds/ExportResources/IOS/";
            manifest  = exportDir + "IOS";
        }
        else
        {
            exportDir = Application.dataPath + "/../../Builds/ExportResources/Windows/";
            manifest  = exportDir + "Windows";
        }

        if (!Directory.Exists(exportDir))
        {
            Directory.CreateDirectory(exportDir);
        }

        if (File.Exists(exportDir + "export_names.txt"))
        {
            File.Copy(exportDir + "export_names.txt", exportDir + "_export_names.txt", true);
        }

        string saveManifest      = manifest + "_2";
        string resourceList1Name = "_ResourceList_1.ab";
        string resourceList2Name = "_ResourceList_2.ab";
        string resourceListName  = resourceList2Name;
#if !RECOURCE_CLIENT
        saveManifest     = manifest + "_1";
        resourceListName = resourceList1Name;
#endif

        if (File.Exists(saveManifest))
        {
            File.Copy(saveManifest, manifest, true);
        }
        if (File.Exists(saveManifest + ".manifest"))
        {
            File.Copy(saveManifest + ".manifest", manifest + ".manifest", true);
        }

        ResourceDatas lastCompressFiles = BuildHelper.LoadResourceDatas(exportDir + resourceListName);
        List <string> lastExports       = new List <string>();

        if (lastCompressFiles != null && lastCompressFiles.Resources.Count > 0)
        {
            var e = lastCompressFiles.Resources.GetEnumerator();
            while (e.MoveNext())
            {
                lastExports.Add(e.Current.Key);
            }
        }

//         if (target == BuildTarget.Android)
//         {
//             for (int idx = 0; idx < sSoundPoolMusic.Count; ++idx)
//             {
//                 int num = sSoundPoolMusic[idx];
//                 string oldName = "Assets/Resources/Sound/" + num + ".mp3";
//                 string newName = "Assets/Resources/UnPackage/Sound/" + num + ".mp3";
//                 string oldId = GetPathID(oldName);
//                 sExportFiles.Remove(oldId);
//                 AssetDatabase.MoveAsset(oldName, newName);
//                 AssetDatabase.Refresh();
//                 string newId = GetPathID(newName);
//                 sExportFiles.Add(newId, newName);
//             }
//         }

        foreach (KeyValuePair <string, string> pair in sExportFiles)
        {
            string fullPath = Application.dataPath.Substring(0, Application.dataPath.Length - 6) + pair.Value;
            if (lastExports.Contains(pair.Key))
            {
                lastExports.Remove(pair.Key);
            }

            if (pair.Value.StartsWith("Assets/Resources/") && pair.Value.Contains("Unpackage"))
            {
                string targetDir = exportDir + pair.Key + ".ab";
                FileHelper.CopyFile(pair.Value, targetDir);
                SetAssetBundleName(fullPath, "", "");
            }
        }
        foreach (string str in lastExports)
        {
            if (File.Exists(exportDir + str + ".ab"))
            {
                File.Delete(exportDir + str + ".ab");
            }

            if (File.Exists(exportDir + str + ".ab.manifest"))
            {
                File.Delete(exportDir + str + ".ab.manifest");
            }
        }

        AssetDatabase.Refresh();

        BuildPipeline.BuildAssetBundles(exportDir, BuildAssetBundleOptions.ChunkBasedCompression, target);
        //AssetDatabase.Refresh();
        //UpdateProgressBar();

        ResourceDatas resources = new ResourceDatas();

        foreach (KeyValuePair <string, string> pair in sExportFiles)
        {
            if (!File.Exists(exportDir + pair.Key + ".ab"))
            {
                FileHelper.CopyFile(pair.Value, exportDir + pair.Key + ".ab");
                UnityEngine.Debug.LogError("Export AssetBundle " + pair.Value + " fail, use copy instead!");
            }

            ResourceData rd = GetResourceData(pair.Key, pair.Value);
            resources.Resources.Add(pair.Key, rd);

            //UpdateProgressBar();
        }
        BuildHelper.SaveResourceDatas(exportDir + resourceListName, resources);

        ResourceDatas resources_1 = BuildHelper.LoadResourceDatas(exportDir + resourceList1Name);
        ResourceDatas resources_2 = BuildHelper.LoadResourceDatas(exportDir + resourceList2Name);
#if !RECOURCE_CLIENT
        if (resources_2 != null)
        {
            foreach (var c in resources_2.Resources)
            {
#else
        if (resources_1 != null)
        {
            foreach (var c in resources_1.Resources)
            {
#endif
                if (!resources.Resources.ContainsKey(c.Key))
                {
                    resources.Resources.Add(c.Key, c.Value);
                }
                else
                {
                    UnityEngine.Debug.LogError("ResourceClient与GameClient中有相同路径的资源 : " + c.Value.Path);
                }
            }
        }

        BuildHelper.SaveResourceDatas(exportDir + "_ResourceList.ab", resources);
        SevenZipHelper.CompressFile(exportDir + "_ResourceList.ab", exportDir + "ResourceList.ab");
        //#endif


        StreamWriter writer = new StreamWriter(exportDir + "export_names.txt", false, Encoding.Default);
        writer.WriteLine("string,uint,int,int,string,string");
        writer.WriteLine("key,crc,size,type,reference,path,depends");
        if (resources_1 != null)
        {
            foreach (var c in resources_1.Resources)
            {
                string[] dependsArray = new string[c.Value.Depends.Count];
                for (int i = 0; i < dependsArray.Length; ++i)
                {
                    dependsArray[i] = c.Value.Depends[i];
                }
                string depends = c.Value.Depends.Count == 0 ? "" : string.Join("|", dependsArray);
                writer.WriteLine(string.Format("{0},{1},{2},{3},{4},{5},{6}", c.Key, c.Value.Crc, c.Value.Size, (int)c.Value.Type, c.Value.Reference, c.Value.Path, depends));
            }
        }
        if (resources_2 != null)
        {
            foreach (var c in resources_2.Resources)
            {
                string[] dependsArray = new string[c.Value.Depends.Count];
                for (int i = 0; i < dependsArray.Length; ++i)
                {
                    dependsArray[i] = c.Value.Depends[i];
                }
                string depends = c.Value.Depends.Count == 0 ? "" : string.Join("|", dependsArray);
                writer.WriteLine(string.Format("{0},{1},{2},{3},{4},{5},{6}", c.Key, c.Value.Crc, c.Value.Size, (int)c.Value.Type, c.Value.Reference, c.Value.Path, depends));
            }
        }
        writer.Close();

#if !RECOURCE_CLIENT
        string versions = ResourceManager.CodeVersion.ToString();
        if (File.Exists(exportDir + "version.txt"))
        {
            string[] strs = File.ReadAllLines(exportDir + "version.txt");
            string[] vers = strs[0].Split(' ');
            versions = vers[0];
        }
        versions += " ";
        versions += FileHelper.GetFileCrc(exportDir + "_ResourceList.ab");
        versions += " ";
        string version = "";
        BuildHelper.GetCSharpVersionCode(ref version);
        version  += ".";
        version  += BuildHelper.GetRourceVersion();
        versions += version;
        byte[] buf = System.Text.Encoding.Default.GetBytes(versions);
        File.WriteAllBytes(exportDir + "version.txt", buf);
#else
        if (File.Exists(exportDir + "version.txt"))
        {
            string[] strs     = File.ReadAllLines(exportDir + "version.txt");
            string[] vers     = strs[0].Split(' ');
            string   versions = vers[0]; versions += " ";
            versions += FileHelper.GetFileCrc(exportDir + "_ResourceList.ab");
            byte[] buf = System.Text.Encoding.Default.GetBytes(versions);
            File.WriteAllBytes(exportDir + "version.txt", buf);
        }
#endif

        //         if (target == BuildTarget.Android)
        //         {
        //             for (int idx = 0; idx < sSoundPoolMusic.Count; ++idx)
        //             {
        //                 int num = sSoundPoolMusic[idx];
        //                 string oldName = "Assets/Resources/UnPackage/Sound/" + num + ".mp3";
        //                 string newName = "Assets/Resources/Sound/" + num + ".mp3";
        //                 string oldId = GetPathID(oldName);
        //                 sExportFiles.Remove(oldId);
        //                 AssetDatabase.MoveAsset(oldName, newName);
        //                 AssetDatabase.Refresh();
        //                 string newId = GetPathID(newName);
        //                 sExportFiles.Add(newId, newName);
        //
        //                 string fullPath = Application.dataPath.Substring(0, Application.dataPath.Length - 6) + newName;
        //                 string[] abs = newId.Split('.');
        //                 SetAssetBundleName(fullPath, abs[0], abs[1]);
        //                 AssetDatabase.Refresh();
        //             }
        //         }
        //         UpdateProgressBar();

        File.Copy(manifest, saveManifest, true);
        File.Copy(manifest + ".manifest", saveManifest + ".manifest", true);

        UnityEngine.Debug.Log("Finish Export Resources " + target + " " + DateTime.Now);
    }