From 0351c0f1f9275c03c91de8c02a4f8d0c41f28b23 Mon Sep 17 00:00:00 2001 From: Chris Gregan Date: Thu, 5 May 2016 18:27:37 +0100 Subject: [PATCH] All Environments option for LuaBindings Lua prefab only registers bindings with itself --- .../FungusLua/Resources/Prefabs/Lua.prefab | 2 ++ .../Scripts/Editor/LuaBindingsEditor.cs | 12 ++++++++++- .../FungusLua/Scripts/LuaBindings.cs | 20 +++++++++++++++++-- .../Thirdparty/FungusLua/Scripts/LuaStore.cs | 5 ++++- .../Thirdparty/FungusLua/Scripts/LuaUtils.cs | 6 ++---- 5 files changed, 37 insertions(+), 8 deletions(-) diff --git a/Assets/Fungus/Thirdparty/FungusLua/Resources/Prefabs/Lua.prefab b/Assets/Fungus/Thirdparty/FungusLua/Resources/Prefabs/Lua.prefab index f64868fe..bfd7cd1c 100644 --- a/Assets/Fungus/Thirdparty/FungusLua/Resources/Prefabs/Lua.prefab +++ b/Assets/Fungus/Thirdparty/FungusLua/Resources/Prefabs/Lua.prefab @@ -107,6 +107,8 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 4cc8a659e950044b69d7c62696c36962, type: 3} m_Name: m_EditorClassIdentifier: + allEnvironments: 0 + luaEnvironment: {fileID: 11483650} tableName: registerTypes: 1 boundTypes: [] diff --git a/Assets/Fungus/Thirdparty/FungusLua/Scripts/Editor/LuaBindingsEditor.cs b/Assets/Fungus/Thirdparty/FungusLua/Scripts/Editor/LuaBindingsEditor.cs index 0e9f0470..1817f201 100644 --- a/Assets/Fungus/Thirdparty/FungusLua/Scripts/Editor/LuaBindingsEditor.cs +++ b/Assets/Fungus/Thirdparty/FungusLua/Scripts/Editor/LuaBindingsEditor.cs @@ -15,8 +15,10 @@ namespace Fungus { protected ReorderableList boundObjectsList; + protected SerializedProperty allLuaEnvironmentsProp; + protected SerializedProperty luaEnvironmentProp; protected SerializedProperty tableNameProp; - protected SerializedProperty registerTypesProp; + protected SerializedProperty registerTypesProp; protected SerializedProperty boundObjectsProp; protected string bindingHelpItem = ""; @@ -24,6 +26,8 @@ namespace Fungus protected virtual void OnEnable() { + allLuaEnvironmentsProp = serializedObject.FindProperty("allEnvironments"); + luaEnvironmentProp = serializedObject.FindProperty("luaEnvironment"); tableNameProp = serializedObject.FindProperty("tableName"); registerTypesProp = serializedObject.FindProperty("registerTypes"); boundObjectsProp = serializedObject.FindProperty("boundObjects"); @@ -140,6 +144,12 @@ namespace Fungus { serializedObject.Update(); + EditorGUILayout.PropertyField(allLuaEnvironmentsProp); + if (!allLuaEnvironmentsProp.boolValue) + { + EditorGUILayout.PropertyField(luaEnvironmentProp); + } + EditorGUILayout.PropertyField(tableNameProp); EditorGUILayout.PropertyField(registerTypesProp); diff --git a/Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaBindings.cs b/Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaBindings.cs index bee26ebc..1757402c 100644 --- a/Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaBindings.cs +++ b/Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaBindings.cs @@ -19,7 +19,7 @@ namespace Fungus /// /// Add all declared bindings to the globals table. /// - public abstract void AddBindings(Table globals); + public abstract void AddBindings(LuaEnvironment luaEnvironment); } /// @@ -39,6 +39,12 @@ namespace Fungus public Component component; } + [Tooltip("Add bindings to every Lua Environment in the scene. If false, only add bindings to a specific Lua Environment.")] + public bool allEnvironments = true; + + [Tooltip("The specific LuaEnvironment to register the bindings in.")] + public LuaEnvironment luaEnvironment; + /// /// Name of global table variable to store bindings in. If left blank then each binding will be added as a global variable. /// @@ -72,8 +78,18 @@ namespace Fungus /// /// Add all declared bindings to the globals table. /// - public override void AddBindings(Table globals) + public override void AddBindings(LuaEnvironment _luaEnvironment) { + if (!allEnvironments && + luaEnvironment != _luaEnvironment) + { + // Don't add bindings to this environment + return; + } + + MoonSharp.Interpreter.Script interpreter = _luaEnvironment.Interpreter; + Table globals = interpreter.Globals; + Table bindingsTable = null; if (tableName == "") { diff --git a/Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaStore.cs b/Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaStore.cs index 02783c7c..b49f94bc 100644 --- a/Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaStore.cs +++ b/Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaStore.cs @@ -69,13 +69,16 @@ namespace Fungus /// /// Callback to bind this LuaStore component with the "unity" table in a LuaEnvironment component. /// - public override void AddBindings(Table globals) + public override void AddBindings(LuaEnvironment luaEnvironment) { if (!Init()) { return; } + MoonSharp.Interpreter.Script interpreter = luaEnvironment.Interpreter; + Table globals = interpreter.Globals; + if (globals == null) { Debug.LogError("Lua globals table is null"); diff --git a/Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaUtils.cs b/Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaUtils.cs index ab6299ca..dd6bc8ef 100644 --- a/Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaUtils.cs +++ b/Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaUtils.cs @@ -165,16 +165,14 @@ namespace Fungus } /// - /// Binds all gameobjects and components defined in scene LuaBindings to the global table. + /// Binds all gameobjects and components defined in LuaBindings components to LuaEnvironments. /// protected virtual void InitBindings() { - MoonSharp.Interpreter.Script interpreter = luaEnvironment.Interpreter; - LuaBindingsBase[] bindings = GameObject.FindObjectsOfType(); foreach (LuaBindingsBase binding in bindings) { - binding.AddBindings(interpreter.Globals); + binding.AddBindings(luaEnvironment); } }