Browse Source

Merge pull request #436 from FungusGames/lua-store

Added LuaStore component and prefab to persist values across scene loads
master
Chris Gregan 9 years ago
parent
commit
ecf999807a
  1. 9
      Assets/Fungus/Lua/Resources/Lua/fungus.txt
  2. 52
      Assets/Fungus/Lua/Resources/Prefabs/LuaStore.prefab
  3. 8
      Assets/Fungus/Lua/Resources/Prefabs/LuaStore.prefab.meta
  4. 28
      Assets/Fungus/Lua/Scripts/Editor/LuaStoreEditor.cs
  5. 12
      Assets/Fungus/Lua/Scripts/Editor/LuaStoreEditor.cs.meta
  6. 16
      Assets/Fungus/Lua/Scripts/Editor/MenuItems.cs
  7. 96
      Assets/Fungus/Lua/Scripts/LuaStore.cs
  8. 12
      Assets/Fungus/Lua/Scripts/LuaStore.cs.meta
  9. 2
      Assets/Fungus/Lua/Scripts/LuaUtils.cs
  10. 285
      Assets/Tests/Lua/LuaEnvironmentTests.unity
  11. 2
      ProjectSettings/EditorBuildSettings.asset

9
Assets/Fungus/Lua/Resources/Lua/fungus.txt

@ -134,9 +134,16 @@ end
-- Fungus Prefs
---------------
-- Handy short alias to the real prefs object
-- Handy alias to the real prefs object
M.prefs = unity.prefs
---------------
-- Lua Store
---------------
-- Handy alias to the real store object
M.store = unity.store
-------------
-- Say Dialog
-------------

52
Assets/Fungus/Lua/Resources/Prefabs/LuaStore.prefab

@ -0,0 +1,52 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &167328
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
serializedVersion: 4
m_Component:
- 4: {fileID: 446178}
- 114: {fileID: 11480748}
m_Layer: 0
m_Name: LuaStore
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &446178
Transform:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 167328}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -3.8587983, y: -2.6257849, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
--- !u!114 &11480748
MonoBehaviour:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 167328}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 2136bcefabdd04152be23647a2ce8cf7, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1001 &100100000
Prefab:
m_ObjectHideFlags: 1
serializedVersion: 2
m_Modification:
m_TransformParent: {fileID: 0}
m_Modifications: []
m_RemovedComponents: []
m_ParentPrefab: {fileID: 0}
m_RootGameObject: {fileID: 167328}
m_IsPrefabParent: 1

8
Assets/Fungus/Lua/Resources/Prefabs/LuaStore.prefab.meta

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 48405e3dbda9a4a7694c21c9a2e712e1
timeCreated: 1459763006
licenseType: Free
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:

28
Assets/Fungus/Lua/Scripts/Editor/LuaStoreEditor.cs

@ -0,0 +1,28 @@
using UnityEngine;
using UnityEditor;
using System.Collections;
using MoonSharp.Interpreter;
using MoonSharp.Interpreter.Serialization;
namespace Fungus
{
[CustomEditor(typeof(LuaStore))]
public class LuaStoreEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
// Use the Serialization extension to display the contents of the prime table.
LuaStore luaStore = target as LuaStore;
if (luaStore.primeTable != null)
{
EditorGUILayout.PrefixLabel(new GUIContent("Inspect Table", "Displays the contents of the fungus.store prime table."));
string serialized = luaStore.primeTable.Serialize();
EditorGUILayout.SelectableLabel(serialized, GUILayout.ExpandHeight(true));
}
}
}
}

12
Assets/Fungus/Lua/Scripts/Editor/LuaStoreEditor.cs.meta

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 8032e344e5a584a388d80ec481e00b97
timeCreated: 1459763437
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

16
Assets/Fungus/Lua/Scripts/Editor/MenuItems.cs

@ -20,11 +20,17 @@ namespace Fungus
SpawnPrefab("Prefabs/LuaBindings", false);
}
[MenuItem("Tools/Fungus/Create/Lua Script", false, 2002)]
static void CreateFungusInvoke()
{
SpawnPrefab("Prefabs/LuaScript", false);
}
[MenuItem("Tools/Fungus/Create/Lua Script", false, 2002)]
static void CreateLuaScript()
{
SpawnPrefab("Prefabs/LuaScript", false);
}
[MenuItem("Tools/Fungus/Create/Lua Store", false, 2003)]
static void CreateLuaStore()
{
SpawnPrefab("Prefabs/LuaStore", false);
}
[MenuItem("Tools/Fungus/Create/Lua File", false, 2100)]
static void CreateLuaFile()

96
Assets/Fungus/Lua/Scripts/LuaStore.cs

