Browse Source

Contents of Lua Script string appended to Lua File contents

master
chrisgregan 9 years ago
parent
commit
6e4e392375
  1. 108
      Assets/Fungus/FungusLua/Scripts/Editor/LuaScriptEditor.cs
  2. 12
      Assets/Fungus/FungusLua/Scripts/Editor/LuaScriptEditor.cs.meta
  3. 14
      Assets/Fungus/FungusLua/Scripts/LuaScript.cs
  4. 183
      Assets/Tests/Lua/LuaEnvironmentTests.unity
  5. 2
      Assets/Tests/TestAssets/Resources/Lua/testlua.txt
  6. 8
      Assets/Tests/TestAssets/Resources/Lua/testlua.txt.meta

108
Assets/Fungus/FungusLua/Scripts/Editor/LuaScriptEditor.cs

@ -1,108 +0,0 @@
// Adapted from the Unity Test Tools project (MIT license)
// https://bitbucket.org/Unity-Technologies/unitytesttools/src/a30d562427e9/Assets/UnityTestTools/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;
namespace Fungus
{
[CustomEditor(typeof(LuaScript))]
public class LuaScriptEditor : Editor
{
protected SerializedProperty luaEnvironmentProp;
protected SerializedProperty runAsCoroutineProp;
protected SerializedProperty luaFileProp;
protected SerializedProperty luaScriptProp;
protected List<TextAsset> luaFiles = new List<TextAsset>();
public virtual void OnEnable()
{
luaEnvironmentProp = serializedObject.FindProperty("luaEnvironment");
runAsCoroutineProp = serializedObject.FindProperty("runAsCoroutine");
luaFileProp = serializedObject.FindProperty("luaFile");
luaScriptProp = serializedObject.FindProperty("luaScript");
// Make a note of all Lua files in the project resources
object[] result = Resources.LoadAll("Lua", typeof(TextAsset));
luaFiles.Clear();
foreach (object res in result)
{
TextAsset ta = res as TextAsset;
// Ignore the built-in modules as you'll never want to execute these
if (ta == null ||
ta.name == "fungus" ||
ta.name == "inspect")
{
continue;
}
luaFiles.Add(ta);
}
}
public override void OnInspectorGUI()
{
serializedObject.Update();
EditorGUILayout.PropertyField(luaEnvironmentProp);
EditorGUILayout.PropertyField(runAsCoroutineProp);
int selected = 0;
List<GUIContent> options = new List<GUIContent>();
options.Add(new GUIContent("<Lua Script>"));
int index = 1;
if (luaFiles != null)
{
foreach (TextAsset textAsset in luaFiles)
{
options.Add(new GUIContent(textAsset.name));
if (luaFileProp.objectReferenceValue == textAsset)
{
selected = index;
}
index++;
}
}
selected = EditorGUILayout.Popup(new GUIContent("Execute Lua", "Lua file or script to execute."), selected, options.ToArray());
if (selected == 0)
{
luaFileProp.objectReferenceValue = null;
}
else
{
luaFileProp.objectReferenceValue = luaFiles[selected - 1];
}
if (luaFileProp.objectReferenceValue == null)
{
EditorGUILayout.PropertyField(luaScriptProp);
}
else
{
EditorGUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
if (GUILayout.Button(new GUIContent("Open in Editor", "Open this Lua file in the external text editor.")))
{
string path = AssetDatabase.GetAssetPath(luaFileProp.objectReferenceValue);
UnityEditorInternal.InternalEditorUtility.OpenFileAtLineExternal(path, 0);
}
GUILayout.FlexibleSpace();
EditorGUILayout.EndHorizontal();
}
serializedObject.ApplyModifiedProperties();
}
}
}

12
Assets/Fungus/FungusLua/Scripts/Editor/LuaScriptEditor.cs.meta

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

14
Assets/Fungus/FungusLua/Scripts/LuaScript.cs

@ -23,15 +23,15 @@ namespace Fungus
public LuaEnvironment luaEnvironment;
/// <summary>
/// Lua script file to execute.
/// Text file containing Lua script to be executed.
/// </summary>
[Tooltip("Lua script file to execute.")]
[Tooltip("Text file containing Lua script to be executed.")]
public TextAsset luaFile;
/// <summary>
/// Lua script to execute.
/// </summary>
[Tooltip("Lua script to execute.")]
[Tooltip("A Lua string to execute, appended to the contents of Lua File (if one is specified).")]
[TextArea(5, 50)]
public string luaScript = "";
@ -103,7 +103,8 @@ namespace Fungus
}
/// <summary>
/// Returns the Lua string to be executed.
/// Returns the Lua string to be executed.
/// This is the contents of the Lua script appended to the contents of the Lua file.
/// </summary>
/// <returns>The lua string.</returns>
protected virtual string GetLuaString()
@ -113,9 +114,10 @@ namespace Fungus
{
s = luaFile.text;
}
else if (luaScript.Length > 0)
if (luaScript.Length > 0)
{
s = luaScript;
s += luaScript;
}
return s;

183
Assets/Tests/Lua/LuaEnvironmentTests.unity

