Xlua基础(三) Lua调用C#

           1.new C#对象

            local newGameObj = CS.UnityEngine.GameObject()
            local newGameObj2 = CS.UnityEngine.GameObject('helloworld')
            print(newGameObj, newGameObj2)

           2.访问静态属性,方法

            local GameObject = CS.UnityEngine.GameObject
            print('UnityEngine.Time.deltaTime:', CS.UnityEngine.Time.deltaTime) --读静态属性
            CS.UnityEngine.Time.timeScale = 0.5 --写静态属性
            print('helloworld', GameObject.Find('helloworld')) --静态方法调用

            3.访问成员属性,方法

            local DerivedClass = CS.Tutorial.DerivedClass
            local testobj = DerivedClass()--调用构造函数
            testobj.DMF = 1024--设置成员属性
            print(testobj.DMF)--读取成员属性
            testobj:DMFunc()--成员方法

            4.重载方法的调用

        testobj:TestFunc(100)
        testobj:TestFunc('hello')

               lua语言没办法区别单精度双精度,所以假设拥有单双精度的重载是按顺序来调用,会调用最上面的那个重载方法。

             5.可变参数方法调用

		public void VariableParamsFunc(int a, params string[] strs)
		{
			UnityEngine.Debug.Log("VariableParamsFunc: a =" + a);
			foreach (var str in strs)
			{
				UnityEngine.Debug.Log("str:" + str);
			}
		}


        ---lua 
        testobj:VariableParamsFunc(5, 'hello', 'john')

            6.带有结构体的参数的方法    

               在lua端定义一个表去映射结构体:

public struct MyStruct
{
     public string x;
     public string y;
}

public void CSharpFunc(MyStruct p)
{
     Debug.Log(p.x);
     Debug.Log(p.y);
}

--lua
myStructTable={x="C#",y="lua"}
obj:CSharpFunc(myStructTable)

           如果是带有接口参数,则和上面相似,不过要在接口的定义上加上[CSharpCallLua]来为接口生成实例代码。具体使用是[CSharpCallLua]还是[LuaCallCSharp]是看由哪方调用的。在这里,如果方法写在C#端,那么肯定由C#端去执行这个接口的业务,也就是说是由C#来执行lua的table的业务,那么实际上在方法的调用上是lua调用C# ,而方法内对接口内方法的调用就是CSharpCallLua.

           委托也是和接口一样,打上[CSharpCallLua]并且生成代码,然后由lua端写好function来传入。

           7.带有多返回数值的C#方法

              C#不是只有一个返回值吗?这里还得算上带有ref out的参数。 看个官方例子:

	public double ComplexFunc(Param1 p1, ref int p2, out string p3, Action luafunc, out Action csfunc)
		{
			Debug.Log("P1 = {x=" + p1.x + ",y=" + p1.y + "},p2 = " + p2);
			luafunc();
			p2 = p2 * p1.x;
			p3 = "hello " + p1.y;
			csfunc = () =>
			{
				Debug.Log("csharp callback invoked!");
			};
			return 1.23;
		}

--lua

   local ret, p2, p3, csfunc = testobj:ComplexFunc({x=3, y = 'john'}, 100, function()
               print('i am lua callback')
            end)
            print('ComplexFunc ret:', ret, p2, p3, csfunc)
            csfunc()

            看起来很复杂,实际上很简单,lua中调用这个方法,传值的时候只传了3个而实际上参数有5个,原因是带out的参数不需要传值。

       8.lua中不支持C#的泛型方法,但可以使用扩展方法来调用

          直接看官方例子:

public void GenericMethod<T>()
{
	Debug.Log("GenericMethod<" + typeof(T) + ">");
}

[LuaCallCSharp]
public static class DerivedClassExtensions
{
	public static int GetSomeData(this DerivedClass obj)
	{
		Debug.Log("GetSomeData ret = " + obj.DMF);
		return obj.DMF;
	}

	public static int GetSomeBaseData(this BaseClass obj)
	{
		Debug.Log("GetSomeBaseData ret = " + obj.BMF);
		return obj.BMF;
	}

	public static void GenericMethodOfString(this DerivedClass obj)
	{
		obj.GenericMethod<string>();
	}
}           


print(testobj:GetSomeData()) 
print(testobj:GetSomeBaseData()) --访问基类的Extension methods
testobj:GenericMethodOfString()  --通过Extension methods实现访问泛化方法

            9.事件

public event Action TestEvent;


--lua 

local function lua_event_callback1() print('lua_event_callback1') end
local function lua_event_callback2() print('lua_event_callback2') end
testobj:TestEvent('+', lua_event_callback1)
testobj:CallEvent()
testobj:TestEvent('+', lua_event_callback2)
testobj:CallEvent()
testobj:TestEvent('-', lua_event_callback1)
testobj:CallEvent()
testobj:TestEvent('-', lua_event_callback2)

           10.委托

		 public Action<string> TestDelegate = (param) =>
		 {
			  Debug.Log("TestDelegate in c#:" + param);
		 };


--lua

            testobj.TestDelegate('hello') --直接调用
            local function lua_delegate(str)
                print('TestDelegate in lua:', str)
            end
            testobj.TestDelegate = lua_delegate + testobj.TestDelegate --combine,这里演示的是C#delegate作为右值,左值也支持
            testobj.TestDelegate('hello')
            testobj.TestDelegate = testobj.TestDelegate - lua_delegate --remove
            testobj.TestDelegate('hello')

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值