From b4b62f22cfee06cf33763d9a1bd859a32d19f1eb Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Mon, 4 Apr 2016 13:06:13 +0100 Subject: [PATCH] Exposed common GameObject methods like Find, Instantiate, Destroy --- Assets/Fungus/Lua/Scripts/LuaUtils.cs | 40 +++ Assets/Tests/Lua/LuaEnvironmentTests.unity | 281 ++++++++++++++++++++- 2 files changed, 317 insertions(+), 4 deletions(-) diff --git a/Assets/Fungus/Lua/Scripts/LuaUtils.cs b/Assets/Fungus/Lua/Scripts/LuaUtils.cs index 8a0a9a4f..5205230d 100644 --- a/Assets/Fungus/Lua/Scripts/LuaUtils.cs +++ b/Assets/Fungus/Lua/Scripts/LuaUtils.cs @@ -302,6 +302,46 @@ namespace Fungus } } + /// + /// Find a game object by name and returns it. + /// + public virtual GameObject Find(string name) + { + return GameObject.Find(name); + } + + /// + /// Returns one active GameObject tagged tag. Returns null if no GameObject was found. + /// + public virtual GameObject FindWithTag(string tag) + { + return GameObject.FindGameObjectWithTag(tag); + } + + /// + /// Returns a list of active GameObjects tagged tag. Returns empty array if no GameObject was found. + /// + public virtual GameObject[] FindGameObjectsWithTag(string tag) + { + return GameObject.FindGameObjectsWithTag(tag); + } + + /// + /// Create a copy of a GameObject. + /// Can be used to instantiate prefabs. + /// + public virtual GameObject Instantiate(GameObject go) + { + return GameObject.Instantiate(go); + } + + /// + /// Destroys an instance of a GameObject. + /// + public virtual void Destroy(GameObject go) + { + GameObject.Destroy(go); + } } } \ No newline at end of file diff --git a/Assets/Tests/Lua/LuaEnvironmentTests.unity b/Assets/Tests/Lua/LuaEnvironmentTests.unity index 9e848e8d..11c6857a 100644 --- a/Assets/Tests/Lua/LuaEnvironmentTests.unity +++ b/Assets/Tests/Lua/LuaEnvironmentTests.unity @@ -262,7 +262,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 1 + m_IsActive: 0 --- !u!114 &256438763 MonoBehaviour: m_ObjectHideFlags: 0 @@ -515,6 +515,65 @@ Transform: Transform: m_PrefabParentObject: {fileID: 495584, guid: 49031c561e16d4fcf91c12153f8e0b25, type: 2} m_PrefabInternal: {fileID: 1073733979} +--- !u!1 &550607923 +GameObject: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 100640, guid: 49031c561e16d4fcf91c12153f8e0b25, type: 2} + m_PrefabInternal: {fileID: 0} + serializedVersion: 4 + m_Component: + - 4: {fileID: 550607924} + - 114: {fileID: 550607926} + - 114: {fileID: 550607925} + m_Layer: 0 + m_Name: LuaEnvironment + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &550607924 +Transform: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 495584, guid: 49031c561e16d4fcf91c12153f8e0b25, type: 2} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 550607923} + 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: 1537199682} + m_RootOrder: 0 +--- !u!114 &550607925 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 11486636, guid: 49031c561e16d4fcf91c12153f8e0b25, + type: 2} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 550607923} + 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 &550607926 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 11493126, guid: 49031c561e16d4fcf91c12153f8e0b25, + type: 2} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 550607923} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: ba19c26c1ba7243d2b57ebc4329cc7c6, type: 3} + m_Name: + m_EditorClassIdentifier: + remoteDebugger: 0 --- !u!1 &637150167 GameObject: m_ObjectHideFlags: 0 @@ -729,6 +788,33 @@ Transform: - {fileID: 681659774} m_Father: {fileID: 0} m_RootOrder: 5 +--- !u!1 &804588883 +GameObject: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + serializedVersion: 4 + m_Component: + - 4: {fileID: 804588884} + m_Layer: 0 + m_Name: TestGameObject + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &804588884 +Transform: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 804588883} + 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: 1537199682} + m_RootOrder: 3 --- !u!1 &1052730009 GameObject: m_ObjectHideFlags: 0 @@ -1159,6 +1245,65 @@ Transform: m_Children: [] m_Father: {fileID: 0} m_RootOrder: 0 +--- !u!1 &1436236943 +GameObject: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 178698, guid: e0c2b90c058ff43f4a56a266d4fa721b, type: 2} + m_PrefabInternal: {fileID: 0} + serializedVersion: 4 + m_Component: + - 4: {fileID: 1436236944} + - 114: {fileID: 1436236945} + m_Layer: 0 + m_Name: LuaBindings + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &1436236944 +Transform: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 403334, guid: e0c2b90c058ff43f4a56a266d4fa721b, type: 2} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1436236943} + 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: 1537199682} + m_RootOrder: 2 +--- !u!114 &1436236945 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 11414792, guid: e0c2b90c058ff43f4a56a266d4fa721b, + type: 2} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1436236943} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 4cc8a659e950044b69d7c62696c36962, type: 3} + m_Name: + m_EditorClassIdentifier: + tableName: + registerTypes: 1 + boundTypes: + - UnityEngine.GameObject, UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null + - UnityEngine.PrimitiveType, UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null + - UnityEngine.Component, UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null + - System.Type, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + - System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + - System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + - UnityEngine.SendMessageOptions, UnityEngine, Version=0.0.0.0, Culture=neutral, + PublicKeyToken=null + - UnityEngine.SceneManagement.Scene, UnityEngine, Version=0.0.0.0, Culture=neutral, + PublicKeyToken=null + - UnityEngine.Object, UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null + - System.Single, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + boundObjects: + - key: testgameobject + obj: {fileID: 804588883} + component: {fileID: 0} --- !u!1 &1463630762 GameObject: m_ObjectHideFlags: 0 @@ -1319,6 +1464,59 @@ Transform: - {fileID: 1738134228} m_Father: {fileID: 0} m_RootOrder: 3 +--- !u!1 &1537199680 +GameObject: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + serializedVersion: 4 + m_Component: + - 4: {fileID: 1537199682} + - 114: {fileID: 1537199681} + m_Layer: 0 + m_Name: GameObjectTest + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &1537199681 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1537199680} + 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 &1537199682 +Transform: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1537199680} + 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: 550607924} + - {fileID: 1932132174} + - {fileID: 1436236944} + - {fileID: 804588884} + m_Father: {fileID: 0} + m_RootOrder: 12 --- !u!1 &1549270604 GameObject: m_ObjectHideFlags: 1 @@ -1623,7 +1821,7 @@ MonoBehaviour: variablesScrollPos: {x: 0, y: 0} variablesExpanded: 1 blockViewHeight: 400 - zoom: 1 + zoom: 0.983999 scrollViewRect: serializedVersion: 2 x: -343 @@ -1631,7 +1829,8 @@ MonoBehaviour: width: 1114 height: 859 selectedBlock: {fileID: 1828947834} - selectedCommands: [] + selectedCommands: + - {fileID: 1828947833} 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." @@ -1683,7 +1882,7 @@ MonoBehaviour: nodeRect: serializedVersion: 2 x: 67 - y: 69 + y: 67.983734 width: 120 height: 40 itemId: 0 @@ -1823,6 +2022,80 @@ Transform: - {fileID: 1124932172} m_Father: {fileID: 0} m_RootOrder: 4 +--- !u!1 &1932132173 +GameObject: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 139298, guid: c356764ac08ce4af2806a601a4f1e6e9, type: 2} + m_PrefabInternal: {fileID: 0} + serializedVersion: 4 + m_Component: + - 4: {fileID: 1932132174} + - 114: {fileID: 1932132175} + m_Layer: 0 + m_Name: LuaScript + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &1932132174 +Transform: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 449874, guid: c356764ac08ce4af2806a601a4f1e6e9, type: 2} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1932132173} + 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: 1537199682} + m_RootOrder: 1 +--- !u!114 &1932132175 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 11472914, guid: c356764ac08ce4af2806a601a4f1e6e9, + type: 2} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1932132173} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 446caeace65234baaacd52095d24f101, type: 3} + m_Name: + m_EditorClassIdentifier: + executeAfterTime: 1 + repeatExecuteTime: 1 + repeatEveryTime: 1 + executeAfterFrames: 1 + repeatExecuteFrame: 1 + repeatEveryFrame: 1 + hasFailed: 0 + executeMethods: 2 + luaEnvironment: {fileID: 0} + luaFile: {fileID: 0} + luaScript: '-- Test cloning and renaming a game object + + + testgo = unity.luautils.find("TestGameObject") + + fungus.assert(testgo != null) + + + go = unity.luautils.instantiate(testgo) + + fungus.assert(go != null) + + + go.name = "fred" + + fungus.assert(go.name == "fred") + + + unity.luautils.destroy(go) + + + fungus.pass()' + runAsCoroutine: 1 + useFungusModule: 1 --- !u!1 &1960220030 GameObject: m_ObjectHideFlags: 0