void Update() { //check if request doesn't empty if (Voice_Over_www != null) { //begin download progress of selected language files to inform user about download status DownloadProgress(); //check if server request do not return an error if it returns an error Network error Canvas will be shawn if (Voice_Over_www.error != null) { NetworkCanvas.SetActive(true); // A barrier to prevent user from hit any button while Any Dialouge canvases appeared interceptor.SetActive(true); } } }
// request server to get json string IEnumerator Download_JsonData() { //url of Server to be requested string url = "https://multilang.vrteek.com/api/v1/sounds/lungs"; Json_www = new WWW(url); //wait server to respond with requested json yield return(Json_www); //check if server request does not return an error if it returns an error Network-error Canvas will be shawn if (Json_www.error == null) { Processjson(Json_www.text); } else { Debug.Log("ERROR: " + Json_www.error); NetworkCanvas.SetActive(true); // A barrier to prevent user from hit any button while Any Dialouge canvases appeared interceptor.SetActive(true); } }
// Main Method That Perform Downloading IEnumerator Download_VoiceOverClips(string LAN, string url, int index) { Lan_temp = LAN; //if voice over sound clip does'nt exist in storage files then Coroutine will start downloading and caching it if (!File.Exists(Application.persistentDataPath + "/" + "Languages/" + LAN + "/" + index + ".mp3")) { //give a voice over sound clip url to www to download it Voice_Over_www = new WWW(url); // wait until www finish download from server yield return(Voice_Over_www); //if www return no error either from server or network itself begin caching downloaded file if (Voice_Over_www.error == null && Voice_Over_www.isDone) { print("Caching...."); // caching download file with a unique id and format File.WriteAllBytes(Application.persistentDataPath + "/" + "Languages/" + LAN + "/" + index + ".mp3", Voice_Over_www.bytes); //once download complete move to another url to be downloaded by increase url index indicator urlList_Counter++; //if url index indicator not equal size of the whole url List again start download the new file if (urlList_Counter != parsejson.url.Count) { //(downloaded) is the value of what has been downloaded to continue download from thist point --> //example [if first file or Clip ratio is "0.17" of the whole files size which is "1" ofcource this means if the file has already downloaded progressbar will begin the next download with a value of 17% and so on] downloaded += parsejson.FileSizeRatio[urlList_Counter]; StartCoroutine(Download_VoiceOverClips(LAN, parsejson.url[urlList_Counter], parsejson.index[urlList_Counter])); } // but if url index indicator equal the whole url List then this means download complete else if (urlList_Counter == parsejson.url.Count) { CachingFiles_Finished = true; Selected_Language = LAN; } } // if www return error either from server or network Network error canvas must be appear to let user refreash request and try again else { Debug.Log("ERROR: " + Voice_Over_www.error); NetworkCanvas.SetActive(true); interceptor.SetActive(true); } } // if the file already exists else { long length = new System.IO.FileInfo(Application.persistentDataPath + "/" + "Languages/" + LAN + "/" + index + ".mp3").Length; //must check the file was downloaded with its whole size and if it's not complete re-download it if (length >= parsejson.FileSize[urlList_Counter]) { //the file downloaded with its complete size so url index indicator will be increased and start download another file urlList_Counter++; if (urlList_Counter != parsejson.url.Count) { downloaded += parsejson.FileSizeRatio[urlList_Counter]; StartCoroutine(Download_VoiceOverClips(LAN, parsejson.url[urlList_Counter], parsejson.index[urlList_Counter])); if (urlList_Counter == parsejson.url.Count) { //if url index indicator equal size of the whole url List progress canvas will disappear and set the player prefs language to this compleated language slider0.SetActive(false); PlayerPrefs.SetString("Selected_Language", LAN); SceneManager.LoadScene("Menu"); } } } //if downloaded file not complete re-download it else { StartCoroutine(Download_VoiceOverClips(LAN, parsejson.url[urlList_Counter], parsejson.index[urlList_Counter])); } } }
// when hit refresh button scene will be re-opened the send new request to the server public void Refresh() { NetworkCanvas.SetActive(false); interceptor.SetActive(false); SceneManager.LoadScene("JSON_Download"); }