Browse Source

Added abstract Initialiser class for extending initialisation behavior

master
chrisgregan 9 years ago
parent
commit
a6ea00536b
  1. 21
      Assets/Fungus/Lua/Scripts/LuaEnvironment.cs
  2. 7
      Assets/Fungus/Lua/Scripts/LuaUtils.cs

21
Assets/Fungus/Lua/Scripts/LuaEnvironment.cs

@ -12,9 +12,19 @@ using MoonSharp.RemoteDebugger;
namespace Fungus namespace Fungus
{ {
public class LuaEnvironment : MonoBehaviour public class LuaEnvironment : MonoBehaviour
{ {
/// <summary>
/// Helper class used to extend the initialization behavior of LuaEnvironment.
/// </summary>
public abstract class Initializer : MonoBehaviour
{
/// <summary>
/// Called when the LuaEnvironment is initializing.
/// </summary>
public abstract void Initialize();
}
/// <summary> /// <summary>
/// Custom file loader for MoonSharp that loads in all Lua scripts in the project. /// Custom file loader for MoonSharp that loads in all Lua scripts in the project.
/// Scripts must be placed in a Resources/Lua directory. /// 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. // You can restrict which core lua modules are available here if needed. See the MoonSharp documentation for details.
interpreter = new Script(CoreModules.Preset_Complete); interpreter = new Script(CoreModules.Preset_Complete);
// Load all Lua scripts in the project
InitLuaScriptFiles(); InitLuaScriptFiles();
// TODO: Use an interface here instead // Initialize any attached initializer components (e.g. LuaUtils)
LuaUtils luaUtils = GetComponent<LuaUtils>(); Initializer[] initializers = GetComponentsInChildren<Initializer>();
if (luaUtils != null) foreach (Initializer initializer in initializers)
{ {
luaUtils.Initialise(); initializer.Initialize();
} }
if (remoteDebugger) if (remoteDebugger)

7
Assets/Fungus/Lua/Scripts/LuaUtils.cs

@ -13,7 +13,7 @@ using MoonSharp.RemoteDebugger;
namespace Fungus namespace Fungus
{ {
public class LuaUtils : MonoBehaviour public class LuaUtils : LuaEnvironment.Initializer
{ {
/// <summary> /// <summary>
/// Lua script file which defines the global string table used for localisation. /// Lua script file which defines the global string table used for localisation.
@ -57,7 +57,10 @@ namespace Fungus
/// </summary> /// </summary>
protected LuaEnvironment luaEnvironment; protected LuaEnvironment luaEnvironment;
public virtual void Initialise() /// <summary>
/// Called by LuaEnvironment when initializing.
/// </summary>
public override void Initialize()
{ {
luaEnvironment = GetComponent<LuaEnvironment>(); luaEnvironment = GetComponent<LuaEnvironment>();
if (luaEnvironment == null) if (luaEnvironment == null)

Loading…
Cancel
Save