IEnumerator CoPrepare(Mode mode) { if (_writer != null) { while (_writer.restBytes > 0) // 書き込み待ち { yield return(null); } _writer.Dispose(); _writer = null; } var cache = Caching.GetCacheByPath(_cachePath); if (cache.valid) { Caching.RemoveCache(cache); } DeleteCache(_cachePath); try { System.IO.Directory.CreateDirectory(_cachePath); } catch (System.Exception e) { Debug.LogException(e); } if (mode == Mode.UnityWebRequestAssetBundle) { while (!Caching.ready) { yield return(null); } cache = Caching.AddCache(_cachePath); Caching.currentCacheForWriting = cache; } System.GC.Collect(); for (int i = 0; i < 60; i++) // frametimeWatcherのmaxを吐き出させる { yield return(null); } _totalFileCount = _metaData.Length; _doneFileCount = 0; _doneBytes = 0; _maxSpike = 0; _time = 0f; _maxMemory = 0; }
IEnumerator CoUwr(string cachePath, int slotCount, Mode mode) { var prepare = CoPrepare(mode); while (prepare.MoveNext()) { yield return(null); } var startTime = System.DateTime.Now; _testing = true; var slots = new Slot[slotCount]; for (int i = 0; i < slots.Length; i++) { slots[i] = new Slot(); slots[i].cachePath = cachePath; slots[i].prevBytes = 0; } if (mode == Mode.UwrAsyncWrite) { UnityEngine.Profiling.Profiler.BeginSample("CoUwr.BufferAllocation"); int writerBufferSize; int.TryParse(_writerBufferSizeInputField.text, out writerBufferSize); writerBufferSize = Mathf.Max(writerBufferSize, 1); writerBufferSize *= 1024; int inputBufferSize; int.TryParse(_inputBufferSizeInputField.text, out inputBufferSize); inputBufferSize = Mathf.Max(inputBufferSize, 1); inputBufferSize *= 1024; _writer = new Kayac.FileWriter(_cachePath, writerBufferSize); for (int i = 0; i < slots.Length; i++) { slots[i].inputBuffer = new byte[inputBufferSize]; } UnityEngine.Profiling.Profiler.EndSample(); } int fileIndex = 0; int slotIndex = 0; var metaData = _shuffleToggle.isOn ? _shuffledMetaData : _metaData; while (true) { slots[slotIndex].Update(mode, _log, ref _doneBytes, ref _doneFileCount); if (slots[slotIndex].isDone) { UnityEngine.Profiling.Profiler.BeginSample("Main.CoUwr: createUnityWebRequest"); var slot = slots[slotIndex]; var meta = metaData[fileIndex]; slot.path = meta.name; // モードごとにdownloadHandlerを差し、書き込み準備をする slot.req = new UnityWebRequest(_root + slot.path); if (mode == Mode.UwrSyncWrite) { slot.req.downloadHandler = new DownloadHandlerBuffer(); } else if (mode == Mode.UwrAsyncWrite) { slot.writerHandle = _writer.Begin(slot.path); slot.req.downloadHandler = new Kayac.DownloadHandlerFileWriter( _writer, slot.writerHandle, slot.inputBuffer); } else if (mode == Mode.UwrDownloadHandlerFile) { slot.req.downloadHandler = new DownloadHandlerFile(cachePath + "/" + slot.path); } else if (mode == Mode.UnityWebRequestAssetBundle) { slot.req.downloadHandler = new DownloadHandlerAssetBundle( _root + slot.path, meta.GenerateHash128(), crc: 0); } else { Debug.Assert(false); } slot.req.SendWebRequest(); fileIndex++; // 最後までやったので終わる UnityEngine.Profiling.Profiler.EndSample(); if (fileIndex == metaData.Length) { break; } // _log.Write("Begin: " + slot.req.url); } slotIndex++; if (slotIndex == slots.Length) // 全スロット見たところでyield { slotIndex = 0; yield return(null); } } // 全スロット終了を待つ slotIndex = 0; while (slotIndex < slots.Length) { var slot = slots[slotIndex]; slot.Update(mode, _log, ref _doneBytes, ref _doneFileCount); if (slot.isDone) // 終わってれば次へ { slotIndex++; } else { yield return(null); } } _time = (float)((System.DateTime.Now - startTime).TotalSeconds); _testing = false; var coVerify = CoVerify(mode); while (coVerify.MoveNext()) { yield return(null); } }