16.TestOverride(这里是关于重写的方式)
public class TestOverride//方法
{
public enum Space
{
World = 1
}
public int Test(object o, string str)
{
Debug.Log("call Test(object o, string str)");
return 1;
}
public int Test(char c)
{
Debug.Log("call Test(char c)");
return 2;
}
public int Test(int i)
{
Debug.Log("call Test(int i)");
return 3;
}
//有这个函数要扔掉上面两个精度不匹配的,因为lua是double
public int Test(double d)
{
Debug.Log("call Test(double d)");
return 4;
}
public int Test(int i, int j)
{
Debug.Log("call Test(int i, int j)");
return 5;
}
public int Test(string str)
{
Debug.Log("call Test(string str)");
return 6;
}
public static int Test(string str1, string str2)
{
Debug.Log("call static Test(string str1, string str2)");
return 7;
}
public int Test(object o)
{
Debug.Log("call Test(object o)");
return 8;
}
public int Test(params object[] objs)
{
Debug.Log("call Test(params object[] objs)");
return 9;
}
public int Test(Space e)
{
Debug.Log("call Test(TestEnum e)");
return 10;
}
}
public class TestOverride01 : MonoBehaviour//运行脚本
{
private string script =
@"
function Test(to)
assert(to:Test(1) == 4)
assert(to:Test('hello') == 6)
assert(to:Test(object.New()) == 8)
assert(to:Test(123, 456) == 5)
assert(to:Test('123', '456') == 1)
assert(to:Test(object.New(), '456') == 1)
assert(to:Test('123', 456) == 9)
assert(to:Test('123', object.New()) == 9)
assert(to:Test(1,2,3) == 9)
assert(to:Test(TestOverride.Space.World) == 10)
end
";
//反射已经无法区分这些重载函数了
void Start()
{
LuaScriptMgr mgr = new LuaScriptMgr();
mgr.Start();
TestOverrideWrap.Register(mgr.GetL());
TestOverride_SpaceWrap.Register(mgr.GetL());
mgr.DoString(script);
TestOverride to = new TestOverride();
LuaFunction func = mgr.GetLuaFunction("Test");
func.Call(to);
}
}
剩下的两个类是调用源码,因为太长,可以跳过,这里只是记录,方便日后查看
public class TestOverride_SpaceWrap
{
static LuaMethod[] enums = new LuaMethod[]
{
new LuaMethod("World", GetWorld),
new LuaMethod("IntToEnum", IntToEnum),
};
public static void Register(IntPtr L)
{
LuaScriptMgr.RegisterLib(L, "TestOverride.Space", typeof(TestOverride.Space), enums);
}
[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
static int GetWorld(IntPtr L)
{
LuaScriptMgr.Push(L, TestOverride.Space.World);
return 1;
}
[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
static int IntToEnum(IntPtr L)
{
int arg0 = (int)LuaDLL.lua_tonumber(L, 1);
TestOverride.Space o = (TestOverride.Space)arg0;
LuaScriptMgr.Push(L, o);
return 1;
}
}
public class TestOverrideWrap
{
public static void Register(IntPtr L)
{
LuaMethod[] regs = new LuaMethod[]
{
new LuaMethod("Test", Test),
new LuaMethod("New", _CreateTestOverride),
new LuaMethod("GetClassType", GetClassType),
};
LuaField[] fields = new LuaField[]
{
};
LuaScriptMgr.RegisterLib(L, "TestOverride", typeof(TestOverride), regs, fields, typeof(object));
}
[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
static int _CreateTestOverride(IntPtr L)
{
int count = LuaDLL.lua_gettop(L);
if (count == 0)
{
TestOverride obj = new TestOverride();
LuaScriptMgr.PushObject(L, obj);
return 1;
}
else
{
LuaDLL.luaL_error(L, "invalid arguments to method: TestOverride.New");
}
return 0;
}
static Type classType = typeof(TestOverride);
[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
static int GetClassType(IntPtr L)
{
LuaScriptMgr.Push(L, classType);
return 1;
}
[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
static int Test(IntPtr L)
{
int count = LuaDLL.lua_gettop(L);
if (count == 2 && LuaScriptMgr.CheckTypes(L, 1, typeof(TestOverride), typeof(string)))
{
TestOverride obj = (TestOverride)LuaScriptMgr.GetNetObjectSelf(L, 1, "TestOverride");
string arg0 = LuaScriptMgr.GetString(L, 2);
int o = obj.Test(arg0);
LuaScriptMgr.Push(L, o);
return 1;
}
else if (count == 2 && LuaScriptMgr.CheckTypes(L, 1, typeof(string), typeof(string)))
{
string arg0 = LuaScriptMgr.GetString(L, 1);
string arg1 = LuaScriptMgr.GetString(L, 2);
int o = TestOverride.Test(arg0,arg1);
LuaScriptMgr.Push(L, o);
return 1;
}
else if (count == 2 && LuaScriptMgr.CheckTypes(L, 1, typeof(TestOverride), typeof(TestOverride.Space)))
{
TestOverride obj = (TestOverride)LuaScriptMgr.GetNetObjectSelf(L, 1, "TestOverride");
TestOverride.Space arg0 = (TestOverride.Space)LuaScriptMgr.GetLuaObject(L, 2);
int o = obj.Test(arg0);
LuaScriptMgr.Push(L, o);
return 1;
}
else if (count == 2 && LuaScriptMgr.CheckTypes(L, 1, typeof(TestOverride), typeof(double)))
{
TestOverride obj = (TestOverride)LuaScriptMgr.GetNetObjectSelf(L, 1, "TestOverride");
double arg0 = (double)LuaDLL.lua_tonumber(L, 2);
int o = obj.Test(arg0);
LuaScriptMgr.Push(L, o);
return 1;
}
else if (count == 2 && LuaScriptMgr.CheckTypes(L, 1, typeof(TestOverride), typeof(object)))
{
TestOverride obj = (TestOverride)LuaScriptMgr.GetNetObjectSelf(L, 1, "TestOverride");
object arg0 = LuaScriptMgr.GetVarObject(L, 2);
int o = obj.Test(arg0);
LuaScriptMgr.Push(L, o);
return 1;
}
else if (count == 3 && LuaScriptMgr.CheckTypes(L, 1, typeof(TestOverride), typeof(int), typeof(int)))
{
TestOverride obj = (TestOverride)LuaScriptMgr.GetNetObjectSelf(L, 1, "TestOverride");
int arg0 = (int)LuaDLL.lua_tonumber(L, 2);
int arg1 = (int)LuaDLL.lua_tonumber(L, 3);
int o = obj.Test(arg0,arg1);
LuaScriptMgr.Push(L, o);
return 1;
}
else if (count == 3 && LuaScriptMgr.CheckTypes(L, 1, typeof(TestOverride), typeof(object), typeof(string)))
{
TestOverride obj = (TestOverride)LuaScriptMgr.GetNetObjectSelf(L, 1, "TestOverride");
object arg0 = LuaScriptMgr.GetVarObject(L, 2);
string arg1 = LuaScriptMgr.GetString(L, 3);
int o = obj.Test(arg0,arg1);
LuaScriptMgr.Push(L, o);
return 1;
}
else if (LuaScriptMgr.CheckParamsType(L, typeof(object), 2, count - 1))
{
TestOverride obj = (TestOverride)LuaScriptMgr.GetNetObjectSelf(L, 1, "TestOverride");
object[] objs0 = LuaScriptMgr.GetParamsObject(L, 2, count - 1);
int o = obj.Test(objs0);
LuaScriptMgr.Push(L, o);
return 1;
}
else
{
LuaDLL.luaL_error(L, "invalid arguments to method: TestOverride.Test");
}
return 0;
}
}
不得不说,做什么都要有耐心和毅力,就拿这个uLua来说吧,也是要花一定的时间的,嗯…就先这样吧.