diff --git a/Assets/Fungus/Scripts/Components/Flowchart.cs b/Assets/Fungus/Scripts/Components/Flowchart.cs
index dd717230..753b8221 100644
--- a/Assets/Fungus/Scripts/Components/Flowchart.cs
+++ b/Assets/Fungus/Scripts/Components/Flowchart.cs
@@ -147,6 +147,15 @@ namespace Fungus
CheckItemIds();
CleanupComponents();
UpdateVersion();
+
+ StringSubstituter.RegisterHandler(this);
+ }
+
+ protected virtual void OnDisable()
+ {
+ cachedFlowcharts.Remove(this);
+
+ StringSubstituter.UnregisterHandler(this);
}
protected virtual void UpdateVersion()
@@ -172,11 +181,6 @@ namespace Fungus
version = FungusConstants.CurrentVersion;
}
- protected virtual void OnDisable()
- {
- cachedFlowcharts.Remove(this);
- }
-
protected virtual void CheckItemIds()
{
// Make sure item ids are unique and monotonically increasing.
@@ -1181,7 +1185,6 @@ namespace Fungus
if (stringSubstituer == null)
{
stringSubstituer = new StringSubstituter();
- stringSubstituer.CacheSubstitutionHandlers();
}
// Use the string builder from StringSubstituter for efficiency.
diff --git a/Assets/Fungus/Scripts/Components/Localization.cs b/Assets/Fungus/Scripts/Components/Localization.cs
index 604e107b..0878cc12 100644
--- a/Assets/Fungus/Scripts/Components/Localization.cs
+++ b/Assets/Fungus/Scripts/Components/Localization.cs
@@ -65,6 +65,16 @@ namespace Fungus
}
}
+ protected virtual void OnEnable()
+ {
+ StringSubstituter.RegisterHandler(this);
+ }
+
+ protected virtual void OnDisable()
+ {
+ StringSubstituter.UnregisterHandler(this);
+ }
+
protected virtual void Start()
{
Init();
diff --git a/Assets/Fungus/Scripts/Components/SayDialog.cs b/Assets/Fungus/Scripts/Components/SayDialog.cs
index 2aee53cd..ef47c28b 100644
--- a/Assets/Fungus/Scripts/Components/SayDialog.cs
+++ b/Assets/Fungus/Scripts/Components/SayDialog.cs
@@ -149,14 +149,6 @@ namespace Fungus
}
}
- protected virtual void OnEnable()
- {
- // We need to update the cached list every time the Say Dialog is enabled
- // due to an initialization order issue after loading scenes.
- stringSubstituter.CacheSubstitutionHandlers();
- }
-
-
protected virtual void LateUpdate()
{
UpdateAlpha();
diff --git a/Assets/Fungus/Thirdparty/FungusLua/Scripts/Components/LuaUtils.cs b/Assets/Fungus/Thirdparty/FungusLua/Scripts/Components/LuaUtils.cs
index 07262ff5..4396c024 100644
--- a/Assets/Fungus/Thirdparty/FungusLua/Scripts/Components/LuaUtils.cs
+++ b/Assets/Fungus/Thirdparty/FungusLua/Scripts/Components/LuaUtils.cs
@@ -68,6 +68,16 @@ namespace Fungus
protected ConversationManager conversationManager;
#endif
+ protected virtual void OnEnable()
+ {
+ StringSubstituter.RegisterHandler(this);
+ }
+
+ protected virtual void OnDisable()
+ {
+ StringSubstituter.UnregisterHandler(this);
+ }
+
///
/// Registers all listed c# types for interop with Lua.
/// You can also register types directly in the Awake method of any
@@ -245,7 +255,6 @@ namespace Fungus
}
stringSubstituter = new StringSubstituter();
- stringSubstituter.CacheSubstitutionHandlers();
#if !FUNGUSLUA_STANDALONE
conversationManager = new ConversationManager();
diff --git a/Assets/Fungus/Thirdparty/FungusLua/Scripts/Interfaces/IStringSubstituter.cs b/Assets/Fungus/Thirdparty/FungusLua/Scripts/Interfaces/IStringSubstituter.cs
index 696b4a79..6080f70d 100644
--- a/Assets/Fungus/Thirdparty/FungusLua/Scripts/Interfaces/IStringSubstituter.cs
+++ b/Assets/Fungus/Thirdparty/FungusLua/Scripts/Interfaces/IStringSubstituter.cs
@@ -16,11 +16,6 @@ namespace Fungus
///
StringBuilder _StringBuilder { get; }
- ///
- /// Populates a cache of all components in the scene that implement ISubstitutionHandler.
- ///
- void CacheSubstitutionHandlers();
-
///
/// Returns a new string that has been processed by all substitution handlers in the scene.
///
diff --git a/Assets/Fungus/Thirdparty/FungusLua/Scripts/Utils/StringSubstituter.cs b/Assets/Fungus/Thirdparty/FungusLua/Scripts/Utils/StringSubstituter.cs
index 597af240..1049ab96 100644
--- a/Assets/Fungus/Thirdparty/FungusLua/Scripts/Utils/StringSubstituter.cs
+++ b/Assets/Fungus/Thirdparty/FungusLua/Scripts/Utils/StringSubstituter.cs
@@ -16,7 +16,7 @@ namespace Fungus
///
public class StringSubstituter : IStringSubstituter
{
- protected List substitutionHandlers = new List();
+ protected static List substitutionHandlers = new List();
///
/// The StringBuilder instance used to substitute strings optimally.
@@ -27,6 +27,19 @@ namespace Fungus
#region Public members
+ public static void RegisterHandler(ISubstitutionHandler handler)
+ {
+ if (!substitutionHandlers.Contains(handler))
+ {
+ substitutionHandlers.Add(handler);
+ }
+ }
+
+ public static void UnregisterHandler(ISubstitutionHandler handler)
+ {
+ substitutionHandlers.Remove(handler);
+ }
+
///
/// Constructor which caches all components in the scene that implement ISubstitutionHandler.
/// Number of levels of recursively embedded keys to resolve.
@@ -43,34 +56,6 @@ namespace Fungus
public virtual StringBuilder _StringBuilder { get { return stringBuilder; } }
- public virtual void CacheSubstitutionHandlers()
- {
- // Use reflection to find all components in the scene that implement ISubstitutionHandler
-#if NETFX_CORE
- var types = this.GetType().GetAssembly().GetTypes().Where(type => type.IsClass() &&
- !type.IsAbstract() &&
- typeof(ISubstitutionHandler).IsAssignableFrom(type));
-#else
- var types = this.GetType().Assembly.GetTypes().Where(type => type.IsClass &&
- !type.IsAbstract &&
- typeof(ISubstitutionHandler).IsAssignableFrom(type));
-#endif
-
- substitutionHandlers.Clear();
- foreach (System.Type t in types)
- {
- Object[] objects = GameObject.FindObjectsOfType(t);
- foreach (Object o in objects)
- {
- ISubstitutionHandler handler = o as ISubstitutionHandler;
- if (handler != null)
- {
- substitutionHandlers.Add(handler);
- }
- }
- }
- }
-
public virtual string SubstituteStrings(string input)
{
stringBuilder.Length = 0;