Пример #1
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            label = new GUIContent(label.text);
            EditorGUI.BeginProperty(position, label, property);

            Rect objectFieldRect = position;

            objectFieldRect.height = 16;

            if (property.objectReferenceValue == null)
            {
                Rect buttonRect = position;
                buttonRect.height = 16;
                buttonRect.x      = position.width - 30;
                buttonRect.width  = 40;

                if (GUI.Button(buttonRect, "New"))
                {
                    property.objectReferenceValue = CreateNewInstance();
                }

                objectFieldRect.width -= 50;
            }
            else
            {
                Rect labelRect = position;
                labelRect.y     += 16;
                labelRect.height = 16;

                TextoData textoData = (TextoData)(property.objectReferenceValue);

                if (textoData.lines == null)
                {
                    textoData.lines = new List <TextoLine>();
                }

                TextoLine englishLine = textoData.lines.Find(x => x.language == TextoLanguage.English);

                if (englishLine != null)
                {
                    EditorGUI.LabelField(labelRect, " ", englishLine.text);
                }
                else
                {
                    EditorGUI.LabelField(labelRect, " ", "[No English text]", EditorStyles.boldLabel);
                }
            }

            EditorGUI.ObjectField(objectFieldRect, property, this.fieldInfo.FieldType, label);

            property.serializedObject.ApplyModifiedProperties();

            EditorGUI.EndProperty();
        }
Пример #2
0
        private TextoData CreateNewInstance()
        {
            string path = EditorUtility.SaveFilePanelInProject("Create new Texto Data", "Texto", "asset", "Hello!", TextoSettingsData.instance.folderPath);

            if (path.Length != 0)
            {
                TextoData textoData = ScriptableObject.CreateInstance <TextoData>();

                AssetDatabase.CreateAsset(textoData, path);
                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();

                return(textoData);
            }

            return(null);
        }
