C# Unity3D中的文件压缩
我正在尝试压缩一个Unity3D项目中的文件。我正在使用以下代码压缩我的文件:C# Unity3D中的文件压缩,c#,unity3d,zip,C#,Unity3d,Zip,我正在尝试压缩一个Unity3D项目中的文件。我正在使用以下代码压缩我的文件: private void CompressFolder(string path, ZipOutputStream zipStream, int folderOffset) { string[] files = Directory.GetFiles(path); foreach (string filename in files) { FileInfo fi = new FileI
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);
}
}
此函数实际上将文件夹的目录作为输入,并逐个压缩它们。我这里的问题是,当压缩发生时,主项目是冻结的。我怎样才能克服这个问题 最简单的方法是使用忍者线程、多线程协程:
使用UnityEngine;
使用系统集合;
使用长矛//包括忍者线程
公共类MyAsyncCompression:单行为{
公共空间(PIT){
//准备好通信数据-在异步协同程序中,您将无法使用任何Unity函数
this.startcroutineasync(MyBackgroundCoroutine());/“this”表示单行为
}
IEnumerator MyBackgroundCoroutine(){
//在此处调用CompressFolder()
收益返回空;
}
私有void CompressFolder(字符串路径、ZipOutStream zipStream、int folderOffset){
/*...
*...
...*/
}
}
ninja线程库允许您在另一个线程中运行任何协同程序,它不会暂停主线程,您也不会遇到FPS打嗝。如何在提供的链接中将其实现到我的项目中Unity中有一个打开的选项(提示资产存储),它会打开Unity资产存储,但我卡在那里了(可能正在尝试加载库)。