From bd7be03c4ae1bda13e526f8623b827c76da39dfb Mon Sep 17 00:00:00 2001 From: Shane Marks Date: Thu, 18 Apr 2024 09:41:55 +0100 Subject: [PATCH 1/2] Add SetTable to Line Provider so you can update reference table to runtime --- .../UnityLocalisedLineProvider.cs | 45 +++++++++++++------ 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/Runtime/LineProviders/UnityLocalisedLineProvider.cs b/Runtime/LineProviders/UnityLocalisedLineProvider.cs index 5d346722..e5acbcb6 100644 --- a/Runtime/LineProviders/UnityLocalisedLineProvider.cs +++ b/Runtime/LineProviders/UnityLocalisedLineProvider.cs @@ -120,11 +120,40 @@ public override LocalizedLine GetLocalizedLine(Yarn.Line line) return localizedLine; } - public override void Start() + public override void Start() => SetTable(stringsTable, assetTable); + + // We've been notified that a new strings table has been loaded. + private void OnStringTableChanged(StringTable newTable) + { + currentStringsTable = newTable; + } + + // We've been notified that a new asset table has been loaded. + private void OnAssetTableChanged(AssetTable value) + { + currentAssetTable = value; + } + + /// + /// Updates referenced LocalizedStringTable. + /// + public void SetTable(LocalizedStringTable stringsTable) => SetTable(stringsTable, assetTable); + + /// + /// Updates referenced LocalizedAssetTable. + /// + public void SetTable(LocalizedAssetTable assetTable) => SetTable(stringsTable, assetTable); + + /// + /// Updates both referenced LocalizedStringTable and LocalizedAssetTable. + /// + public void SetTable(LocalizedStringTable stringsTable, LocalizedAssetTable assetTable) { // doing an initial load of the strings if (stringsTable != null) { + this.stringsTable = stringsTable; + // Adding an event handler to TableChanged will trigger an // initial async load of the string table, which will call the // handler on completion. So, we don't set currentStringsTable @@ -134,23 +163,13 @@ public override void Start() if (assetTable != null) { + this.assetTable = assetTable; + // Same logic for asset table as for strings table, above. assetTable.TableChanged += OnAssetTableChanged; } } - // We've been notified that a new strings table has been loaded. - private void OnStringTableChanged(StringTable newTable) - { - currentStringsTable = newTable; - } - - // We've been notified that a new asset table has been loaded. - private void OnAssetTableChanged(AssetTable value) - { - currentAssetTable = value; - } - /// /// Clears all loaded assets from the cache. /// From 73531fe33f55b596e6be03f59926e0fde5adc968 Mon Sep 17 00:00:00 2001 From: Shane Marks Date: Thu, 18 Apr 2024 09:42:23 +0100 Subject: [PATCH 2/2] check for same table before calling TableChanged += --- Runtime/LineProviders/UnityLocalisedLineProvider.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Runtime/LineProviders/UnityLocalisedLineProvider.cs b/Runtime/LineProviders/UnityLocalisedLineProvider.cs index e5acbcb6..bdd48577 100644 --- a/Runtime/LineProviders/UnityLocalisedLineProvider.cs +++ b/Runtime/LineProviders/UnityLocalisedLineProvider.cs @@ -150,7 +150,7 @@ private void OnAssetTableChanged(AssetTable value) public void SetTable(LocalizedStringTable stringsTable, LocalizedAssetTable assetTable) { // doing an initial load of the strings - if (stringsTable != null) + if (stringsTable != null && this.stringsTable != stringsTable) { this.stringsTable = stringsTable; @@ -161,7 +161,7 @@ public void SetTable(LocalizedStringTable stringsTable, LocalizedAssetTable asse stringsTable.TableChanged += OnStringTableChanged; } - if (assetTable != null) + if (assetTable != null && this.assetTable != assetTable) { this.assetTable = assetTable;