From 41d2bf56c87007349d814a6f8c6f7d3405c64c0d Mon Sep 17 00:00:00 2001 From: Jon Manning Date: Mon, 31 Oct 2022 16:11:31 +1100 Subject: [PATCH] Put Unity Localisation behind YARN_ENABLE_EXPERIMENTAL_FEATURES flag --- CHANGELOG.md | 7 +++- Editor/Editors/YarnProjectImporterEditor.cs | 8 ++-- Editor/Importers/YarnProjectImporter.cs | 6 +-- .../UnityLocalisedLineProvider.cs | 41 +++++++++++++++---- 4 files changed, 45 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f98771e..54d64cfd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,9 +16,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Added -- A Unity Localization package localised line provider subclass, `UnityLocalisedLineProvider.cs`, is now provided alongside the other line providers. -- Yarn Project importer now has initial support for Unity's Localization system. - The `DialogueReference` class, which stores a reference to a named node in a given project (and shows a warning in the Inspector if the reference can't be found) has been added. Thanks to [@sttz](https://github.com/sttz) for the [contribution](https://github.com/YarnSpinnerTool/YarnSpinner-Unity/pull/189)! +- Initial work on support for the Unity Localization system has been added. + - These features are currently behind a feature flag. They are not yet considered ready for production use, and we aren't offering support for it yet. + - To access them, add the scripting define symbol `YARN_ENABLE_EXPERIMENTAL_FEATURES`. You should only do this if you know what this involves. + - Yarn Project importer now has initial support for Unity's Localization system. + - A new localised line provider subclass, `UnityLocalisedLineProvider.cs` has been added. ### Changed diff --git a/Editor/Editors/YarnProjectImporterEditor.cs b/Editor/Editors/YarnProjectImporterEditor.cs index b4ef95b6..ae2f1fce 100644 --- a/Editor/Editors/YarnProjectImporterEditor.cs +++ b/Editor/Editors/YarnProjectImporterEditor.cs @@ -17,7 +17,7 @@ using UnityEditor.AddressableAssets; #endif -#if USE_UNITY_LOCALIZATION +#if USE_UNITY_LOCALIZATION && YARN_ENABLE_EXPERIMENTAL_FEATURES using UnityEditor.Localization; using UnityEngine.Localization.Tables; #endif @@ -44,8 +44,10 @@ public class YarnProjectImporterEditor : ScriptedImporterEditor private SerializedProperty predeterminedFunctionsProperty; +#if YARN_ENABLE_EXPERIMENTAL_FEATURES private SerializedProperty useUnityLocalisationSystemProperty; private SerializedProperty unityLocalisationTableCollectionProperty; +#endif public override void OnEnable() { @@ -66,7 +68,7 @@ public override void OnEnable() predeterminedFunctionsProperty = serializedObject.FindProperty(nameof(YarnProjectImporter.ListOfFunctions)); -#if USE_UNITY_LOCALIZATION +#if USE_UNITY_LOCALIZATION && YARN_ENABLE_EXPERIMENTAL_FEATURES useUnityLocalisationSystemProperty = serializedObject.FindProperty(nameof(YarnProjectImporter.UseUnityLocalisationSystem)); unityLocalisationTableCollectionProperty = serializedObject.FindProperty(nameof(YarnProjectImporter.unityLocalisationStringTableCollection)); #endif @@ -130,7 +132,7 @@ public override void OnInspectorGUI() CurrentProjectDefaultLanguageProperty = defaultLanguageProperty; -#if USE_UNITY_LOCALIZATION +#if USE_UNITY_LOCALIZATION && YARN_ENABLE_EXPERIMENTAL_FEATURES EditorGUILayout.PropertyField(useUnityLocalisationSystemProperty, new GUIContent("Use Unity's Built-in Localisation System")); // if we are using the unity localisation system we need a field to add in the string table diff --git a/Editor/Importers/YarnProjectImporter.cs b/Editor/Importers/YarnProjectImporter.cs index da139b42..2480e018 100644 --- a/Editor/Importers/YarnProjectImporter.cs +++ b/Editor/Importers/YarnProjectImporter.cs @@ -147,7 +147,7 @@ public class LanguageToSourceAsset bool IYarnErrorSource.Destroyed => this == null; -#if USE_UNITY_LOCALIZATION +#if USE_UNITY_LOCALIZATION && YARN_ENABLE_EXPERIMENTAL_FEATURES public bool UseUnityLocalisationSystem = false; public StringTableCollection unityLocalisationStringTableCollection; #endif @@ -481,7 +481,7 @@ public override void OnImportAsset(AssetImportContext ctx) project.lineMetadata = new LineMetadata(LineMetadataTableEntriesFromCompilationResult(compilationResult)); } -#if USE_UNITY_LOCALIZATION +#if USE_UNITY_LOCALIZATION && YARN_ENABLE_EXPERIMENTAL_FEATURES if (UseUnityLocalisationSystem) { ConvertInternalYarnStringTableEntriesIntoUnityLocalisedStringTableEntries(StringTableEntriesFromCompilationResult(compilationResult)); @@ -511,7 +511,7 @@ public override void OnImportAsset(AssetImportContext ctx) } -#if USE_UNITY_LOCALIZATION +#if USE_UNITY_LOCALIZATION && YARN_ENABLE_EXPERIMENTAL_FEATURES private void ConvertInternalYarnStringTableEntriesIntoUnityLocalisedStringTableEntries(IEnumerable entries) { if (this.unityLocalisationStringTableCollection == null) diff --git a/Runtime/LineProviders/UnityLocalisedLineProvider.cs b/Runtime/LineProviders/UnityLocalisedLineProvider.cs index 87e52373..218a434a 100644 --- a/Runtime/LineProviders/UnityLocalisedLineProvider.cs +++ b/Runtime/LineProviders/UnityLocalisedLineProvider.cs @@ -3,6 +3,8 @@ using UnityEngine; using Yarn.Unity; +#if YARN_ENABLE_EXPERIMENTAL_FEATURES + #if USE_UNITY_LOCALIZATION using UnityEngine.Localization.Tables; using UnityEngine.Localization; @@ -17,17 +19,21 @@ public override void PrepareForLines(IEnumerable lineIDs) { } // I belie #if USE_UNITY_LOCALIZATION // the string table asset that has all of our (hopefully) localised strings inside - [SerializeField] private LocalizedStringTable strings; + [SerializeField] private LocalizedStringTable stringsTable; + [SerializeField] private LocalizedAssetTable assetTable; + // the runtime table we actually get our strings out of // this changes at runtime depending on the language - private StringTable table; + private StringTable currentStringsTable; + private AssetTable currentAssetTable; + public override LocalizedLine GetLocalizedLine(Yarn.Line line) { var text = line.ID; - if (table != null) + if (currentStringsTable != null) { - text = table[line.ID]?.LocalizedValue ?? line.ID; + text = currentStringsTable[line.ID]?.LocalizedValue ?? line.ID; } return new LocalizedLine() @@ -42,18 +48,33 @@ public override LocalizedLine GetLocalizedLine(Yarn.Line line) public override void Start() { // doing an initial load of the strings - var loading = strings.GetTable(); - table = loading; + if (stringsTable != null) { + currentStringsTable = stringsTable.GetTable(); + } + + if (assetTable != null) { + currentAssetTable = assetTable.GetTable(); + } // registering for any changes to the string table so we can update as needed - strings.TableChanged += OnStringTableChanged; + stringsTable.TableChanged += OnStringTableChanged; + assetTable.TableChanged += OnAssetTableChanged; } - // if the strings change, either by actual modifications at runtime or locale change we update our strings + // if the strings change, either by actual modifications at runtime or + // locale change we update our strings private void OnStringTableChanged(StringTable newTable) { - table = newTable; + currentStringsTable = newTable; } + + // if the assets change, either by actual modifications at runtime or + // locale change we update our assets + private void OnAssetTableChanged(AssetTable value) + { + currentAssetTable = value; + } + #else public override void Start() { @@ -73,3 +94,5 @@ public override LocalizedLine GetLocalizedLine(Yarn.Line line) #endif } } + +#endif