zoukankan      html  css  js  c++  java
  • lua-路径加载lua文件-函数返回值,访问lua文件中的变量

    lua文件如下:

    print("ddhdhh")


    function login(username,pswd)
    if username =="ms" and pswd=="ms" then
    print('用户存在')
    return true;

    else
    print('用户不存在')
    return false

    end
    end


    user = {id = "010101",LastLoginTime= "2016-5"}
    version = "12.11"

    c#代码调用上述lua文件的内容:

    using UnityEngine;
    using System.Collections;
    using LuaInterface;
    public class MyScriptsFromFile : MonoBehaviour {

        LuaState lua;
        string serchPath;
        // Use this for initialization
        void Start () {
            serchPath = Application.dataPath + "/0My/02-ScriptsFromFile";//lua文件的路径(不包括文件名)
            lua = new LuaState();
            lua.Start();
            //添加搜索路径
            lua.AddSearchPath(serchPath);
           // lua.Require("02");   //用require和dofile都行,注意参数的区别
            lua.DoFile("02.lua");
            LuaFunction func = lua.GetFunction("login");//调用login函数
            func.BeginPCall();
            func.Push("ms");
            func.Push("mss");
            func.PCall();
            
            //取函数返回值,布尔类型
            bool isOk =  func.CheckBoolean();
            func.EndPCall();
            if (isOk)
            {
                //访问lua变量  非table类型变量
                print(lua["version"]);
                //访问lua变量  table类型变量
                LuaTable user = lua.GetTable("user");//获取usertable表
                print(user["id"]);//获取表中的id变量
            }
            else
            {
                print("用户不存在");
            }
            
            lua.CheckTop();
            lua.Dispose();

        }
        
        // Update is called once per frame
        void Update () {
        
        }
    }

  • 相关阅读:
    nginx 配置https
    linux 文件上传下载
    linux系统搭建ftp服务器及创建用户使用
    Centos7.3防火墙配置
    CentOS7搭建svn部署项目
    工作中总结的常用PHP代码
    Git查看、创建、上传SSH密钥
    run `npm fund` for details found 16 vulnerabilities (2 low, 8 moderate, 6 high) run `npm audit fix` to fix them, or `npm audit` for details
    获取官方节假日数据的api接口,获取指定日期的节假日数据
    vue-elementUi项目打包后样式入坑
  • 原文地址:https://www.cnblogs.com/ningyongbin/p/6009210.html
Copyright © 2011-2022 走看看