Unity3D中的文件压缩

File compression in Unity3D

我正在尝试从我拥有的 Unity3D 项目中压缩文件。我正在使用以下代码来压缩我的文件:

private void CompressFolder(string path, ZipOutputStream zipStream, int folderOffset) {

    string[] files = Directory.GetFiles(path);

    foreach (string filename in files) {

        FileInfo fi = new FileInfo(filename);

        string entryName = filename.Substring(folderOffset); // Makes the name in zip based on the folder
        entryName = ZipEntry.CleanName(entryName); // Removes drive from name and fixes slash direction
        ZipEntry newEntry = new ZipEntry(entryName);
        newEntry.DateTime = fi.LastWriteTime; // Note the zip format stores 2 second granularity

        newEntry.Size = fi.Length;

        zipStream.PutNextEntry(newEntry);

        // Zip the file in buffered chunks
        // the "using" will close the stream even if an exception occurs
        byte[ ] buffer = new byte[4096];
        using (FileStream streamReader = File.OpenRead(filename)) {
            StreamUtils.Copy(streamReader, zipStream, buffer);
        }
        zipStream.CloseEntry();
    }
    string[ ] folders = Directory.GetDirectories(path);
    foreach (string folder in folders) {
        CompressFolder(folder, zipStream, folderOffset);
    }
}

这个函数实际上将带有文件夹的目录作为输入,并将它们一个一个地压缩。我的问题是,当压缩发生时,主要项目冻结了。我怎样才能克服这个问题?

最简单的方法是使用忍者线程,多线程协程:https://www.assetstore.unity3d.com/en/#!/content/15717

using UnityEngine;
using System.Collections;
using CielaSpike; //include ninja threads

public class MyAsyncCompression : MonoBehaviour {


    public void ZipIt(){
        //ready data for commpressing - you will not be able to use any Unity functions while inside the asynchronous coroutine
        this.StartCoroutineAsync(MyBackgroundCoroutine()); // "this" means MonoBehaviour

    }

    IEnumerator MyBackgroundCoroutine(){
        //Call CompressFolder() here
        yield return null;
    }

    private void CompressFolder(string path, ZipOutputStream zipStream, int folderOffset) {
        /*...
         *... 
          ...*/
    }

}