/** * Downloads a file to a local path, then loads the file */ private IEnumerator DownloadFile(ModelDataTemplate.ModelImportData modelData, Text progressDisplay, int index, string localPathAndFilename, System.Action <ModelFile> callback = null) { UnityWebRequest fileDownloader = UnityWebRequest.Get(modelData.url); //get size of model first to allocate what is needed long modelSize = 0; yield return(StartCoroutine(GetFileSize(modelData.url, (size) => { modelSize = size; }) )); //set our model download settings fileDownloader.method = UnityWebRequest.kHttpVerbGET; using (var dh = new DownloadHandlerFile(localPathAndFilename)) { dh.removeFileOnAbort = true; fileDownloader.downloadHandler = dh; fileDownloader.SendWebRequest(); while (!fileDownloader.isDone) { string stats = WebGLMemoryStats.GetMoreStats("Downloading"); progressDisplay.text = $"Downloading {modelData.name}: {fileDownloader.downloadProgress.ToString("P")}\n{stats}"; yield return(null); } } System.GC.Collect(); //Debug.Log($"Successfully downloaded model {modelData.name}, size {fileDownloader.downloadedBytes} bytes."); if (fileDownloader.result == UnityWebRequest.Result.ConnectionError || fileDownloader.result == UnityWebRequest.Result.ProtocolError) { Debug.LogError(fileDownloader.error); } //Debug.Log($"Successfully downloaded asset {assetData.name}, size {fileDownloader.downloadedBytes} bytes."); ulong downloadedSize = fileDownloader.downloadedBytes; fileDownloader = null; callback(new ModelFile(localPathAndFilename, modelData.name, downloadedSize)); }
/** * Creates a directory to store the model in and then passes model data onto a download coroutine. */ public IEnumerator GetFileFromURL(ModelDataTemplate.ModelImportData modelData, Text progressDisplay, int index, System.Action <ModelFile> callback) { //Gets guid and filename and extension string[] modelParams = getModelParameters(modelData.url); var guid = modelParams[0]; var fileNameAndExtension = modelParams[1]; //Create a unique directory based on the guid var modelDirectoryLocation = $"{Application.persistentDataPath}/{guid}"; //Debug.Log($"Storing model at {modelDirectoryLocation}"); Directory.CreateDirectory(modelDirectoryLocation); var modelFileLocation = $"{modelDirectoryLocation}/{fileNameAndExtension}"; if (!File.Exists(modelFileLocation)) { Debug.Log($"Downloading {modelData.name}"); yield return(StartCoroutine(DownloadFile(modelData, progressDisplay, index, modelFileLocation, callback))); yield break; } Debug.Log($"{modelData.name} cached. Loading immediately."); progressDisplay.text = $"{modelData.name} cached. Loading immediately."; FileInfo modelInfo = new FileInfo(modelFileLocation); ulong modelSize = (ulong)modelInfo.Length; callback(new ModelFile(modelFileLocation, modelData.name, modelSize)); //TryLoadLocalFile(, callback); //TODO remove }