Beispiel #1
0
    void UpdateLocalizations(string newBaseCsv)
    {
        // Goes through each localization string table csv file and merges it with the new base string table
        YarnImporter yarnImporter = (target as YarnImporter);

        for (int i = 0; i < yarnImporter.localizations.Length; i++)
        {
            // Feeds the merge function with the old localized string table, the new base string table and the current file name in case it has changed.
            string merged = MergeStringTables(yarnImporter.localizations[i].text.text, newBaseCsv, Path.GetFileNameWithoutExtension(yarnImporter.assetPath));

            // Save the merged csv to file (copied from the "Create New Localisation" button).
            var assetDirectory = Path.GetDirectoryName(yarnImporter.assetPath);

            string language            = yarnImporter.localizations[i].languageName;
            var    newStringsTablePath = $"{assetDirectory}/{Path.GetFileNameWithoutExtension(yarnImporter.assetPath)} ({language}).csv";
            newStringsTablePath = AssetDatabase.GenerateUniqueAssetPath(newStringsTablePath);

            var writer = File.CreateText(newStringsTablePath);
            writer.Write(merged);
            writer.Close();

            AssetDatabase.ImportAsset(newStringsTablePath);

            var asset = AssetDatabase.LoadAssetAtPath <TextAsset>(newStringsTablePath);

            EditorGUIUtility.PingObject(asset);
        }
    }
Beispiel #2
0
    public override void OnInspectorGUI()
    {
        serializedObject.Update();
        EditorGUILayout.Space();
        YarnImporter yarnImporter = (target as YarnImporter);

        var cultures = cultureInfo.Select(c => $"{c.DisplayName}");
        // Array of translations that have been added to this asset + base language
        var culturesAvailableOnAsset = yarnImporter.localizations.
                                       Select(element => element.languageName).
                                       Append(cultureInfo[selectedLanguageIndex].Name).
                                       OrderBy(element => element).
                                       ToArray();

        selectedLanguageIndex        = EditorGUILayout.Popup("Base Language", selectedLanguageIndex, cultures.ToArray());
        baseLanguageProp.stringValue = cultureInfo[selectedLanguageIndex].Name;

        if (yarnImporter.isSuccesfullyCompiled == false)
        {
            EditorGUILayout.HelpBox(yarnImporter.compilationErrorMessage, MessageType.Error);
            return;
        }

        EditorGUILayout.Space();

        var canCreateLocalisation = yarnImporter.StringsAvailable == true && yarnImporter.AnyImplicitStringIDs == false;

        using (new EditorGUI.DisabledScope(!canCreateLocalisation))
            using (new EditorGUILayout.HorizontalScope()) {
                selectedNewTranslationLanguageIndex = EditorGUILayout.Popup(selectedNewTranslationLanguageIndex, cultures.ToArray());

                if (GUILayout.Button("Create New Localisation", EditorStyles.miniButton))
                {
                    var stringsTableText = AssetDatabase
                                           .LoadAllAssetsAtPath(yarnImporter.assetPath)
                                           .OfType <TextAsset>()
                                           .FirstOrDefault()?
                                           .text ?? "";

                    var selectedCulture = cultureInfo[selectedNewTranslationLanguageIndex];

                    var assetDirectory = Path.GetDirectoryName(yarnImporter.assetPath);

                    var newStringsTablePath = $"{assetDirectory}/{Path.GetFileNameWithoutExtension(yarnImporter.assetPath)} ({selectedCulture.Name}).csv";
                    newStringsTablePath = AssetDatabase.GenerateUniqueAssetPath(newStringsTablePath);

                    var writer = File.CreateText(newStringsTablePath);
                    writer.Write(stringsTableText);
                    writer.Close();

                    AssetDatabase.ImportAsset(newStringsTablePath);

                    var asset = AssetDatabase.LoadAssetAtPath <TextAsset>(newStringsTablePath);

                    EditorGUIUtility.PingObject(asset);

                    // Automatically add newly created translation csv file to yarn program
                    var localizationsIndex             = System.Array.FindIndex(yarnImporter.localizations, element => element.languageName == selectedCulture.Name);
                    var localizationSerializedProperty = serializedObject.FindProperty("localizations");
                    if (localizationsIndex != -1)
                    {
                        localizationSerializedProperty.GetArrayElementAtIndex(localizationsIndex).FindPropertyRelative("text").objectReferenceValue = asset;
                    }
                    else
                    {
                        localizationSerializedProperty.InsertArrayElementAtIndex(localizationSerializedProperty.arraySize);
                        localizationSerializedProperty.GetArrayElementAtIndex(localizationSerializedProperty.arraySize - 1).FindPropertyRelative("text").objectReferenceValue = asset;
                        localizationSerializedProperty.GetArrayElementAtIndex(localizationSerializedProperty.arraySize - 1).FindPropertyRelative("languageName").stringValue  = selectedCulture.Name;
                    }
                }
            }

        if (yarnImporter.StringsAvailable == false)
        {
            EditorGUILayout.HelpBox("This file doesn't contain any localisable lines or options.", MessageType.Info);
        }

        if (yarnImporter.AnyImplicitStringIDs)
        {
            EditorGUILayout.HelpBox("Add #line: tags to all lines and options to enable creating new localisations. Either add them manually, or click Add Line Tags to automatically add tags. Note that this will modify your files on disk, and cannot be undone.", MessageType.Info);
            if (GUILayout.Button("Add Line Tags"))
            {
                AddLineTagsToFile(yarnImporter.assetPath);
            }
        }
        // Only update localizations if line tags exist and localizations exist
        else if (yarnImporter.localizations.Length > 0)
        {
            if (GUILayout.Button("Update Localizations"))
            {
                UpdateLocalizations(AssetDatabase
                                    .LoadAllAssetsAtPath(yarnImporter.assetPath)
                                    .OfType <TextAsset>()
                                    .FirstOrDefault()?
                                    .text ?? "");
            }
        }

        // Localization list
        EditorGUILayout.PropertyField(serializedObject.FindProperty("localizations"), true);

        var success = serializedObject.ApplyModifiedProperties();

#if UNITY_2018
        if (success)
        {
            EditorUtility.SetDirty(target);
            AssetDatabase.WriteImportSettingsIfDirty(AssetDatabase.GetAssetPath(target));
        }
#endif
#if UNITY_2019_1_OR_NEWER
        ApplyRevertGUI();
#endif
    }
