Browse Source

All Environments option for LuaBindings

Lua prefab only registers bindings with itself
master
Chris Gregan 9 years ago
parent
commit
0351c0f1f9
  1. 2
      Assets/Fungus/Thirdparty/FungusLua/Resources/Prefabs/Lua.prefab
  2. 10
      Assets/Fungus/Thirdparty/FungusLua/Scripts/Editor/LuaBindingsEditor.cs
  3. 20
      Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaBindings.cs
  4. 5
      Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaStore.cs
  5. 6
      Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaUtils.cs

2
Assets/Fungus/Thirdparty/FungusLua/Resources/Prefabs/Lua.prefab vendored

@ -107,6 +107,8 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 4cc8a659e950044b69d7c62696c36962, type: 3} m_Script: {fileID: 11500000, guid: 4cc8a659e950044b69d7c62696c36962, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
allEnvironments: 0
luaEnvironment: {fileID: 11483650}
tableName: tableName:
registerTypes: 1 registerTypes: 1
boundTypes: [] boundTypes: []

10
Assets/Fungus/Thirdparty/FungusLua/Scripts/Editor/LuaBindingsEditor.cs vendored

@ -15,6 +15,8 @@ namespace Fungus
{ {
protected ReorderableList boundObjectsList; protected ReorderableList boundObjectsList;
protected SerializedProperty allLuaEnvironmentsProp;
protected SerializedProperty luaEnvironmentProp;
protected SerializedProperty tableNameProp; protected SerializedProperty tableNameProp;
protected SerializedProperty registerTypesProp; protected SerializedProperty registerTypesProp;
protected SerializedProperty boundObjectsProp; protected SerializedProperty boundObjectsProp;
@ -24,6 +26,8 @@ namespace Fungus
protected virtual void OnEnable() protected virtual void OnEnable()
{ {
allLuaEnvironmentsProp = serializedObject.FindProperty("allEnvironments");
luaEnvironmentProp = serializedObject.FindProperty("luaEnvironment");
tableNameProp = serializedObject.FindProperty("tableName"); tableNameProp = serializedObject.FindProperty("tableName");
registerTypesProp = serializedObject.FindProperty("registerTypes"); registerTypesProp = serializedObject.FindProperty("registerTypes");
boundObjectsProp = serializedObject.FindProperty("boundObjects"); boundObjectsProp = serializedObject.FindProperty("boundObjects");
@ -140,6 +144,12 @@ namespace Fungus
{ {
serializedObject.Update(); serializedObject.Update();
EditorGUILayout.PropertyField(allLuaEnvironmentsProp);
if (!allLuaEnvironmentsProp.boolValue)
{
EditorGUILayout.PropertyField(luaEnvironmentProp);
}
EditorGUILayout.PropertyField(tableNameProp); EditorGUILayout.PropertyField(tableNameProp);
EditorGUILayout.PropertyField(registerTypesProp); EditorGUILayout.PropertyField(registerTypesProp);

20
Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaBindings.cs vendored

@ -19,7 +19,7 @@ namespace Fungus
/// <summary> /// <summary>
/// Add all declared bindings to the globals table. /// Add all declared bindings to the globals table.
/// </summary> /// </summary>
public abstract void AddBindings(Table globals); public abstract void AddBindings(LuaEnvironment luaEnvironment);
} }
/// <summary> /// <summary>
@ -39,6 +39,12 @@ namespace Fungus
public Component component; 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;
/// <summary> /// <summary>
/// Name of global table variable to store bindings in. If left blank then each binding will be added as a global variable. /// Name of global table variable to store bindings in. If left blank then each binding will be added as a global variable.
/// </summary> /// </summary>
@ -72,8 +78,18 @@ namespace Fungus
/// <summary> /// <summary>
/// Add all declared bindings to the globals table. /// Add all declared bindings to the globals table.
/// </summary> /// </summary>
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; Table bindingsTable = null;
if (tableName == "") if (tableName == "")
{ {

5
Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaStore.cs vendored

@ -69,13 +69,16 @@ namespace Fungus
/// <summary> /// <summary>
/// Callback to bind this LuaStore component with the "unity" table in a LuaEnvironment component. /// Callback to bind this LuaStore component with the "unity" table in a LuaEnvironment component.
/// </summary> /// </summary>
public override void AddBindings(Table globals) public override void AddBindings(LuaEnvironment luaEnvironment)
{ {
if (!Init()) if (!Init())
{ {
return; return;
} }
MoonSharp.Interpreter.Script interpreter = luaEnvironment.Interpreter;
Table globals = interpreter.Globals;
if (globals == null) if (globals == null)
{ {
Debug.LogError("Lua globals table is null"); Debug.LogError("Lua globals table is null");

6
Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaUtils.cs vendored

@ -165,16 +165,14 @@ namespace Fungus
} }
/// <summary> /// <summary>
/// Binds all gameobjects and components defined in scene LuaBindings to the global table. /// Binds all gameobjects and components defined in LuaBindings components to LuaEnvironments.
/// </summary> /// </summary>
protected virtual void InitBindings() protected virtual void InitBindings()
{ {
MoonSharp.Interpreter.Script interpreter = luaEnvironment.Interpreter;
LuaBindingsBase[] bindings = GameObject.FindObjectsOfType<LuaBindingsBase>(); LuaBindingsBase[] bindings = GameObject.FindObjectsOfType<LuaBindingsBase>();
foreach (LuaBindingsBase binding in bindings) foreach (LuaBindingsBase binding in bindings)
{ {
binding.AddBindings(interpreter.Globals); binding.AddBindings(luaEnvironment);
} }
} }

Loading…
Cancel
Save