tolua与c#的互相调用

15 篇文章 0 订阅
13 篇文章 0 订阅

https://blog.csdn.net/fjjaylz/article/details/86578489

 


using LuaInterface;
using UnityEngine;
 
public class LuaAccess : MonoBehaviour
{
    string luaFile = "LuaAccess";
    LuaState state;
 
 
    void Start()
    {
        state = new LuaState();
        state.Start();
 
        //使用文件调用Lua
        //手动添加一个lua文件搜索地址
        string sceneFile = Application.dataPath + "/LuaStudy";
        state.AddSearchPath(sceneFile);
 
        state.Require(luaFile);//载入文件
 
        //获取Lua变量
        Debug.Log("获取文件中变量:" + state["num"]);
        state["num"] = 10;
        Debug.Log("设置文件中变量为:" + state["num"]);
 
        //调用Lua方法
        LuaFunction luaFunc = state.GetFunction("Count");
        luaFunc.Call();
        Debug.Log("C#调用LuaFunc,函数返回值:" + state["num"]);
 
        Debug.Log("C#直接调用Count方法。");
        state.Call("Count", false);
 
 
        //对方法传入参数
        LuaFunction valueFunc = state.GetFunction("InputValue");
        valueFunc.BeginPCall();
        valueFunc.Push("--push方法从C#中传入参数--");
        valueFunc.PCall();
        valueFunc.EndPCall();
 
        valueFunc.Call("--直接Call方法从C#传入参数--");
 
 
        //获取LuaTable
        LuaTable table = state.GetTable("mytable");
        table.Call("tableFunc");
        LuaFunction tableFunc = table.GetLuaFunction("tableFunc");
        Debug.Log("C#调用table中的func");
        tableFunc.Call();
 
        Debug.Log("获取table中的num值:"+table["num"]);
 
        //通过下标直接获取
        for (int i = 0; i < table.Length; i++)
        {
            Debug.Log("获取table的值:" + table[i]);
        }
 
 
        //转换成LuaDictTable
        LuaDictTable dicTable = table.ToDictTable();
        foreach (var item in dicTable)
        {
            Debug.LogFormat("遍历table:{0}--{1}", item.Key, item.Value);
        }
 
 
        state.Dispose();
    }

————————————————
版权声明:本文为CSDN博主「达也酱」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/fjjaylz/article/details/86578489

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值