Beispiel #3
0
    public override void OnInspectorGUI()
    {
        EditorGUILayout.Space();

        var cultures = cultureInfo.Select(c => $"{c.DisplayName}");

        using (var check = new EditorGUI.ChangeCheckScope()) {
            selectedLanguageIndex = EditorGUILayout.Popup("Base Language", selectedLanguageIndex, cultures.ToArray());

            if (check.changed)
            {
                baseLanguageProp.stringValue = cultureInfo[selectedLanguageIndex].Name;
                serializedObject.ApplyModifiedProperties();
                (target as YarnImporter).SaveAndReimport();
            }
        }

        YarnImporter yarnImporter = (target as YarnImporter);

        if (yarnImporter.isSuccesfullyCompiled == false)
        {
            EditorGUILayout.HelpBox(yarnImporter.compilationErrorMessage, MessageType.Error);
            return;
        }

        EditorGUILayout.Space();

        var canCreateLocalisation = yarnImporter.StringsAvailable == true && yarnImporter.AnyImplicitStringIDs == false;

        using (new EditorGUI.DisabledScope(!canCreateLocalisation))
            using (new EditorGUILayout.HorizontalScope()) {
                selectedNewTranslationLanguageIndex = EditorGUILayout.Popup(selectedNewTranslationLanguageIndex, cultures.ToArray());

                if (GUILayout.Button("Create New Localisation", EditorStyles.miniButton))
                {
                    var stringsTableText = AssetDatabase
                                           .LoadAllAssetsAtPath(yarnImporter.assetPath)
                                           .OfType <TextAsset>()
                                           .FirstOrDefault()?
                                           .text ?? "";

                    var selectedCulture = cultureInfo[selectedNewTranslationLanguageIndex];

                    var assetDirectory = Path.GetDirectoryName(yarnImporter.assetPath);

                    var newStringsTablePath = $"{assetDirectory}/{Path.GetFileNameWithoutExtension(yarnImporter.assetPath)} ({selectedCulture.Name}).csv";
                    newStringsTablePath = AssetDatabase.GenerateUniqueAssetPath(newStringsTablePath);

                    var writer = File.CreateText(newStringsTablePath);
                    writer.Write(stringsTableText);
                    writer.Close();

                    AssetDatabase.ImportAsset(newStringsTablePath);

                    var asset = AssetDatabase.LoadAssetAtPath <TextAsset>(newStringsTablePath);

                    EditorGUIUtility.PingObject(asset);
                }
            }

        if (yarnImporter.StringsAvailable == false)
        {
            EditorGUILayout.HelpBox("This file doesn't contain any localisable lines or options.", MessageType.Info);
        }

        if (yarnImporter.AnyImplicitStringIDs)
        {
            EditorGUILayout.HelpBox("Add #line: tags to all lines and options to enable creating new localisations. Either add them manually, or click Add Line Tags to automatically add tags. Note that this will modify your files on disk, and cannot be undone.", MessageType.Info);
            if (GUILayout.Button("Add Line Tags"))
            {
                AddLineTagsToFile(yarnImporter.assetPath);
            }
        }
    }