点击 Lua下的Clear luabinder
然后Genluawrapfiles,C#的常用类转化成lua,存在LuaWrap文件夹
点Game下的Build Windows,在电脑的C盘生成一个和StramingAssets同样的文件夹
01_HelloWorld
using UnityEngine;
using System.Collections;
using LuaInterface;
public class HelloWorld : MonoBehaviour {
// Use this for initialization
void Start ()
{
LuaState l = new LuaState();//相当于lua的解析器
string str = "print('hello world 世界')";
l.DoString(str);//执行lua语句
}
02_CreateGameObject=======LuaState
using UnityEngine;
using System.Collections;
using LuaInterface;
public class CreateGameObject01 : MonoBehaviour {
private string script = @"
luanet.load_assembly('UnityEngine')
GameObject = luanet.import_type('UnityEngine.GameObject')
ParticleSystem = luanet.import_type('UnityEngine.ParticleSystem')
local newGameObj = GameObject('NewObj')
newGameObj:AddComponent(luanet.ctype(ParticleSystem))
";
//反射调用,反射就是lua取得C#中的类
void Start ()
{
//LuaState对lua进行了封装
LuaState lua = new LuaState();
lua.DoString(script);
}
LuaScriptMgr脚本
using UnityEngine;
using System.Collections;
using LuaInterface;
public class CreateGameObject02 : MonoBehaviour {
private string script = @"
luanet.load_assembly('UnityEngine')
GameObject = UnityEngine.GameObject
ParticleSystem = UnityEngine.ParticleSystem
local newGameObj = GameObject('NewObj')
newGameObj:AddComponent(ParticleSystem.GetClassType())
";
//非反射调用
void Start ()
{
//把预先要用到的类注册到lua环境当中,C盘中的simpleframwork
//单例模式,在luastate的基础上又进行了封装
LuaScriptMgr lua = new LuaScriptMgr();//从C盘中的simpleframwork加载lua代码
lua.Start();//启动框架,初始化工作
lua.DoString(script);
}
在Unity中访问Lua的变量
using LuaInterface;
public class AccessingLuaVariables01 : MonoBehaviour
{
private string script = @"
luanet.load_assembly('UnityEngine')
GameObject = luanet.import_type('UnityEngine.GameObject')
ParticleSystem = luanet.import_type('UnityEngine.ParticleSystem')
particles = {}
for i = 1, Objs2Spawn, 1 do
local newGameObj = GameObject('NewObj' .. tostring(i))
local ps = newGameObj:AddComponent(luanet.ctype(ParticleSystem))
ps:Stop()--粒子系统停止
table.insert(particles, ps)
end
var2read = 42
";
void Start () {
LuaState l = new LuaState();
// Assign to global scope variables as if they're keys in a dictionary (they are really)
l["Objs2Spawn"] = 5;
l.DoString(script);
print("Read from lua: " + l["var2read"].ToString());//从lua中读取的变量
LuaTable particles = (LuaTable)l["particles"];
foreach( ParticleSystem ps in particles.Values )
{
ps.Play();
}
}
}
using LuaInterface;
public class AccessingLuaVariables02 : MonoBehaviour
{
//cstolua要求必须要先定义变量才能使用
private string var = @"Objs2Spawn = 0";
private string script = @"
particles = {}
ParticleSystem = UnityEngine.ParticleSystem
for i = 1, Objs2Spawn, 1 do
local newGameObj = GameObject('NewObj' .. tostring(i))
local ps = newGameObj:AddComponent(ParticleSystem.GetClassType())
ps:Stop()
table.insert(particles, ps)
end
var2read = 42
";
// Use this for initialization
void Start () {
LuaScriptMgr mgr = new LuaScriptMgr();
mgr.Start();
LuaState l = mgr.lua;
l.DoString(var);//定义变量
l["Objs2Spawn"] = 5;
l.DoString(script);
print("Read from lua: " + l["var2read"].ToString());
LuaTable particles = (LuaTable)l["particles"];
foreach( ParticleSystem ps in particles.Values )
{
ps.Play();
}
}
}
执行Lua脚本文件,(脚本文件在Unity中指定)
using LuaInterface;
public class ScriptsFromFile_01 : MonoBehaviour
{
public TextAsset scriptFile;
// Use this for initialization
void Start()
{
LuaState l = new LuaState();
l.DoString(scriptFile.text);
}
}
public class ScriptsFromFile_02 : MonoBehaviour
{
// Use this for initialization
void Start()
{
//只是展示如何加载文件。不是推荐这么做
LuaState l = new LuaState();
string path = Application.dataPath + "/uLua/Examples/04_ScriptsFromFile/ScriptsFromFile02.lua";
l.DoFile(path);
}
}
调用Lua方法
using LuaInterface;
public class CallLuaFunction_01 : MonoBehaviour {
private string script = @"
function luaFunc(message)
print(message)
return 42
end
";
void Start () {
LuaState l = new LuaState();
l.DoString(script);
LuaFunction f = l.GetFunction("luaFunc");
object[] r = f.Call("I called a lua function!");
print(r[0]);
}
}
using LuaInterface;
using System;
public class CallLuaFunction_02 : MonoBehaviour {
private string script = @"
function luaFunc(num)
return num
end
";
LuaFunction func = null;
void Start () {
LuaScriptMgr mgr = new LuaScriptMgr();
mgr.DoString(script);
func = mgr.GetLuaFunction("luaFunc");
object[] r = func.Call(123456);
print(r[0]);
int num = CallFunc();
print(num);
}
void OnDestroy()
{
if (func != null)
{
func.Release();
}
}
int CallFunc()
{
int top = func.BeginPCall();
IntPtr L = func.GetLuaState();
LuaScriptMgr.Push(L, 123456);
int num = (int)LuaScriptMgr.GetNumber(L, -1);
func.EndPCall(top);
return num;
}
}
在C#调用Lua中的协程
using LuaInterface;
public class LuaCoroutines : MonoBehaviour
{
private string script = @"
function fib(n)
local a, b = 0, 1
while n > 0 do
a, b = b, a + b
n = n - 1
end
return a
end
function CoFunc()
print('Coroutine started')
local i = 0
for i = 0, 10, 1 do
print(fib(i))
coroutine.wait(1)
end
print('Coroutine ended')
end
function myFunc()
coroutine.start(CoFunc)--调用lua自身的的协程
end
";
private LuaScriptMgr lua = null;
void Awake ()
{
lua = new LuaScriptMgr();
lua.Start();
lua.DoString(script);
LuaFunction f = lua.GetLuaFunction("myFunc");
f.Call();
f.Release();
}
// Update is called once per frame
void Update ()
{
lua.Update();
}
void LateUpdate()
{
lua.LateUpate();
}
void FixedUpdate()
{
lua.FixedUpdate();
}
}