Skip to content

Commit

Permalink
Put Unity Localisation behind YARN_ENABLE_EXPERIMENTAL_FEATURES flag
Browse files Browse the repository at this point in the history
  • Loading branch information
desplesda committed Oct 31, 2022
1 parent fe4399f commit 41d2bf5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 5 additions & 3 deletions Editor/Editors/YarnProjectImporterEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
{
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions Editor/Importers/YarnProjectImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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<StringTableEntry> entries)
{
if (this.unityLocalisationStringTableCollection == null)
Expand Down
41 changes: 32 additions & 9 deletions Runtime/LineProviders/UnityLocalisedLineProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,17 +19,21 @@ public override void PrepareForLines(IEnumerable<string> 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()
Expand All @@ -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()
{
Expand All @@ -73,3 +94,5 @@ public override LocalizedLine GetLocalizedLine(Yarn.Line line)
#endif
}
}

#endif

0 comments on commit 41d2bf5

Please sign in to comment.