虚拟机
using UnityEngine;
using System;
using System.IO;
using XLua;
public class LuaStart : MonoBehaviour
{
//new一个虚拟机对象
LuaEnv lua = new LuaEnv();
//start update事件
public Action action_Start, action_Update;
void Start()
{
//回调
lua.AddLoader(OnCustom);
//加载主代码
lua.DoString("require'Mainlua'");
//给事件赋值
action_Start = lua.Global.GetInPath<Action>("Start");
action_Update = lua.Global.GetInPath<Action>("Update");
//执行start事件
if (action_Start != null)
action_Start();
}
private byte[] OnCustom(ref string filepath)
{
//获取到lua代码所在路径
string path = Application.dataPath + "/LuaScripts/" + filepath + ".lua";
return File.ReadAllBytes(path);
}
void Update()
{
//执行update事件
if (action_Update != null)
action_Update();
}
}
lua
-- 引用
require("Lipus/BaseClass")
require("Lipus/head")
function Start()
end
function Update()
end