找回密码
 立即注册
查看: 941|回复: 0

[热更] u3dchina_Unity_tolua基础教学2_c#调lua

[复制链接]
发表于 2020-3-9 14:34 | 显示全部楼层 |阅读模式
资源信息 Tutorial Information
教程名称: u3dchina_Unity_tolua基础教学2_c#调lua(发帖教程)
适用引擎: Unity3D  (适用引擎,为空默认为Unity)
教程语种: 中文
教程等级: 1
教程格式: 图文(请用IE9以上浏览器访问本版块)
教程作者: 转载自互联网 (如有问题请短消息联系作者或发表回复)
下载地址: (兑换积分)
点击查看原图
美丽分割线
demo中有详细的例子说明:

====c#调用tolua静态方法
我们来自己写一个:
新建一个TestLua.cs脚本,脚本如下:
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using LuaInterface;
  5. using System;

  6. public class TestLua : MonoBehaviour
  7. {
  8.     private LuaState luaState;

  9.     private LuaFunction luaFunc = null;//lua函数
  10.     private string strLog = "";
  11.     // Start is called before the first frame update
  12.     void Start()
  13.     {
  14.         luaState= new LuaState(); // 创建lua虚拟机
  15.         Application.RegisterLogCallback(Log);
  16.          luaState.Start();
  17.         string fullPath = Application.dataPath + "\\ToLua/Test";
  18.         luaState.AddSearchPath(fullPath);
  19.     }
  20.     void Log(string msg, string stackTrace, LogType type)
  21.     {
  22.         strLog += msg;
  23.         strLog += "\r\n";
  24.     }
  25.     //自定义功能函数,将被注册到lua虚拟机中  
  26.     public string CSharpMethod(int num)   
  27.     {
  28.         return string.Format("Hello World {0} !" , num+1);
  29.     }
  30.     private void OnGUI()
  31.     {
  32.         GUI.Label(new Rect(100, Screen.height / 2 - 100, 600, 400), strLog);
  33.         if (GUI.Button(new Rect(50, 50, 120, 45), "DoFile"))
  34.         {
  35.             strLog = "";
  36.             luaState.DoFile("TestLua.lua");  
  37.             luaFunc = luaState.GetFunction("test.luaFunc");//这里test 可以去掉
  38.             if (luaFunc!=null)
  39.             {
  40.                 // 加载完文件后,使用GetFunction获取lua脚本中的函数,再调用Call执行。  
  41.                 int  num = luaState.Invoke<int, int>("test.luaFunc", 123456, true);
  42.                 Debugger.Log("c#调用lua", num);
  43.             }

  44.            
  45.             
  46.             
  47.         }
  48.         else if (GUI.Button(new Rect(50, 150, 120, 45), "Require"))
  49.         {
  50.             strLog = "";            
  51.             luaState.Require("TestLua");            
  52.         }
  53.         
  54.         luaState.Collect();
  55.         luaState.CheckTop();
  56.     }
  57.     void OnApplicationQuit()
  58.     {
  59.         luaState.Dispose();
  60.         luaState = null;
  61. #if UNITY_5 || UNITY_2017 || UNITY_2018        
  62.         Application.logMessageReceived -= Log;
  63. #else
  64.         Application.RegisterLogCallback(null);
  65. #endif
  66.     }
  67.     // Update is called once per frame
  68.     void Update()
  69.     {
  70.         
  71.     }
  72. }
复制代码
注意路径要指定,在新建一个lua文件为TestLua.lua,内容为:
  1. function luaFunc(num)
  2.     return num + 1
  3. end

  4. test = {}
  5. test.luaFunc = luaFunc
复制代码
这里讲解了通过Invoke的方法调用lua代码。
--
在C#中加载lua文件(LuaState.DoFile)
拿到目标函数(LuaState.GetFunction)
通过Invoke调用目标函数lua中静态方法test.luaFunc

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-4-27 09:21 , Processed in 0.098091 second(s), 31 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表