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 () {
        
        }
    }

  • 相关阅读:
    #define中的特殊符号
    c++ windows下计时
    c++内存池
    u3d 场景资源打包
    Unity3d 动态加载材质方法
    Unity3D Shader基础教程
    Unity3D Shader入门指南(一)
    Unreal发展史
    阴影锥(shadow volume)原理与展望
    软硬RAID 和 不常见的RAID
  • 原文地址:https://www.cnblogs.com/ningyongbin/p/6009210.html
Copyright © 2011-2022 走看看