该代码是基于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();
}
}