Tolua Examples 1 - 4 HelloWorld , Dofile & Require , CallLuaFunction , AccessingLuaVariables

这篇博客展示了如何在Unity中使用tolua库与Lua进行交互。通过几个实例,包括HelloWorld、dofile与require的区别、调用Lua函数以及访问Lua变量,详细解释了tolua在C#中的使用方法,如加载并执行Lua脚本、调用Lua函数、设置和读取Lua全局变量等。
摘要由CSDN通过智能技术生成

Examples:

 

1 HelloWorld

using UnityEngine;

using LuaInterface;

using System;

 

public class HelloWorld : MonoBehaviour

{

    void Awake()

    {

        LuaState lua = new LuaState();

        //lua_State 中放的是 lua 虚拟机中的环境表、注册表、运行堆栈、虚拟机的上下文等数据。

        //从一个主线程(特指 lua 虚拟机中的线程,即 coroutine)中创建出来的新的 lua_State 会共享大部分数据,但会拥有一个独立的运行堆栈。所以一个线程对象拥有一个lua_State

        //Lua_State细节内容:

        //https://blog.csdn.net/chenjiayi_yun/article/details/24304607

 

 

        lua.Start();

        string hello =

            @"               

                print('hello tolua#')                                 

            ";

       

        lua.DoString(hello, "HelloWorld.cs");//加载并执行?

 

        lua.CheckTop();//销毁前调用了CheckTop,用于检测LUA栈中是否还有未执行的指令

 

        lua.Dispose();

        lua = null;

    }

}

 

2 Dofile & Require

 using UnityEngine;

using System.Collections;

using LuaInterface;

using System;

using System.IO;

 

//展示searchpath 使用,require dofile 区别

public class ScriptsFromFile : MonoBehaviour

{

    LuaState lua = null;

    private string strLog = "";   

 

    void Start ()

    {

#if UNITY_5 || UNITY_2017 || UNITY_2018    

        Application.logMessageReceived += Log;

#else

        Application.RegisterLogCallback(Log);

#endif        

        lua = new LuaState();                

        lua.Start();       

        //如果移动了ToLua目录,自己手动修复吧,只是例子就不做配置了

        string fullPath = Application.dataPath + "\\ToLua/Examples/02_ScriptsFromFile";

        lua.AddSearchPath(fullPath);       

    }

 

    void Log(string msg, string stackTrace, LogType type)

    {

        strLog += msg;

        strLog += "\r\n";

    }

 

    void OnGUI()

    {

        GUI.Label(new Rect(100, Screen.height / 2 - 100, 600, 400), strLog);//打印到GUI

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值