Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mobile support #15

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 27 additions & 116 deletions Assets/Example/ExampleBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,131 +3,42 @@

using NLua;

public class ExampleBehaviour : MonoBehaviour {
public class ExampleBehaviour : NLuaBehaviour
{
protected override string LuaScriptFileName
{
get { return "ExampleBehaviour.lua"; }
}

string source = @"
import 'System'
import 'UnityEngine'
import 'Assembly-CSharp' -- The user-code assembly generated by Unity.
public override void Awake()
{
base.Awake();

local Angle = Vector3.up;
local Speed = 5;

local showEnv = false
local envScroll = Vector2.zero

function Update()
transform:RotateAround(transform.position,Angle,Speed * Time.deltaTime)

if Input.GetKey(KeyCode.W) then
Speed = Speed + 1
end

if Input.GetKey(KeyCode.S) then
Speed = Speed - 1
end

if Input.GetKey(KeyCode.A) then
Angle.z = Angle.z + 1
end

if Input.GetKey(KeyCode.D) then
Angle.z = Angle.z - 1
end

end

function OnGUI()
GUILayout.BeginArea(Rect(10,10,(Screen.width / 2) - 20,Screen.height - 20))

-- Adding '{ }' to the end of GUI functions satisfies their 'params' argument.
GUILayout.Label('[W/S] Speed: ' .. Speed)
GUILayout.Label('[A/D] Rot Angle: ' .. Angle:ToString())

GUILayout.EndArea()

GUILayout.BeginArea(Rect((Screen.width / 2) + 10,10,(Screen.width / 2) - 20,Screen.height - 20))

if GUILayout.Button('Show Enviroment',{ }) then
showEnv = not showEnv
end

if showEnv then
envScroll = GUILayout.BeginScrollView(envScroll,{ })
for k, v in pairs(_G) do
GUILayout.BeginHorizontal({ })

GUILayout.Label(k,{ })
GUILayout.FlexibleSpace()
GUILayout.Label(tostring(v),{ })

GUILayout.EndHorizontal()
end
GUILayout.EndScrollView()
end

GUILayout.EndArea()
end

";

Lua env;

void Awake() {
env = new Lua();
env.LoadCLRPackage();

env["this"] = this; // Give the script access to the gameobject.
env["transform"] = transform;

//System.Object[] result = new System.Object[0];
try {
//result = env.DoString(source);
env.DoString(source);
} catch(NLua.Exceptions.LuaException e) {
Debug.LogError(FormatException(e), gameObject);
}

}

void Start () {
Call("Start");
void Start ()
{
if (initialized == true)
{
Call("Start");
}
}

void Update () {
Call("Update");
}

void OnGUI() {
Call("OnGUI");
}

public System.Object[] Call(string function, params System.Object[] args) {
System.Object[] result = new System.Object[0];
if(env == null) return result;
LuaFunction lf = env.GetFunction(function);
if(lf == null) return result;
try {
// Note: calling a function that does not
// exist does not throw an exception.
if(args != null) {
result = lf.Call(args);
} else {
result = lf.Call();
}
} catch(NLua.Exceptions.LuaException e) {
Debug.LogError(FormatException(e), gameObject);
throw e;
}
return result;
}

public System.Object[] Call(string function) {
return Call(function, null);
void Update ()
{
if (initialized == true)
{
Call("Update");
}
}

public static string FormatException(NLua.Exceptions.LuaException e) {
string source = (string.IsNullOrEmpty(e.Source)) ? "<no source>" : e.Source.Substring(0, e.Source.Length - 2);
return string.Format("{0}\nLua (at {2})", e.Message, string.Empty, source);
void OnGUI()
{
if (initialized == true)
{
Call("OnGUI");
}
}
}
89 changes: 89 additions & 0 deletions Assets/Example/NLuaBehaviour.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using UnityEngine;
using System.Collections;

using NLua;

public abstract class NLuaBehaviour : MonoBehaviour
{
protected bool initialized;
protected Lua env;

protected abstract string LuaScriptFileName
{
get;
}

public virtual void Awake()
{
env = new Lua();
env.LoadCLRPackage();

StartCoroutine(LoadFile(LuaScriptFileName));
}

public System.Object[] Call(string function, params System.Object[] args)
{
System.Object[] result = new System.Object[0];
if (env == null) return result;
LuaFunction lf = env.GetFunction(function);
if (lf == null) return result;
try
{
// Note: calling a function that does not
// exist does not throw an exception.
if (args != null)
{
result = lf.Call(args);
}
else
{
result = lf.Call();
}
}
catch (NLua.Exceptions.LuaException e)
{
Debug.LogError(FormatException(e), gameObject);
}
return result;
}

public System.Object[] Call(string function)
{
return Call(function, null);
}

public static string FormatException(NLua.Exceptions.LuaException e)
{
string source = (string.IsNullOrEmpty(e.Source)) ? "<no source>" : e.Source.Substring(0, e.Source.Length - 2);
return string.Format("{0}\nLua (at {2})", e.Message, string.Empty, source);
}

public IEnumerator LoadFile(string luaFileName)
{
string filePath = Application.streamingAssetsPath + "/" + luaFileName;

string luaScript = null;
if (filePath.Contains("://"))
{
WWW www = new WWW(filePath);
yield return www;

luaScript = www.text;
}
else
{
luaScript = System.IO.File.ReadAllText(filePath);
}

try
{
env.DoString(luaScript);
}
catch (NLua.Exceptions.LuaException e)
{
Debug.LogError(FormatException(e), gameObject);
}

initialized = true;
}
}
12 changes: 12 additions & 0 deletions Assets/Example/NLuaBehaviour.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 27 additions & 62 deletions Assets/Example/SpawnSphere.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,80 +3,45 @@

using NLua;

public class SpawnSphere : MonoBehaviour {

string source = @"
import 'System'
import 'UnityEngine'
import 'Assembly-CSharp' -- The user-code assembly generated by Unity

function Update()
if Input.GetKey(KeyCode.Space) then
GameObject.Instantiate(sphere, transform.position, Quaternion.identity)
end
end

";

Lua env;
public class SpawnSphere : NLuaBehaviour
{
protected override string LuaScriptFileName
{
get { return "SpawnSphere.lua"; }
}

public GameObject sphere;

void Awake() {
env = new Lua();
env.LoadCLRPackage();
public override void Awake()
{
base.Awake();

env["this"] = this;
env["transform"] = transform;
env["sphere"] = sphere; // Give the script access to the prefab.

//System.Object[] result = new System.Object[0];
try {
//result = env.DoString(source);
env.DoString(source);
} catch(NLua.Exceptions.LuaException e) {
Debug.LogError(FormatException(e), gameObject);
}

}

void Start() {
Call("Start");
}

void Update() {
Call("Update");
}

void OnGUI() {
Call("OnGUI");
}

public System.Object[] Call(string function, params System.Object[] args) {
System.Object[] result = new System.Object[0];
if(env == null) return result;
LuaFunction lf = env.GetFunction(function);
if(lf == null) return result;
try {
// Note: calling a function that does not
// exist does not throw an exception.
if(args != null) {
result = lf.Call(args);
} else {
result = lf.Call();
}
} catch(NLua.Exceptions.LuaException e) {
Debug.LogError(FormatException(e), gameObject);
}
return result;
void Start()
{
if (initialized == true)
{
Call("Start");
}
}

public System.Object[] Call(string function) {
return Call(function, null);
void Update()
{
if (initialized == true)
{
Call("Update");
}
}

public static string FormatException(NLua.Exceptions.LuaException e) {
string source = (string.IsNullOrEmpty(e.Source)) ? "<no source>" : e.Source.Substring(0, e.Source.Length - 2);
return string.Format("{0}\nLua (at {2})", e.Message, string.Empty, source);
void OnGUI()
{
if (initialized == true)
{
Call("OnGUI");
}
}
}
4 changes: 3 additions & 1 deletion Assets/Example/Sphere.prefab.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading