您的位置:首页 > 移动开发 > Unity3D

Unity使用C#通过自定义Loader加载指定目录的Lua脚本

2018-03-06 23:16 811 查看
该代码是基于XLua,XLua插件下载链接:https://github.com/Tencent/xLua

代码如下:using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;

public class CreateLoader : MonoBehaviour {

private LuaEnv m_luaEnv;

// Use this for initialization
void Awake () {

m_luaEnv = new LuaEnv();

m_luaEnv.AddLoader(MyLoader);
m_luaEnv.DoString("require 'test001'");
}

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

}

/// <summary>
/// 自定义loader,如果返回为null则调用内置的loader
/// </summary>
/// <param name="_filePath"></param>
/// <returns></returns>
private byte[] MyLoader(ref string _filePath)
{
string luaPath = Application.streamingAssetsPath + "/" + _filePath + ".lua.txt";

return System.Text.Encoding.UTF8.GetBytes(System.IO.File.ReadAllText(luaPath));
}

private void OnDestroy()
{
m_luaEnv.Dispose();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