【Unity】zipを非同期にProgressBarを表示しながら解凍する
2020-04-20
ランタイム上でzipの解凍がしたかった…
StackOverFlowにあった方法を参考にUnityで使えたのでそれのメモです.
Env
Windows10 64bit
Unity2019.1.12f1
Method
stack over flowのこのページをおもいっきり参考にした
https://stackoverflow.com/questions/43661211/extract-an-archive-with-progress-bar
上記のURLの先にあるZipProgress
クラスをUnityのプロジェクトに入れる.
そして以下のソースを追加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
//UnZip.cs using System; using System.IO.Compression; using System.Threading.Tasks; using UnityEngine; public class UnZip : MonoBehaviour { private Progress<ZipProgress> _progress; private void Start() { _progress = new Progress<ZipProgress>(); _progress.ProgressChanged += Report; } public async void Extract() { await Task.Run(() => UnZipTask("zipPath", "unzipPath")); } public void UnZipTask(string zipPath, string extratPath) { ZipArchive zip = ZipFile.OpenRead(zipPath); zip.ExtractToDirectory(extratPath, _progress); } private void Report(object sender, ZipProgress zipProgress) { //ここの値を何かしらの方法でProgressBarに入れる Debug.Log("Extract : " + zipProgress.CurrentItem + " " + zipProgress.Processed + " / " + zipProgress.Total); } } |
パスは適宜置き換えて,Extractメソッドを実行することで非同期にプログレスバーを更新しながらzipの解凍ができる.