From e9aa381f20a17a752c3ef1a1e22e7ad5d4577355 Mon Sep 17 00:00:00 2001 From: Christopher Date: Tue, 1 Nov 2016 12:11:18 +0000 Subject: [PATCH] =?UTF-8?q?Fixed=20Fungus=20ignores=20existing=20=E2=80=9C?= =?UTF-8?q?SayDialog=E2=80=9D=20and=20creates=20another=20one=20#555?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Fungus/Scripts/Components/SayDialog.cs | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/Assets/Fungus/Scripts/Components/SayDialog.cs b/Assets/Fungus/Scripts/Components/SayDialog.cs index bea77588..548ff5f8 100644 --- a/Assets/Fungus/Scripts/Components/SayDialog.cs +++ b/Assets/Fungus/Scripts/Components/SayDialog.cs @@ -5,6 +5,7 @@ using UnityEngine; using UnityEngine.UI; using System; using System.Collections; +using System.Collections.Generic; namespace Fungus { @@ -54,7 +55,23 @@ namespace Fungus protected StringSubstituter stringSubstituter = new StringSubstituter(); - protected Writer GetWriter() + // Cache active Say Dialogs to avoid expensive scene search + protected static List activeSayDialogs = new List(); + + protected void Awake() + { + if (!activeSayDialogs.Contains(this)) + { + activeSayDialogs.Add(this); + } + } + + protected void OnDestroy() + { + activeSayDialogs.Remove(this); + } + + protected Writer GetWriter() { if (writer != null) { @@ -200,8 +217,14 @@ namespace Fungus { if (ActiveSayDialog == null) { - // Use first Say Dialog found in the scene (if any) - SayDialog sd = GameObject.FindObjectOfType(); + SayDialog sd = null; + + // Use first active Say Dialog in the scene (if any) + if (activeSayDialogs.Count > 0) + { + sd = activeSayDialogs[0]; + } + if (sd != null) { ActiveSayDialog = sd;