码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 使用自定义委托来调用Lua中的多返回值和长参数类型函数


    合集 - Unity学习笔记(19)
    1.Unity学习笔记--基础2023-11-012.Unity学习笔记--入门2023-10-293.Unity学习笔记--数据持久化之PlayerPrefs的使用2023-11-194.Unity学习笔记--数据持久化XML文件(1)2023-11-205.Unity学习笔记--数据持久化XML文件(2)2023-12-016.Unity学习笔记--数据持久化Json2023-12-037.NGUI学习笔记(1)03-018.NGUI学习笔记203-039.NGUI学习笔记3.503-0510.NGUI学习笔记4.003-0611.Unity 热更--AssetBundle学习笔记 0.705-0112.Unity 热更--AssetBundle学习笔记 0.805-0213.Unity 热更--AssetBundle学习笔记 1.0【AB包资源加载工具类的实现】05-0314.[2]自定义Lua解析方式05-0615.Unity热更学习toLua使用--[1]toLua的导入和默认加载执行lua脚本05-0716.自定义Lua解析器管理器-------演化脚本V0.505-08
    17.使用自定义委托来调用Lua中的多返回值和长参数类型函数05-09
    18.使用自定义lua解析管理器调用lua脚本中的table05-1019.Lua热更学习--使用toLua中的协程05-12
    收起

    使用自定义lua解析管理器调用函数

    使用自定义委托来调用lua脚本中的多返回值函数和长参数类型的函数。

    先看代码,依旧是上篇文章中所贴的脚本。新增调用两个函数testFunc

    using System;
    using BaseFramework;
    using LuaInterface;
    using UnityEngine;
    using UnityEngine.Events;
    using Object = System.Object;
    namespace CallLua
    {
    public class CallLuaEntrance:MonoBehaviour
    {
    //+ 委托
    public delegate int CustomCallFunc(int a, out int b, out int c, out string d, out bool e);
    public delegate void CustomCallParams(int a, params Object[] objects);
    private void Start()
    {
    CallLuaManager.Instance().Init();
    CallLuaManager.Instance().Require("Main");
    //获取全局变量
    Debug.Log(CallLuaManager.Instance().LuaState["string1"]);
    //无法获取lua脚本中的局部变量
    CallLuaManager.Instance().LuaState["string1"] = "我被修改了!";
    Debug.Log(CallLuaManager.Instance().LuaState["string1"]);
    //可以理解LuaState中存储的所有全局变量列表
    //如果有则可以查看并修改
    //如果没有则新建
    CallLuaManager.Instance().LuaState["newGloString"] = "我是新来的,是Lua全局变量";
    //获取执行无参无返回值的lua函数
    LuaFunction luaFunction = CallLuaManager.Instance().LuaState.GetFunction("testFunc");
    luaFunction.Call();
    luaFunction.Dispose();
    //直接获取
    luaFunction = CallLuaManager.Instance().LuaState["testFunc"] as LuaFunction;
    luaFunction.Call();
    luaFunction.Dispose();
    //存入委托中再使用
    luaFunction = CallLuaManager.Instance().LuaState.GetFunction("testFunc");
    UnityAction action = luaFunction.ToDelegate();
    action();
    //-------------------------------------------------------------------------------------------------
    //有参有返回值函数获取调用 方式1
    luaFunction = CallLuaManager.Instance().LuaState.GetFunction("testFunc1");
    luaFunction.BeginPCall();
    luaFunction.Push(66);
    luaFunction.PCall();
    int res = (int)luaFunction.CheckNumber();
    Debug.Log("参数为"+66+" ,返回值为"+res);
    luaFunction.EndPCall();
    //通过函数的Invoke方法来调用 方式2
    //<参数类型,返回值类型>
    res = luaFunction.Invoke<int, int>(88);
    Debug.Log("参数为"+88+" ,返回值为"+res);
    //通过委托调用 方式3
    Func<int, int> func = luaFunction.ToDelegateint, int>>();
    res = func(99);
    Debug.Log("参数为"+99+" ,返回值为"+res);
    //通过解析器直接调用 方式4 和2本质上是一样的掉用方式
    res = CallLuaManager.Instance().LuaState.Invoke<int, int>("testFunc1", 166, true);
    Debug.Log("参数为"+166+" ,返回值为"+res);
    //+ 新增内容
    //----------------------------多返回值函数----------------------------------------------------
    //001直接获取 执行结果 传统方式
    luaFunction = CallLuaManager.Instance().LuaState.GetFunction("testFunc2");
    luaFunction.BeginPCall();
    luaFunction.Push(566);
    luaFunction.PCall();
    int res1 = (int)luaFunction.CheckNumber();
    int res2 = (int)luaFunction.CheckNumber();
    int res3 = (int)luaFunction.CheckNumber();
    string res4 = luaFunction.CheckString();
    bool res5 = luaFunction.CheckBoolean();
    Debug.Log("多返回值函数数值结果--->"+res1+","+res2+","+res3+","+res4+","+res5);
    //002使用委托方式调用函数
    CustomCallFunc customCallFunc = luaFunction.ToDelegate();
    int b2, b3;
    string s2;
    bool bl;
    //注意 res接收第一个返回值 其它都按照out 变量赋值出
    int res0 = customCallFunc(788, out b2, out b3, out s2, out bl);
    Debug.Log("多返回值函数数值结果--->"+res0+","+b2+","+b3+","+","+s2+","+bl);
    //--------------------------------------------长参数函数调用--------------------------------
    luaFunction = CallLuaManager.Instance().LuaState.GetFunction("testFunc3");
    CustomCallParams customCallParams = luaFunction.ToDelegate();
    customCallParams(1, 2, "tony", true, 666.66);
    //也可以直接调用 call用来调用void 类型函数
    luaFunction.Call<int,bool,float,string>(56,false,88.88f,"Chang");
    luaFunction.Call(98,365,false,88.88f,"Chang");//不给泛型也可以!
    CallLuaManager.Instance().Dispose();
    }
    }
    }

    注意!在tolua中使用自定义委托时候,需要在Seting脚本中添加自定义委托,之后再重新Generate一下。

    image-20240509205251581

    image-20240509205422368

    要调用的Main.lua

    --主入口函数。从这里开始lua逻辑
    function Main()
    print("logic start")
    end
    Main()
    --场景切换通知
    function OnLevelWasLoaded(level)
    collectgarbage("collect")
    Time.timeSinceLevelLoad = 0
    end
    --全局变量
    string1 = "我是全局变量"
    function testFunc()
    print("无参无返回值函数调用成功!")
    end
    --有参数有返回值的函数
    function testFunc1(a)
    return a + 100
    end
    --多返回值函数
    function testFunc2(e)
    print("多返回值函数执行")
    return e,e+100,e+200,"yes!",true
    end
    --变长参数函数
    function testFunc3(a,...)
    print("变长参数函数---")
    print(a)
    args = {...}
    for k,v in pairs(args) do
    print(k,v)
    end
    end
    function OnApplicationQuit()
    end

    好了,现在自定义的lua解析管理器已经完善对lua中全局变量的访问修改和添加、以及多种函数类型的调用。

    先到这里了,接下来要接着完善管理器的功能,敬请期待!

  • 相关阅读:
    uniapp制作——交友盲盒
    构建React TodoList应用:管理你的任务清单
    前端Vue-then方法和catch方法
    Quarto 入门教程 (1):简单介绍和资料汇总
    redis 支持ipv6和ipv4设置方法
    Django channel 使用说明 -- 以聊天室为例(2)
    python - yield详解
    反射及暴力反射
    云原生之K8S------list-watch机制,调度约束以及故障排查
    Mac 环境下 Java JDK 的安装与环境变量配置详解(已完美解决)
  • 原文地址:https://www.cnblogs.com/TonyCode/p/18183121
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号