From a6ea00536b7ef3c63d5fe500cd8c01b12f736371 Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Fri, 1 Apr 2016 16:10:28 +0100 Subject: [PATCH] Added abstract Initialiser class for extending initialisation behavior --- Assets/Fungus/Lua/Scripts/LuaEnvironment.cs | 21 ++++++++++++++++----- Assets/Fungus/Lua/Scripts/LuaUtils.cs | 7 +++++-- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/Assets/Fungus/Lua/Scripts/LuaEnvironment.cs b/Assets/Fungus/Lua/Scripts/LuaEnvironment.cs index b33eaa28..1ef4f62e 100644 --- a/Assets/Fungus/Lua/Scripts/LuaEnvironment.cs +++ b/Assets/Fungus/Lua/Scripts/LuaEnvironment.cs @@ -12,9 +12,19 @@ using MoonSharp.RemoteDebugger; namespace Fungus { - public class LuaEnvironment : MonoBehaviour { + /// + /// Helper class used to extend the initialization behavior of LuaEnvironment. + /// + public abstract class Initializer : MonoBehaviour + { + /// + /// Called when the LuaEnvironment is initializing. + /// + public abstract void Initialize(); + } + /// /// Custom file loader for MoonSharp that loads in all Lua scripts in the project. /// Scripts must be placed in a Resources/Lua directory. @@ -127,13 +137,14 @@ namespace Fungus // You can restrict which core lua modules are available here if needed. See the MoonSharp documentation for details. interpreter = new Script(CoreModules.Preset_Complete); + // Load all Lua scripts in the project InitLuaScriptFiles(); - // TODO: Use an interface here instead - LuaUtils luaUtils = GetComponent(); - if (luaUtils != null) + // Initialize any attached initializer components (e.g. LuaUtils) + Initializer[] initializers = GetComponentsInChildren(); + foreach (Initializer initializer in initializers) { - luaUtils.Initialise(); + initializer.Initialize(); } if (remoteDebugger) diff --git a/Assets/Fungus/Lua/Scripts/LuaUtils.cs b/Assets/Fungus/Lua/Scripts/LuaUtils.cs index 8fcdb5cd..1465f391 100644 --- a/Assets/Fungus/Lua/Scripts/LuaUtils.cs +++ b/Assets/Fungus/Lua/Scripts/LuaUtils.cs @@ -13,7 +13,7 @@ using MoonSharp.RemoteDebugger; namespace Fungus { - public class LuaUtils : MonoBehaviour + public class LuaUtils : LuaEnvironment.Initializer { /// /// Lua script file which defines the global string table used for localisation. @@ -57,7 +57,10 @@ namespace Fungus /// protected LuaEnvironment luaEnvironment; - public virtual void Initialise() + /// + /// Called by LuaEnvironment when initializing. + /// + public override void Initialize() { luaEnvironment = GetComponent(); if (luaEnvironment == null)