public static void DrawImportCSVConfigUI(DataboxObject database) { selectedOption = GUILayout.Toolbar(selectedOption, importOptions); if (selectedOption == 0) { using (new GUILayout.VerticalScope("Window")) { //GUILayout.Label("Import from GOOGLE"); using (new GUILayout.HorizontalScope("Box")) { GUILayout.Label("Google Sheet URL:", GUILayout.Width(120)); database.googleSheetUrl = GUILayout.TextField(database.googleSheetUrl); GUILayout.Label(new GUIContent("[?]", "Make sure public link sharing is enabled and enter the share link here."), GUILayout.Width(20)); if (GUILayout.Button(">", GUILayout.Width(30))) { Application.OpenURL(database.googleSheetUrl); } } using (new GUILayout.VerticalScope("Box")) { GUILayout.Label(new GUIContent("WORKSHEETS [?]", "Each worksheet has an unique ID. You can find the ID in the URL at: gid='ID-NUMBER-HERE'")); if (GUILayout.Button("Add new worksheet ID")) { database.googleWorksheets.Add(new DataboxObject.GoogleWorksheet()); } for (int i = 0; i < database.googleWorksheets.Count; i++) { using (new GUILayout.HorizontalScope("Box")) { GUI.color = colBlue; GUILayout.Label((i + 1).ToString(), GUILayout.Width(20)); GUI.color = Color.white; GUILayout.Label("Google Worksheet Name:", GUILayout.Width(170)); database.googleWorksheets[i].name = GUILayout.TextField(database.googleWorksheets[i].name, GUILayout.Width(150)); GUILayout.Label("Google Worksheet ID:"); database.googleWorksheets[i].id = GUILayout.TextField(database.googleWorksheets[i].id); if (GUILayout.Button("x", GUILayout.Width(20))) { database.googleWorksheets.RemoveAt(i); } } } } if (string.IsNullOrEmpty(database.googleSheetUrl) || string.IsNullOrEmpty(database.googleSheetUrl) || database.googleWorksheets.Count == 0) { GUI.enabled = false; } else { GUI.enabled = true; } using (new GUILayout.HorizontalScope()) { if (GUILayout.Button("Download & Append", GUILayout.Height(30))) { DataboxGoogleSheetDownloader.Download(database, DataboxGoogleSheetDownloader.ImportType.Append); } if (GUILayout.Button("Download & Replace", GUILayout.Height(30))) { if (EditorUtility.DisplayDialog("Replace database?", "Are you sure you want to replace the current database?", "Replace", "Cancel")) { DataboxGoogleSheetDownloader.Download(database, DataboxGoogleSheetDownloader.ImportType.Replace); } GUIUtility.ExitGUI(); } } GUI.enabled = true; GUILayout.Label("Log:"); GUILayout.Label(DataboxGoogleSheetDownloader.report); } } if (selectedOption == 1) { using (new GUILayout.VerticalScope("Window")) { if (GUILayout.Button("Select file...")) { localFilePath = EditorUtility.OpenFilePanel("Select CSV file", "", ""); GUIUtility.ExitGUI(); } if (localFilePath != lastFileName) { fileOK = System.IO.File.Exists(localFilePath); } if (fileOK) { EditorGUILayout.HelpBox("Selected file: " + localFilePath, MessageType.Info); } using (new GUILayout.HorizontalScope()) { GUILayout.Label("Table name:", GUILayout.Width(100)); localFileTableName = GUILayout.TextField(localFileTableName); } if (string.IsNullOrEmpty(localFileTableName) || string.IsNullOrEmpty(localFilePath)) { GUI.enabled = false; } else { GUI.enabled = true; } using (new GUILayout.HorizontalScope()) { if (GUILayout.Button("Append", GUILayout.Height(30))) { if (System.IO.File.Exists(localFilePath)) { // open file at path var _csvString = ReadLocalFile(localFilePath); List <DataboxCSVConverter.Entry> entries = new List <DataboxCSVConverter.Entry>(); DataboxCSVConverter.ConvertCSV(_csvString, out entries); #if DATABOX_LOCALIZATION DataboxCSVConverter.AppendToDB(database, localFileTableName, entries, database.isLocalizationTable); #else DataboxCSVConverter.AppendToDB(database, localFileTableName, entries); #endif } } if (GUILayout.Button("Replace", GUILayout.Height(30))) { if (EditorUtility.DisplayDialog("Replace database?", "Are you sure you want to replace the current database?", "Replace", "Cancel")) { if (System.IO.File.Exists(localFilePath)) { // open file at path var _csvString = ReadLocalFile(localFilePath); List <DataboxCSVConverter.Entry> entries = new List <DataboxCSVConverter.Entry>(); DataboxCSVConverter.ConvertCSV(_csvString, out entries); #if DATABOX_LOCALIZATION DataboxCSVConverter.ReplaceDB(database, localFileTableName, entries, database.isLocalizationTable); #else DataboxCSVConverter.ReplaceDB(database, localFileTableName, entries); #endif } } GUIUtility.ExitGUI(); } } GUI.enabled = true; } } }
IEnumerator ImportFromGoogleIE(DataboxObject database, DataboxGoogleSheetDownloader.ImportType _importType) { DataboxCSVConverter.firstTimeReplace = true; for (int i = 0; i < database.googleWorksheets.Count; i++) { var _url = database.googleSheetUrl; _url = FixURL(_url, database.googleWorksheets[i].id); UnityWebRequest _download = UnityWebRequest.Get(_url); yield return(_download.SendWebRequest()); while (_download.isDone == false) { yield return(null); } // handle an unknown internet error if (_download.isNetworkError || _download.isHttpError) { Debug.LogWarningFormat("Couldn't retrieve file at <{0}> and error message was: {1}", _download.url, _download.error); } else { // make sure the fetched file isn't just a Google login page if (_download.downloadHandler.text.Contains("google-site-verification")) { Debug.LogWarningFormat("Couldn't retrieve file at <{0}> because the Google Doc didn't have public link sharing enabled", _download.url); continue; } List <DataboxCSVConverter.Entry> entries = new List <DataboxCSVConverter.Entry>(); DataboxCSVConverter.ConvertCSV(_download.downloadHandler.text, out entries); yield return(new WaitForSeconds(1f)); switch (_importType) { case DataboxGoogleSheetDownloader.ImportType.Append: DataboxCSVConverter.AppendToDB(database, database.googleWorksheets[i].name, entries); break; case DataboxGoogleSheetDownloader.ImportType.Replace: DataboxCSVConverter.ReplaceDB(database, database.googleWorksheets[i].name, entries); break; } } _download.Dispose(); } if (database.OnImportFromGoogleComplete != null) { database.OnImportFromGoogleComplete(); } }
static IEnumerator DownloadIE(DataboxObject database, ImportType _importType) { DataboxCSVConverter.firstTimeReplace = true; report = "Start downloading from Google"; for (int i = 0; i < database.googleWorksheets.Count; i++) { var _url = database.googleSheetUrl; _url = FixURL(_url, database.googleWorksheets[i].id); //Debug.Log(_url); UnityWebRequest _download = UnityWebRequest.Get(_url); yield return(_download.SendWebRequest()); while (_download.isDone == false) { yield return(null); } // handle an unknown internet error if (_download.isNetworkError || _download.isHttpError) { Debug.LogWarningFormat("Couldn't retrieve file at <{0}> and error message was: {1}", _download.url, _download.error); report += string.Format("\n- [ERROR] {0}: {1}", database.googleWorksheets[i].name, _download.error); } else { // make sure the fetched file isn't just a Google login page if (_download.downloadHandler.text.Contains("google-site-verification")) { Debug.LogWarningFormat("Couldn't retrieve file at <{0}> because the Google Doc didn't have public link sharing enabled", _download.url); report += string.Format("\n- [ERROR] {0}: This Google Docs share link does not have 'VIEW' access; make sure you enable link sharing.", _url, _download.url); continue; } //Debug.Log(_download.downloadHandler.text); report += string.Format("\n- {0} : DOWNLOADED SUCCESSFULLY", database.googleWorksheets[i].id); List <DataboxCSVConverter.Entry> entries = new List <DataboxCSVConverter.Entry>(); DataboxCSVConverter.ConvertCSV(_download.downloadHandler.text, out entries); //for (int e = 0; e < entries.Count; e ++) //{ // Debug.Log(entries[e].entryName); //} yield return(new WaitForSeconds(1f)); switch (_importType) { case ImportType.Append: DataboxCSVConverter.AppendToDB(database, database.googleWorksheets[i].name, entries); break; case ImportType.Replace: DataboxCSVConverter.ReplaceDB(database, database.googleWorksheets[i].name, entries); break; } } _download.Dispose(); } }