Пример #3
0
        private static IEnumerator PullRoutine(TextoSettingsData textoSettingsData)
        {
            EditorUtility.DisplayProgressBar("Pulling Textos!", "Please wait...", 0);

            string range = "A1:Z2000";

            foreach (TextoSettingsData.GoogleSheetInfo googleSheetInfo in textoSettingsData.googleSheetsInfos)
            {
                foreach (string tableID in googleSheetInfo.tableIDs)
                {
                    EditorUtility.DisplayProgressBar("Pulling Textos!", "Please wait...", ExtraMath.Map(googleSheetInfo.tableIDs.IndexOf(tableID), 0, googleSheetInfo.tableIDs.Count, 0.05f, 1));

                    string url = string.Format("https://sheets.googleapis.com/v4/spreadsheets/{0}/values/{1}!{2}?key={3}", googleSheetInfo.sheetID, tableID, range, TextoSettingsData.instance.googleAPIKey);

                    using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
                    {
                        yield return(webRequest.SendWebRequest());

                        try
                        {
                            string[] pages = url.Split('/');
                            int      page  = pages.Length - 1;

                            if (webRequest.isNetworkError)
                            {
                                Debug.Log(pages[page] + ": Error: " + webRequest.error);
                            }
                            else
                            {
                                Variant          jsonVar   = Decoder.Decode(webRequest.downloadHandler.text);
                                GoogleSheetsJSON textoJSON = new GoogleSheetsJSON();
                                jsonVar.Make(out textoJSON);

                                int           labelsRow        = 0;
                                List <string> labelsRowStrings = new List <string>(textoJSON.values[labelsRow]);

                                int uniqueIDColumn = labelsRowStrings.FindIndex(x => x.ToLower() == "line");

                                if (uniqueIDColumn < 0)
                                {
                                    uniqueIDColumn = labelsRowStrings.FindIndex(x => x.ToLower() == "uniqueid");
                                }

                                if (uniqueIDColumn >= 0)
                                {
                                    Dictionary <TextoLanguage, int> languageColumns = new Dictionary <TextoLanguage, int>();

                                    foreach (TextoLanguage language in TextoSettingsData.instance.languages)
                                    {
                                        int index = labelsRowStrings.FindIndex(x => Texto.StringToLanguage(x) == language);
                                        languageColumns.Add(language, index);
                                    }

                                    string[] paths = AssetDatabase.FindAssets("t:TextoData");

                                    List <TextoData> allTextos = new List <TextoData>();

                                    for (int i = 0; i < paths.Length; i++)
                                    {
                                        allTextos.Add(AssetDatabase.LoadAssetAtPath <TextoData>(AssetDatabase.GUIDToAssetPath(paths[i])));
                                    }

                                    for (int i = labelsRow + 1; i < textoJSON.values.Length; i++)
                                    {
                                        if (textoJSON.values.Length <= i)
                                        {
                                            continue;
                                        }

                                        if (textoJSON.values[i].Length <= uniqueIDColumn)
                                        {
                                            continue;
                                        }

                                        string uniqueID = textoJSON.values[i][uniqueIDColumn];

                                        if (!string.IsNullOrEmpty(uniqueID) && !string.IsNullOrWhiteSpace(uniqueID))
                                        {
                                            TextoData matchedData = allTextos.Find(x => x.uniqueID.ToLower() == uniqueID.ToLower());

                                            if (matchedData == null)
                                            {
                                                Debug.Log("Creating new Texto with ID: " + uniqueID);
                                                TextoData newTexto = ScriptableObject.CreateInstance <TextoData>();

                                                string parentFolderPath = TextoSettingsData.instance.folderPath;

                                                if (!AssetDatabase.IsValidFolder(parentFolderPath))
                                                {
                                                    Debug.Log(string.Format("Folder \"{0}\" doesn't exist!", parentFolderPath));
                                                    EditorUtility.ClearProgressBar();
                                                    yield break;
                                                }

                                                string folderPath = parentFolderPath + "/" + tableID;

                                                if (!AssetDatabase.IsValidFolder(folderPath))
                                                {
                                                    AssetDatabase.CreateFolder(parentFolderPath, tableID);
                                                }

                                                AssetDatabase.CreateAsset(newTexto, folderPath + "/" + uniqueID + ".asset");
                                                matchedData = newTexto;

                                                SerializedObject newSerializedObject = new SerializedObject(matchedData);
                                                newSerializedObject.FindProperty("uniqueID").stringValue = uniqueID;
                                                newSerializedObject.ApplyModifiedProperties();
                                            }

                                            SerializedObject serializedObject = new SerializedObject(matchedData);

                                            foreach (TextoLanguage language in TextoSettingsData.instance.languages)
                                            {
                                                int    languageColumn = languageColumns[language];
                                                string languageText   = "[Not found]";

                                                if (languageColumn >= 0)
                                                {
                                                    if (languageColumn < textoJSON.values[i].Length)
                                                    {
                                                        if (language == TextoLanguage.Arabic)
                                                        {
                                                            languageText = ArabicFixer.Fix(textoJSON.values[i][languageColumn]);
                                                        }
                                                        else
                                                        {
                                                            languageText = textoJSON.values[i][languageColumn];
                                                        }
                                                    }
                                                }

                                                int lineIndex = -1;

                                                for (int j = 0; j < matchedData.lines.Count; j++)
                                                {
                                                    if (matchedData.lines[j].language == language)
                                                    {
                                                        lineIndex = j;
                                                        break;
                                                    }
                                                }

                                                if (lineIndex >= 0)
                                                {
                                                    SerializedProperty lineProperty = serializedObject.FindProperty(string.Format("lines.Array.data[{0}]", lineIndex));
                                                    lineProperty.FindPropertyRelative("text").stringValue = languageText;
                                                    serializedObject.ApplyModifiedProperties();
                                                }
                                                else
                                                {
                                                    SerializedProperty linesProperty = serializedObject.FindProperty("lines");

                                                    int newIndex = matchedData.lines.Count;

                                                    linesProperty.InsertArrayElementAtIndex(newIndex);

                                                    SerializedProperty lineProperty = serializedObject.FindProperty(string.Format("lines.Array.data[{0}]", newIndex));
                                                    lineProperty.FindPropertyRelative("text").stringValue        = languageText;
                                                    lineProperty.FindPropertyRelative("language").enumValueIndex = (int)language;
                                                    serializedObject.ApplyModifiedProperties();
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            EditorUtility.ClearProgressBar();
                            Debug.LogException(e);
                            yield break;
                        }
                    }
                }
            }

            EditorUtility.ClearProgressBar();
        }