How to write a file to local using IEnumerator?

In my project, I need to write a big size file (about 2MB) to the persistantDataPath, and every time it does writing, the frame will drop, there is a slight lag, because I write it in one frame.
Is there any way to write a file using WWW or some other IEnumerator? so that I can use coroutine to do it.

I tried POST using WWW, it doesn’t work.


I tried system.thread, and I tried multi-thread, the system will have lag, and the deltatime between lag frame is ALWAYS 1/3, that’s 0.33333333, always, no matter I’m using filestream.BeginWrite, or start a new thread with thread.start().

can someone provide a valid way to deal with it?

Coroutines will not help in this instance. All coroutines run on the main thread like other Unity scripts.

You could do the writing on another thread.

using System.IO;
using System.Threading;

public class Saver {
  public byte[] data;
  private string path;

  public void Save(string filePath, byte[] dataToSave) {
    path = filePath;
    data = dataToSave;
    var thread = new Thread(SaveData);
  private void SaveData() {
    File.WriteAllBytes(path, data);

Be careful when accessing Unity classes from the other thread. Some may even be completely inaccessible.