toLua学习笔记
tolua版本1.0.7
1.生成wrap文件
创建自定义的类MyTestObject 在Editor/Custom/CustomSettings 的customTypeList 中添加自定义的类型
_GT(typeof(QualitySettings)),
_GT(typeof(RenderSettings)),
_GT(typeof(BlendWeights)),
_GT(typeof(RenderTexture)),
_GT(typeof(Resources)),
_GT(typeof(MyTestObject)),
点击unity菜单栏中的lua/gen lua Wrap + binder 运行后可以再Source/Generate中发现MytestObjectWrap文件 并且luabinder中也有注册信息
调用方法
LuaState lua = new LuaState();
lua.Start();
string hello =
@"
MyTestObject.PrintTest()
";
LuaBinder.Bind(lua); //调用将c#中的类传给lua
lua.DoString(hello);
lua.CheckTop();
lua.Dispose();
lua = null;
tolua添加委托
在customseting中添加自定义的委托类型
//附加导出委托类型(在导出委托时, customTypeList 中牵扯的委托类型都会导出, 无需写在这里)
public static DelegateType[] customDelegateList =
{
_DT(typeof(Action)),
_DT(typeof(UnityEngine.Events.UnityAction)),
_DT(typeof(System.Predicate<int>)),
_DT(typeof(System.Action<int>)),
_DT(typeof(System.Comparison<int>)),
_DT(typeof(System.Func<int, int>)),
_DT(typeof(MyTongDelegate.PrintTestDelegate)),
};
在下面的类型中 添加自定义的类
public static BindType[] customTypeList =
{
//------------------------为例子导出--------------------------------
//_GT(typeof(TestEventListener)),
//_GT(typeof(TestProtol)),
//_GT(typeof(TestAccount)),
//_GT(typeof(Dictionary<int, TestAccount>)).SetLibName("AccountMap"),
//_GT(typeof(KeyValuePair<int, TestAccount>)),
//_GT(typeof(Dictionary<int, TestAccount>.KeyCollection)),
//_GT(typeof(Dictionary<int, TestAccount>.ValueCollection)),
//_GT(typeof(TestExport)),
//_GT(typeof(TestExport.Space)),
//-------------------------------------------------------------------
_GT(typeof(Debugger)).SetNameSpace(null),
_GT(typeof(MyTongDelegate)),
lua中 直接通过类访问代理 通过= 或者 +绑定代理
function luaDele(listener)
listener.myDele = print1
end
整体代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LuaInterface;
public class MyTongDelegate : MonoBehaviour {
public delegate void PrintTestDelegate();
public PrintTestDelegate myDele = delegate { };
// Use this for initialization
void Start () {
string luaStr = @"
function print1()
print('print from lua')
end
function luaDele(listener)
listener.myDele = print1
end
";
LuaState lua = new LuaState();
lua.Start();
LuaBinder.Bind(lua);
DelegateFactory.Init();
lua.DoString(luaStr);
LuaFunction addClick = lua.GetFunction("luaDele");
addClick.Call<MyTongDelegate>(this);
myDele();
}
// Update is called once per frame
void Update () {
}
}
tolua版本1.0.7
1.生成wrap文件
创建自定义的类MyTestObject 在Editor/Custom/CustomSettings 的customTypeList 中添加自定义的类型
_GT(typeof(QualitySettings)),
_GT(typeof(RenderSettings)),
_GT(typeof(BlendWeights)),
_GT(typeof(RenderTexture)),
_GT(typeof(Resources)),
_GT(typeof(MyTestObject)),
点击unity菜单栏中的lua/gen lua Wrap + binder 运行后可以再Source/Generate中发现MytestObjectWrap文件 并且luabinder中也有注册信息
调用方法
LuaState lua = new LuaState();
lua.Start();
string hello =
@"
MyTestObject.PrintTest()
";
LuaBinder.Bind(lua); //调用将c#中的类传给lua
lua.DoString(hello);
lua.CheckTop();
lua.Dispose();
lua = null;
tolua添加委托
在customseting中添加自定义的委托类型
//附加导出委托类型(在导出委托时, customTypeList 中牵扯的委托类型都会导出, 无需写在这里)
public static DelegateType[] customDelegateList =
{
_DT(typeof(Action)),
_DT(typeof(UnityEngine.Events.UnityAction)),
_DT(typeof(System.Predicate<int>)),
_DT(typeof(System.Action<int>)),
_DT(typeof(System.Comparison<int>)),
_DT(typeof(System.Func<int, int>)),
_DT(typeof(MyTongDelegate.PrintTestDelegate)),
};
在下面的类型中 添加自定义的类
public static BindType[] customTypeList =
{
//------------------------为例子导出--------------------------------
//_GT(typeof(TestEventListener)),
//_GT(typeof(TestProtol)),
//_GT(typeof(TestAccount)),
//_GT(typeof(Dictionary<int, TestAccount>)).SetLibName("AccountMap"),
//_GT(typeof(KeyValuePair<int, TestAccount>)),
//_GT(typeof(Dictionary<int, TestAccount>.KeyCollection)),
//_GT(typeof(Dictionary<int, TestAccount>.ValueCollection)),
//_GT(typeof(TestExport)),
//_GT(typeof(TestExport.Space)),
//-------------------------------------------------------------------
_GT(typeof(Debugger)).SetNameSpace(null),
_GT(typeof(MyTongDelegate)),
lua中 直接通过类访问代理 通过= 或者 +绑定代理
function luaDele(listener)
listener.myDele = print1
end
整体代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LuaInterface;
public class MyTongDelegate : MonoBehaviour {
public delegate void PrintTestDelegate();
public PrintTestDelegate myDele = delegate { };
// Use this for initialization
void Start () {
string luaStr = @"
function print1()
print('print from lua')
end
function luaDele(listener)
listener.myDele = print1
end
";
LuaState lua = new LuaState();
lua.Start();
LuaBinder.Bind(lua);
DelegateFactory.Init();
lua.DoString(luaStr);
LuaFunction addClick = lua.GetFunction("luaDele");
addClick.Call<MyTongDelegate>(this);
myDele();
}
// Update is called once per frame
void Update () {
}
}