From d91ad823369d4cf919ef02471bfdeee441b48296 Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Tue, 7 Apr 2015 17:02:02 +0100 Subject: [PATCH] Improved performance of populating localized strings #8 --- Assets/Fungus/Flowchart/Scripts/Language.cs | 31 +++++++++++++++------ 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/Assets/Fungus/Flowchart/Scripts/Language.cs b/Assets/Fungus/Flowchart/Scripts/Language.cs index b55170ed..79513937 100644 --- a/Assets/Fungus/Flowchart/Scripts/Language.cs +++ b/Assets/Fungus/Flowchart/Scripts/Language.cs @@ -255,6 +255,13 @@ namespace Fungus return; } + // Cache a lookup table of flowcharts in the scene + Dictionary flowchartDict = new Dictionary(); + foreach (Flowchart flowChart in GameObject.FindObjectsOfType()) + { + flowchartDict[flowChart.name] = flowChart; + } + for (int i = 1; i < csvTable.Length; ++i) { string[] fields = csvTable[i]; @@ -270,12 +277,12 @@ namespace Fungus if (languageEntry.Length > 0) { localizedStrings[stringId] = languageEntry; - PopulateGameString(stringId, languageEntry); + PopulateGameString(stringId, languageEntry, flowchartDict); } } } - public virtual void PopulateGameString(string stringId, string text) + public virtual void PopulateGameString(string stringId, string text, Dictionary flowchartDict) { string[] idParts = stringId.Split('.'); if (idParts.Length == 0) @@ -291,11 +298,15 @@ namespace Fungus return; } - string flowchartName = idParts[1]; + string flowchartId = idParts[1]; + if (!flowchartDict.ContainsKey(flowchartId)) + { + return; + } + Flowchart flowchart = flowchartDict[flowchartId]; + int itemId = int.Parse(idParts[2]); - GameObject go = GameObject.Find(flowchartName); - Flowchart flowchart = go.GetComponentInChildren(); if (flowchart != null) { foreach (Say say in flowchart.GetComponentsInChildren()) @@ -314,11 +325,15 @@ namespace Fungus return; } - string flowchartName = idParts[1]; + string flowchartId = idParts[1]; + if (!flowchartDict.ContainsKey(flowchartId)) + { + return; + } + Flowchart flowchart = flowchartDict[flowchartId]; + int itemId = int.Parse(idParts[2]); - GameObject go = GameObject.Find(flowchartName); - Flowchart flowchart = go.GetComponentInChildren(); if (flowchart != null) { foreach (Menu menu in flowchart.GetComponentsInChildren())