public void OnDownloadDataPress(int id) { m_Result = ERESULT.SUCCESS; m_Progress.Show(); m_Progress.SetProgress("Wait.."); StartCoroutine(RefreshData((EDATATYPE)id)); }
public IEnumerator DownloadSubData(EDATATYPE tData) { switch (tData) { case EDATATYPE.VOCABULARY: m_VocabularySet = new List <WordDictionary>(); break; case EDATATYPE.PHRASAL_VERBS: m_PhrasalVerbSet = new List <WordDictionary>(); break; case EDATATYPE.IDIOMS: m_IdiomsSet = new List <WordDictionary>(); break; case EDATATYPE.EXPRESSIONS: m_ExpressionsSet = new List <WordDictionary>(); break; case EDATATYPE.GRAMMAR: m_grammarSet = new List <GrammarDictionary>(); break; } m_Result = ERESULT.SUCCESS; if (m_InfoFileList == null) { m_Result = ERESULT.FAIL; yield break; } int indexData = (int)tData; if (indexData >= m_InfoFileList.Count) { m_Result = ERESULT.FAIL; yield break; } string pictureFolder = Path.Combine(m_CloudDataUrl, m_PicturesFolder); for (int iData = 0; iData < m_InfoFileList[indexData].Data.Data.Count; iData++) { m_Progress.SetProgress("Downloading " + m_InfoFileList[indexData].Data.Data[iData].FileName); string fileName = m_InfoFileList[indexData].Data.Data[iData].FileName + ".json"; string dataCloudURL = m_CloudDataUrl + "/" + m_InfoFileList[indexData].DataFolderName + "/" + fileName; Debug.Log("<color=purple>" + "[LauncherControl.DownloadData] Retrieving (" + (iData + 1) + "/" + m_InfoFileList[indexData].Data.Data.Count + ") - URL: " + dataCloudURL + "</color>"); WWW wwwDataFile = new WWW(dataCloudURL); yield return(wwwDataFile); if (!string.IsNullOrEmpty(wwwDataFile.text)) { string dataLocalURL = Path.Combine(Application.persistentDataPath, m_InfoFileList[indexData].DataFolderName); dataLocalURL = Path.Combine(dataLocalURL, fileName); if (SaveFileToLocal(dataLocalURL, wwwDataFile)) { if (m_InfoFileList[indexData].DataType == EDATATYPE.VOCABULARY) { WordDictionary set = JsonUtility.FromJson <WordDictionary>(wwwDataFile.text); m_VocabularySet.Add(set); // Download images for (int iWord = 0; iWord < set.Data.Count; iWord++) { if (!string.IsNullOrEmpty(set.Data[iWord].PictureName)) { string pictureUrl = Path.Combine(pictureFolder, set.Data[iWord].PictureName); Debug.Log("<color=purple>" + "[LauncherControl.DownloadData] Picture URL at " + pictureUrl + "</color>"); WWW wwwPictureFile = new WWW(pictureUrl); yield return(wwwPictureFile); if (wwwPictureFile.texture != null) { Debug.Log("<color=purple>" + "[LauncherControl.RequestRecipe] Texture: (" + wwwPictureFile.texture.width + " x " + wwwPictureFile.texture.height + ")" + "</color>"); Texture2D texture = new Texture2D(wwwPictureFile.texture.width, wwwPictureFile.texture.height, TextureFormat.DXT1, false); wwwPictureFile.LoadImageIntoTexture(texture); Rect rec = new Rect(0, 0, texture.width, texture.height); set.Data[iWord].Sprite = Sprite.Create(texture, rec, new Vector2(0.5f, 0.5f), 100); yield return(new WaitForEndOfFrame()); string localPicture = Path.Combine(m_LocalPictureDirectory, set.Data[iWord].PictureName); // Load Picture in memory SaveFileToLocal(localPicture, wwwPictureFile); } else { m_Result = ERESULT.FAIL; break; } wwwPictureFile.Dispose(); wwwPictureFile = null; } } } else if (m_InfoFileList[indexData].DataType == EDATATYPE.GRAMMAR) { GrammarDictionary set = JsonUtility.FromJson <GrammarDictionary>(wwwDataFile.text); set.Category = m_InfoFileList[indexData].Data.Data[iData].FileName; m_grammarSet.Add(set); } else if (m_InfoFileList[indexData].DataType == EDATATYPE.PHRASAL_VERBS) { WordDictionary set = JsonUtility.FromJson <WordDictionary>(wwwDataFile.text); m_PhrasalVerbSet.Add(set); } else if (m_InfoFileList[indexData].DataType == EDATATYPE.EXPRESSIONS) { WordDictionary set = JsonUtility.FromJson <WordDictionary>(wwwDataFile.text); m_ExpressionsSet.Add(set); } else if (m_InfoFileList[indexData].DataType == EDATATYPE.IDIOMS) { WordDictionary set = JsonUtility.FromJson <WordDictionary>(wwwDataFile.text); m_IdiomsSet.Add(set); } } } else { m_Result = ERESULT.FAIL; break; } } m_Progress.Hide(); }
private IEnumerator DownloadIndexData(EDATATYPE dataType) { int indexType = (int)dataType; string indexLocalFileURL = Path.Combine(Application.persistentDataPath, m_InfoFileList[indexType].FileName); // Index cloud url string indexCloudURL = m_CloudDataUrl + "/" + m_InfoFileList[indexType].FileName; WWW wwwFile = new WWW(indexCloudURL); yield return(wwwFile); string data = wwwFile.text; Debug.Log("<color=purple>" + "[LauncherControl.DownloadIndexData] File Downloaded: " + indexCloudURL + "</color>"); if (!string.IsNullOrEmpty(data)) { try { m_InfoFileList[indexType].Data = JsonUtility.FromJson <FileData>(data); } catch (Exception e) { Debug.Log("<color=purple>" + "[LauncherControl.DownloadData] There was an error parsing json: " + data + " - ERROR: " + e.Message + "</color>"); if (OnDownloadCompleted != null) { OnDownloadCompleted(ERESULT.FAIL, "ERROR PARSING JSON"); } } yield return(new WaitForEndOfFrame()); bool result = SaveFileToLocal(indexLocalFileURL, wwwFile); if (!result) { if (OnDownloadCompleted != null) { OnDownloadCompleted(ERESULT.FAIL, "ERROR PARSING JSON"); } yield break; } m_Result = ERESULT.SUCCESS; Debug.Log("<color=purple>" + "[LauncherControl.DownloadIndexData] Downloading subdata: " + dataType.ToString() + "</color>"); yield return(DownloadSubData(dataType)); m_Progress.Show(); if (m_Result == ERESULT.FAIL) { if (OnDownloadCompleted != null) { OnDownloadCompleted(ERESULT.FAIL, "Fail to download sub data"); } } } else { if (OnDownloadCompleted != null) { OnDownloadCompleted(ERESULT.FAIL, "Empty Data"); } } }