Unity3D UniLua调用C#

Test2.CS

using UnityEngine;
using System.Collections;
using UniLua;
using System;

public class Test2 : MonoBehaviour
{
    // 库的名称, 可以是任意字符串
    public const string LIB_NAME = "Test2.cs";

    public string LuaScriptFile = "framework/lantern.lua";

    // 创建 Lua 虚拟机
    private ILuaState Lua = LuaAPI.NewState();

    private int add;

    void Awake()
    {
        Lua.L_OpenLibs();
        Lua.L_RequireF(Test2.LIB_NAME, Test2.OpenLib, false);
        var status = Lua.L_DoFile(LuaScriptFile);
        if (status != ThreadStatus.LUA_OK)
        {
            throw new Exception(Lua.ToString(-1));
        }

        if (!Lua.IsTable(-1))
        {
            throw new Exception("framework main's return value is not a table");
        }
        add = StoreMethod("add");
        Lua.Pop(1);
    }

    void OnGUI()
    {
        if (GUI.Button(new Rect(0, 0, 200, 20), "ADD"))
        {
            CallMethod(add);
        }
    }

    public static int OpenLib(ILuaState lua) // 库的初始化函数
    {
        var define = new NameFuncPair[]
        {
            new NameFuncPair("add", Add),
        };

        lua.L_NewLib(define);
        return 1;
    }

    public static int Add(ILuaState lua)
    {
        // 第一个参数
        var a = lua.L_CheckNumber(1);
        // 第二个参数
        var b = lua.L_CheckNumber(2);
        // 执行加法操作
        var c = a + b;
        // 将返回值入栈
        lua.PushNumber(c);
        // 有一个返回值
        return 1;
    }

    private int StoreMethod(string name)
    {
        Lua.GetField(-1, name);
        if (!Lua.IsFunction(-1))
        {
            throw new Exception(string.Format("method {0} not found!", name));
        }
        return Lua.L_Ref(LuaDef.LUA_REGISTRYINDEX);
    }

    private void CallMethod(int funcRef)
    {
        Lua.RawGetI(LuaDef.LUA_REGISTRYINDEX, funcRef);

        // insert `traceback' function
        var b = Lua.GetTop();
        Lua.PushCSharpFunction(Traceback);
        Lua.Insert(b);

        var status = Lua.PCall(0, 0, b);
        if (status != ThreadStatus.LUA_OK)
        {
            Debug.LogError(Lua.ToString(-1));
        }

        // remove `traceback' function
        Lua.Remove(b);
    }

    private static int Traceback(ILuaState lua)
    {
        var msg = lua.ToString(1);
        if (msg != null)
        {
            lua.L_Traceback(lua, msg, 1);
        }
        // is there an error object?
        else if (!lua.IsNoneOrNil(1))
        {
            // try its `tostring' metamethod
            if (!lua.L_CallMeta(1, "__tostring"))
            {
                lua.PushString("(no error message)");
            }
        }
        return 1;
    }
}

lantern.lua

--获取库
local test2 = require "Test2.cs"
local function add()
	print(test2.add(500,21))
end


return {
	add = add,
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

地狱为王

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值