Crystal‘s Lua 封装调用类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using System.IO;
using XLua;


public class xLuaEnv
{
    private static xLuaEnv _instance = null;

    private xLuaEnv()
    {
        _env = new LuaEnv();
        _env.AddLoader(ProjectLoader);
    }

    public static xLuaEnv Instance
    {
        get
        {
            if(_instance == null)
            {
                _instance = new xLuaEnv();
            }
            return _instance;
        }
    }

    private LuaEnv _env;
    public byte[] ProjectLoader(ref string filepath)
    {
        //filepath是从Lua的require("路径")
        //在项目的Assets的同级目录下创建一个可写文件夹(DataPath)
        //通过构造路径,将require加载的文件夹指向我们想要的路径
        string path = Application.dataPath;
        path = path.Substring(0, path.Length - 7) + "/DataPath/Lua" + filepath + ".lua";
        Debug.Log(path);
        if (File.Exists(path))
        {
            //将Lua文件读取为字节数组,xLua的环境会根据这个数组去执行Lua代码
            return File.ReadAllBytes(path);
        }
        else
        {
            return null;
        }
    }

    //用于释放Lua解析器,同时销毁_instance对象
    public void Free()
    {
        _env.Dispose();
        _instance = null;
    }

    /// <summary>
    /// 用于执行Lua代码
    /// </summary>
    /// <param name="code">要执行的Lua代码</param>
    /// <returns>Lua代码的返回值</returns>
    public object[] RunLua(string code)
    {
        return _env.DoString(code);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值