@ -0,0 +1,96 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using MoonSharp.Interpreter;
using MoonSharp.Interpreter.Serialization;
namespace Fungus
{
/// <summary>
/// Wrapper for a prime Lua table that persists across scene loads.
/// This is useful for transferring values from one scene to another. One one LuaStore component may exist
/// in a scene at a time.
/// </summary>
public class LuaStore : LuaBindingsBase
{
/// <summary>
/// A Lua table that can be shared between multiple LuaEnvironments.
/// </summary>
public Table primeTable;
protected bool initialized;
protected static LuaStore instance;
public void Start()
{
Init();
}
/// <summary>
/// Initialize the LuaStore component.
/// This component behaves somewhat like a singleton in that only one instance
/// is permitted in the application which persists until shutdown.
/// </summary>
protected virtual bool Init()
{
if (initialized)
{
return true;
}
if (instance == null)
{
// This is the first instance of the LuaStore, so store a static reference to it.
instance = this;
}
else if (instance != this)
{
// This is an extra instance of LuaStore. We only need one in the scene, so delete this one.
Destroy(gameObject);
return false;
}
// We're now guaranteed that this instance of LuaStore is the first and only instance.
primeTable = DynValue.NewPrimeTable().Table;
// DontDestroyOnLoad only works for root objects
transform.parent = null;
DontDestroyOnLoad(this);
initialized = true;
return true;
}
/// <summary>
/// Callback to bind this LuaStore component with the "unity" table in a LuaEnvironment component.
/// </summary>
public override void AddBindings(Table globals)
{
if (!Init())
{
return;
}
if (globals == null)
{
Debug.LogError("Lua globals table is null");
return;
}
Table unityTable = globals.Get("unity").Table;
if (unityTable == null)
{
Debug.LogError("Unity table not found");
return;
}
unityTable["store"] = primeTable;
}
}
}

12
Assets/Fungus/Lua/Scripts/LuaStore.cs.meta

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 2136bcefabdd04152be23647a2ce8cf7
timeCreated: 1459761254
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

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

@ -76,8 +76,8 @@ namespace Fungus
}
InitTypes();
InitBindings();
InitCustomObjects();
InitBindings();
InitStringTable();
}

285
Assets/Tests/Lua/LuaEnvironmentTests.unity

