private void Start() { state = State.Wait; Versions.Load(); }
public void Update() { if (isDone) { return; } switch (state) { case State.HeadRequest: if (request.error != null) { error = request.error; } if (request.isDone) { maxlen = long.Parse(request.GetResponseHeader("Content-Length")); request.Dispose(); request = null; var dir = Path.GetDirectoryName(savePath); if (!Directory.Exists(dir)) { // ReSharper disable once AssignNullToNotNullAttribute Directory.CreateDirectory(dir); } fs = new FileStream(savePath, FileMode.OpenOrCreate, FileAccess.Write); len = fs.Length; var emptyVersion = string.IsNullOrEmpty(version); var oldVersion = Versions.Get(savePath); var emptyOldVersion = string.IsNullOrEmpty(oldVersion); if (emptyVersion || emptyOldVersion || !oldVersion.Equals(version)) { Versions.Set(savePath, version); len = 0; } if (len < maxlen) { fs.Seek(len, SeekOrigin.Begin); request = UnityWebRequest.Get(url); request.SetRequestHeader("Range", "bytes=" + len + "-"); #if UNITY_2017_1_OR_NEWER request.SendWebRequest(); #else request.Send(); #endif index = 0; state = State.BodyRequest; } else { state = State.FinishRequest; } } break; case State.BodyRequest: if (request.error != null) { error = request.error; } if (!request.isDone) { WriteBuffer(); } else { WriteBuffer(); if (fs != null) { fs.Close(); fs.Dispose(); } request.Dispose(); state = State.FinishRequest; } break; case State.FinishRequest: if (completed != null) { completed.Invoke(); } isDone = true; state = State.Completed; break; } }