@ -320,6 +320,75 @@ MonoBehaviour:
repeatEveryFrame: 1
hasFailed: 0
executeMethods: 2
--- !u!1 &159008372
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 139298, guid: c356764ac08ce4af2806a601a4f1e6e9, type: 2}
m_PrefabInternal: {fileID: 0}
serializedVersion: 4
m_Component:
- 4: {fileID: 159008373}
- 114: {fileID: 159008375}
- 114: {fileID: 159008374}
m_Layer: 0
m_Name: LuaScript
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &159008373
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 449874, guid: c356764ac08ce4af2806a601a4f1e6e9, type: 2}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 159008372}
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: 1575922007}
m_RootOrder: 0
--- !u!114 &159008374
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 11472914, guid: c356764ac08ce4af2806a601a4f1e6e9,
type: 2}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 159008372}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 446caeace65234baaacd52095d24f101, type: 3}
m_Name:
m_EditorClassIdentifier:
luaEnvironment: {fileID: 0}
luaFile: {fileID: 4900000, guid: 0046c396ec84246688b1057076138fdc, type: 3}
luaScript: '-- Check that the testlua file has set a local variable correctly
check(v==5)
pass()'
runAsCoroutine: 1
--- !u!114 &159008375
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 11446228, guid: c356764ac08ce4af2806a601a4f1e6e9,
type: 2}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 159008372}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 6ee79785811ba49399c1b56d7309e3df, type: 3}
m_Name:
m_EditorClassIdentifier:
executeAfterTime: 1
repeatExecuteTime: 1
repeatEveryTime: 1
executeAfterFrames: 1
repeatExecuteFrame: 1
repeatEveryFrame: 1
hasFailed: 0
executeMethods: 2
--- !u!4 &236999594 stripped
Transform:
m_PrefabParentObject: {fileID: 495584, guid: 49031c561e16d4fcf91c12153f8e0b25, type: 2}
@ -1067,6 +1136,67 @@ Prefab:
m_RemovedComponents: []
m_ParentPrefab: {fileID: 100100000, guid: 49031c561e16d4fcf91c12153f8e0b25, type: 2}
m_IsPrefabParent: 0
--- !u!1 &1073989294
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 100640, guid: 49031c561e16d4fcf91c12153f8e0b25, type: 2}
m_PrefabInternal: {fileID: 0}
serializedVersion: 4
m_Component:
- 4: {fileID: 1073989295}
- 114: {fileID: 1073989297}
- 114: {fileID: 1073989296}
m_Layer: 0
m_Name: LuaEnvironment
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1073989295
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 495584, guid: 49031c561e16d4fcf91c12153f8e0b25, type: 2}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1073989294}
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: 1575922007}
m_RootOrder: 1
--- !u!114 &1073989296
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 11486636, guid: 49031c561e16d4fcf91c12153f8e0b25,
type: 2}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1073989294}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: c10f0b861365b42b0928858f7b086ff3, type: 3}
m_Name:
m_EditorClassIdentifier:
fungusModule: 0
stringTable: {fileID: 0}
activeLanguage: en
timeScale: -1
registerTypes:
- {fileID: 4900000, guid: 9c3ab7a98d51241bbb499643399fa761, type: 3}
- {fileID: 4900000, guid: 93fddea8208764a2dbb189cc238aed40, type: 3}
--- !u!114 &1073989297
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 11493126, guid: 49031c561e16d4fcf91c12153f8e0b25,
type: 2}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1073989294}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: ba19c26c1ba7243d2b57ebc4329cc7c6, type: 3}
m_Name:
m_EditorClassIdentifier:
remoteDebugger: 0
--- !u!1 &1124932171
GameObject:
m_ObjectHideFlags: 0
@ -1682,6 +1812,57 @@ Transform:
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 8
--- !u!1 &1575922005
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 4
m_Component:
- 4: {fileID: 1575922007}
- 114: {fileID: 1575922006}
m_Layer: 0
m_Name: LuaScriptTest
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
--- !u!114 &1575922006
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1575922005}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: b1dba0b27b0864740a8720e920aa88c0, type: 3}
m_Name:
m_EditorClassIdentifier:
timeout: 5
ignored: 0
succeedAfterAllAssertionsAreExecuted: 0
expectException: 0
expectedExceptionList:
succeedWhenExceptionIsThrown: 0
includedPlatforms: -1
platformsToIgnore: []
dynamic: 0
dynamicTypeName:
--- !u!4 &1575922007
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1575922005}
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: 159008373}
- {fileID: 1073989295}
m_Father: {fileID: 0}
m_RootOrder: 14
--- !u!1 &1734188738
GameObject:
m_ObjectHideFlags: 0
@ -2132,7 +2313,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!114 &1887843214
MonoBehaviour:
m_ObjectHideFlags: 0

2
Assets/Tests/TestAssets/Resources/Lua/testlua.txt

@ -0,0 +1,2 @@
-- Set a local variable. The test checks its value in a LuaScript component.
local v = 5

8
Assets/Tests/TestAssets/Resources/Lua/testlua.txt.meta

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 0046c396ec84246688b1057076138fdc
timeCreated: 1460546209
licenseType: Free
TextScriptImporter:
userData:
assetBundleName:
assetBundleVariant:
Loading…
Cancel
Save