@ -247,6 +247,58 @@ MonoBehaviour:
Transform:
m_PrefabParentObject: {fileID: 495584, guid: 49031c561e16d4fcf91c12153f8e0b25, type: 2}
m_PrefabInternal: {fileID: 32811161}
--- !u!1 &256438762
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 4
m_Component:
- 4: {fileID: 256438764}
- 114: {fileID: 256438763}
m_Layer: 0
m_Name: StoreTest
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &256438763
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 256438762}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: b1dba0b27b0864740a8720e920aa88c0, type: 3}
m_Name:
m_EditorClassIdentifier:
timeout: 5
ignored: 0
succeedAfterAllAssertionsAreExecuted: 1
expectException: 0
expectedExceptionList:
succeedWhenExceptionIsThrown: 0
includedPlatforms: -1
platformsToIgnore: []
dynamic: 0
dynamicTypeName:
--- !u!4 &256438764
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 256438762}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:
- {fileID: 1258610947}
- {fileID: 1828947836}
- {fileID: 1463630763}
m_Father: {fileID: 0}
m_RootOrder: 11
--- !u!1 &304626605
GameObject:
m_ObjectHideFlags: 0
@ -921,6 +973,46 @@ Transform:
- {fileID: 113453956}
m_Father: {fileID: 0}
m_RootOrder: 10
--- !u!1 &1258610945
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 167328, guid: 48405e3dbda9a4a7694c21c9a2e712e1, type: 2}
m_PrefabInternal: {fileID: 0}
serializedVersion: 4
m_Component:
- 4: {fileID: 1258610947}
- 114: {fileID: 1258610946}
m_Layer: 0
m_Name: LuaStore
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1258610946
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 11480748, guid: 48405e3dbda9a4a7694c21c9a2e712e1,
type: 2}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1258610945}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 2136bcefabdd04152be23647a2ce8cf7, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!4 &1258610947
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 446178, guid: 48405e3dbda9a4a7694c21c9a2e712e1, type: 2}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1258610945}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 256438764}
m_RootOrder: 0
--- !u!4 &1272779136 stripped
Transform:
m_PrefabParentObject: {fileID: 495584, guid: 49031c561e16d4fcf91c12153f8e0b25, type: 2}
@ -1067,6 +1159,65 @@ Transform:
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
--- !u!1 &1463630762
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 100640, guid: 49031c561e16d4fcf91c12153f8e0b25, type: 2}
m_PrefabInternal: {fileID: 0}
serializedVersion: 4
m_Component:
- 4: {fileID: 1463630763}
- 114: {fileID: 1463630765}
- 114: {fileID: 1463630764}
m_Layer: 0
m_Name: LuaEnvironment
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1463630763
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 495584, guid: 49031c561e16d4fcf91c12153f8e0b25, type: 2}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1463630762}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 256438764}
m_RootOrder: 2
--- !u!114 &1463630764
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 11486636, guid: 49031c561e16d4fcf91c12153f8e0b25,
type: 2}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1463630762}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: c10f0b861365b42b0928858f7b086ff3, type: 3}
m_Name:
m_EditorClassIdentifier:
stringTable: {fileID: 0}
activeLanguage: en
timeScale: -1
registerTypes:
- {fileID: 4900000, guid: 9c3ab7a98d51241bbb499643399fa761, type: 3}
--- !u!114 &1463630765
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 11493126, guid: 49031c561e16d4fcf91c12153f8e0b25,
type: 2}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1463630762}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: ba19c26c1ba7243d2b57ebc4329cc7c6, type: 3}
m_Name:
m_EditorClassIdentifier:
remoteDebugger: 0
--- !u!4 &1500823905 stripped
Transform:
m_PrefabParentObject: {fileID: 403334, guid: e0c2b90c058ff43f4a56a266d4fa721b, type: 2}
@ -1132,7 +1283,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!114 &1532103953
MonoBehaviour:
m_ObjectHideFlags: 0
@ -1195,7 +1346,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 61dddfdc5e0e44ca298d8f46f7f5a915, type: 3}
m_Name:
m_EditorClassIdentifier:
selectedFlowchart: {fileID: 0}
selectedFlowchart: {fileID: 1828947832}
--- !u!4 &1549270606
Transform:
m_ObjectHideFlags: 1
@ -1436,6 +1587,136 @@ MonoBehaviour:
'
runAsCoroutine: 1
useFungusModule: 1
--- !u!1 &1828947831
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 142980, guid: 5e7fbc8d4eb714b279eeeef2262c1e1a, type: 2}
m_PrefabInternal: {fileID: 0}
serializedVersion: 4
m_Component:
- 4: {fileID: 1828947836}
- 114: {fileID: 1828947832}
- 114: {fileID: 1828947834}
- 114: {fileID: 1828947835}
- 114: {fileID: 1828947833}
m_Layer: 0
m_Name: Flowchart
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1828947832
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 11430050, guid: 5e7fbc8d4eb714b279eeeef2262c1e1a,
type: 2}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1828947831}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 7a334fe2ffb574b3583ff3b18b4792d3, type: 3}
m_Name:
m_EditorClassIdentifier:
version: 1
scrollPos: {x: 0, y: 0}
variablesScrollPos: {x: 0, y: 0}
variablesExpanded: 1
blockViewHeight: 400
zoom: 1
scrollViewRect:
serializedVersion: 2
x: -343
y: -340
width: 1114
height: 859
selectedBlock: {fileID: 1828947834}
selectedCommands: []
variables: []
description: "Check if the LuaStore prime table is created \nand registered as fungus.store,
and if we can write and read values to it."
stepPause: 0
colorCommands: 1
hideComponents: 1
saveSelection: 1
localizationId:
showLineNumbers: 0
hideCommands: []
--- !u!114 &1828947833
MonoBehaviour:
m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1828947831}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 71f455683d4ba4405b8dbba457159620, type: 3}
m_Name:
m_EditorClassIdentifier:
itemId: 1
errorMessage:
indentLevel: 0
luaEnvironment: {fileID: 0}
luaScript: 'fungus.store.test = "ok"
fungus.assert(fungus.store.test == "ok")
fungus.pass()'
useFungusModule: 1
runAsCoroutine: 1
waitUntilFinished: 1
returnVariable: {fileID: 0}
--- !u!114 &1828947834
MonoBehaviour:
m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 11433304, guid: 5e7fbc8d4eb714b279eeeef2262c1e1a,
type: 2}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1828947831}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 3d3d73aef2cfc4f51abf34ac00241f60, type: 3}
m_Name:
m_EditorClassIdentifier:
nodeRect:
serializedVersion: 2
x: 67
y: 69
width: 120
height: 40
itemId: 0
blockName: Start
description:
eventHandler: {fileID: 1828947835}
commandList:
- {fileID: 1828947833}
--- !u!114 &1828947835
MonoBehaviour:
m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 11462346, guid: 5e7fbc8d4eb714b279eeeef2262c1e1a,
type: 2}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1828947831}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: d2f6487d21a03404cb21b245f0242e79, type: 3}
m_Name:
m_EditorClassIdentifier:
parentBlock: {fileID: 1828947834}
--- !u!4 &1828947836
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 467082, guid: 5e7fbc8d4eb714b279eeeef2262c1e1a, type: 2}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1828947831}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 256438764}
m_RootOrder: 1
--- !u!1001 &1855619325
Prefab:
m_ObjectHideFlags: 0

2
ProjectSettings/EditorBuildSettings.asset

@ -7,3 +7,5 @@ EditorBuildSettings:
m_Scenes:
- enabled: 1
path: Assets/FungusExamples/Sherlock/TheExperiment.unity
- enabled: 1
path: Assets/Tests/Lua/SceneB.unity

Loading…
Cancel
Save