From ee3533ba6dfa91efe62bacd0703223fb0f5223d3 Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Thu, 31 Mar 2016 11:29:05 +0100 Subject: [PATCH] Type factory class for constructing POD types like Color MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit MoonSharp’s __new function appears to be unreliable for POD types like Color, Vector2, etc. This class provides simple factory methods for constructing these types, which are then exposed through unity.factory in Lua. --- .../FungusScript/Scripts/FungusScript.cs | 3 + .../FungusScript/Scripts/PODTypeFactory.cs | 84 +++++++++++++++++++ .../Scripts/PODTypeFactory.cs.meta | 12 +++ Assets/Tests/FungusScript/FungusTests.unity | 41 ++------- 4 files changed, 107 insertions(+), 33 deletions(-) create mode 100644 Assets/Fungus/FungusScript/Scripts/PODTypeFactory.cs create mode 100644 Assets/Fungus/FungusScript/Scripts/PODTypeFactory.cs.meta diff --git a/Assets/Fungus/FungusScript/Scripts/FungusScript.cs b/Assets/Fungus/FungusScript/Scripts/FungusScript.cs index a671fa51..0b349f15 100644 --- a/Assets/Fungus/FungusScript/Scripts/FungusScript.cs +++ b/Assets/Fungus/FungusScript/Scripts/FungusScript.cs @@ -258,6 +258,9 @@ namespace Fungus unityTable["time"] = UserData.CreateStatic(typeof(Time)); unityTable["fungusprefs"] = UserData.CreateStatic(typeof(FungusPrefs)); + UserData.RegisterType(typeof(PODTypeFactory)); + unityTable["factory"] = UserData.CreateStatic(typeof(PODTypeFactory)); + // This FungusScript object unityTable["fungusscript"] = this; diff --git a/Assets/Fungus/FungusScript/Scripts/PODTypeFactory.cs b/Assets/Fungus/FungusScript/Scripts/PODTypeFactory.cs new file mode 100644 index 00000000..bef1e265 --- /dev/null +++ b/Assets/Fungus/FungusScript/Scripts/PODTypeFactory.cs @@ -0,0 +1,84 @@ +using UnityEngine; +using System.Collections; + +namespace Fungus +{ + + /// + /// Factory class to create new instances of common POD value types used by Unity. + /// Supports the same types as the SerializedProperty class: Color, Vector2, Vector3, Vector4, Quaternion & Rect. + /// MoonSharp doesn't work well with these types due to internal interop issues with c#. + /// Use these factory methods to constuct these types instead of using the __new function call in Lua. + /// + public static class PODTypeFactory + { + /// + /// Returns a new Color object. + /// + /// The red component. + /// The green component. + /// The blue component. + /// The alpha component. + public static Color color(float r, float g, float b, float a) + { + return new Color(r,g,b,a); + } + + /// + /// Returns a new Vector2 object. + /// + /// The x coordinate. + /// The y coordinate. + public static Vector2 vector2(float x, float y) + { + return new Vector2(x,y); + } + + /// + /// Returns a new Vector3 object. + /// + /// The x coordinate. + /// The y coordinate. + /// The z coordinate. + public static Vector3 vector3(float x, float y, float z) + { + return new Vector3(x,y,z); + } + + /// + /// Returns a new Vector4 object. + /// + /// The x coordinate. + /// The y coordinate. + /// The z coordinate. + /// The w coordinate. + public static Vector4 vector4(float x, float y, float z, float w) + { + return new Vector4(x,y,z,w); + } + + /// + /// Returns a new Quaternion object representing a rotation. + /// + /// The x rotation in degrees. + /// The y rotation in degrees. + /// The z rotation in degrees. + public static Quaternion quaternion(float x, float y, float z) + { + return UnityEngine.Quaternion.Euler(x,y,z); + } + + /// + /// Returns a new Rect object. + /// + /// The x coordinate. + /// The y coordinate. + /// The rectangle width. + /// The rectangle height. + public static Rect rect(float x, float y, float width, float height) + { + return new Rect(x,y,width, height); + } + } + +} \ No newline at end of file diff --git a/Assets/Fungus/FungusScript/Scripts/PODTypeFactory.cs.meta b/Assets/Fungus/FungusScript/Scripts/PODTypeFactory.cs.meta new file mode 100644 index 00000000..ec05b69d --- /dev/null +++ b/Assets/Fungus/FungusScript/Scripts/PODTypeFactory.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 7e3401d05b72e4960ad1b35a73e1571a +timeCreated: 1459374585 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Tests/FungusScript/FungusTests.unity b/Assets/Tests/FungusScript/FungusTests.unity index f1b068d1..2057ea62 100644 --- a/Assets/Tests/FungusScript/FungusTests.unity +++ b/Assets/Tests/FungusScript/FungusTests.unity @@ -142,7 +142,6 @@ MonoBehaviour: - key: voiceover obj: {fileID: 8300000, guid: d2ab1fbe555d44e9e870311657856bdc, type: 3} component: {fileID: 0} - typeRegistry: {fileID: 0} --- !u!1 &13683311 GameObject: m_ObjectHideFlags: 0 @@ -715,7 +714,6 @@ MonoBehaviour: - key: menudialog obj: {fileID: 560555132} component: {fileID: 560555134} - typeRegistry: {fileID: 0} --- !u!4 &36455799 stripped Transform: m_PrefabParentObject: {fileID: 495584, guid: 49031c561e16d4fcf91c12153f8e0b25, type: 2} @@ -1274,7 +1272,6 @@ MonoBehaviour: - key: optionbutton0 obj: {fileID: 876352038} component: {fileID: 876352041} - typeRegistry: {fileID: 0} --- !u!1 &190681717 GameObject: m_ObjectHideFlags: 0 @@ -2738,7 +2735,6 @@ MonoBehaviour: - key: flowchart obj: {fileID: 1983492490} component: {fileID: 1983492495} - typeRegistry: {fileID: 0} --- !u!1 &507572619 GameObject: m_ObjectHideFlags: 0 @@ -3617,7 +3613,6 @@ MonoBehaviour: - key: transform obj: {fileID: 328064332} component: {fileID: 328064333} - typeRegistry: {fileID: 0} --- !u!1001 &726346786 Prefab: m_ObjectHideFlags: 0 @@ -4749,7 +4744,6 @@ MonoBehaviour: - key: obj: {fileID: 0} component: {fileID: 0} - typeRegistry: {fileID: 0} --- !u!1 &928936324 GameObject: m_ObjectHideFlags: 0 @@ -5024,7 +5018,7 @@ MonoBehaviour: m_EditorClassIdentifier: nodeRect: serializedVersion: 2 - x: 100 + x: 98.90469 y: 61 width: 120 height: 40 @@ -5060,8 +5054,8 @@ MonoBehaviour: version: 1 scrollPos: {x: 32, y: -30} variablesScrollPos: {x: 0, y: 0} - variablesExpanded: 1 - blockViewHeight: 400 + variablesExpanded: 0 + blockViewHeight: 251 zoom: 0.9129947 scrollViewRect: serializedVersion: 2 @@ -5071,7 +5065,7 @@ MonoBehaviour: height: 859 selectedBlock: {fileID: 942227027} selectedCommands: - - {fileID: 942227038} + - {fileID: 942227053} variables: - {fileID: 942227029} - {fileID: 942227035} @@ -5238,15 +5232,7 @@ MonoBehaviour: errorMessage: indentLevel: 0 fungusScript: {fileID: 715246548} - luaScript: 'local c = unity.color.__new() - - c.r = 1 - - c.g = 0.5 - - c.b = 1 - - c.a = 1 + luaScript: 'local c = unity.factory.color(1, 0.5, 1, 1) return c' @@ -5419,11 +5405,8 @@ MonoBehaviour: errorMessage: indentLevel: 0 fungusScript: {fileID: 715246548} - luaScript: 'local v = unity.vector2.__new() - - v.x = 1 + luaScript: 'local v = unity.factory.vector2(1,1) - v.y =1 return v' useFungusModule: 1 @@ -5493,13 +5476,7 @@ MonoBehaviour: errorMessage: indentLevel: 0 fungusScript: {fileID: 715246548} - luaScript: 'local v = unity.vector3.__new() - - v.x = 1 - - v.y =1 - - v.z = 1 + luaScript: 'local v = unity.factory.vector3(1,1,1) return v' useFungusModule: 1 @@ -6129,7 +6106,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 1 + m_IsActive: 0 --- !u!114 &1109996700 MonoBehaviour: m_ObjectHideFlags: 0 @@ -8133,7 +8110,6 @@ MonoBehaviour: - key: flowchart obj: {fileID: 807616582} component: {fileID: 807616590} - typeRegistry: {fileID: 0} --- !u!1 &1738857300 GameObject: m_ObjectHideFlags: 0 @@ -9626,7 +9602,6 @@ MonoBehaviour: - key: mousepointer obj: {fileID: 2800000, guid: efe8aa62cc87f4b70b9272e4e8e06668, type: 3} component: {fileID: 0} - typeRegistry: {fileID: 0} --- !u!1 &2119731355 GameObject: m_ObjectHideFlags: 0