Lua_ uLua_Lua调用C#方法_020

本文通过一个具体的示例展示了如何使用Lua脚本与C#进行交互,包括使用反射和非反射两种方式来调用C#类的方法,并介绍了如何在Unity环境中实现这一过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


反射方式

 string str=@"

        luanet.load_assembly('UnityEngine');
        GameObject  = luanet.import_type('UnityEngine.GameObject');
        ParticleSystem  = luanet.import_type('UnityEngine.ParticleSystem');

        Test = luanet.import_type('Test')   ;

        particles={}

        for i=1,sum,1 do
            local obj = GameObject('hello')
            obj:AddComponent(luanet.ctype(ParticleSystem))
            test = Test()
            test:Show();
            local ps = obj:GetComponent(luanet.ctype(ParticleSystem));
            ps:Stop();
            table.insert(particles,ps)
        end

“;



Test类:

public class Test  {

    public void Show(){

        Debug.Log("showMessage");
    }

}

非反射方式

1、将自己编写的脚本加入到WrapFile文件下方
如图:
这里写图片描述
点击Lua -> Gen Lua Wrap Files

using UnityEngine;
using System.Collections;

public class LuaTest03 : MonoBehaviour {


    string script =@"


        test = Test();
        test:Show();
        Test.ShowMess();

    ";

    // Use this for initialization
    void Start () {
        LuaScriptMgr mgr = new LuaScriptMgr();
        mgr.Start();
        mgr.DoString(script);

    }

    // Update is called once per frame
    void Update () {

    }
}

C#类:

using UnityEngine;
using System.Collections;

public class Test  {

    // Use this for initialization
    void Start () {

    }


    public void Show(){

        Debug.Log("showMessage");
    }

    public  static void ShowMess()
    {
        Debug.Log("Show");

    }
    // Update is called once per frame
    void Update () {

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yy763496668

您的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值