有空看了下lua,研究下Xlua。 笔记如下
01_Helloworld
void Start ()
{
LuaEnv luaEnv = new XLua.LuaEnv();
luaEnv.DoString("CS.UnityEngine.Debug.Log('hellow world')");
luaEnv.Dispose();
}
1、DoString参数为string,可输入任意合法的Lua代码,本示例在lua里调用C#的UnityEngine.Debug.Log打印了个日志。
2、一个LuaEnv实例对应Lua虚拟机,出于开销的考虑,建议全局唯一。
考虑全局唯一,新建LuaManager测试代码。
using System;
using System.Collections;
using System.Collections.Generic;
using MyFrameWork;
using UnityEngine;
using XLua;
public class LuaManager : MonoSingleton<LuaManager>
{
private static LuaEnv _globleLuaEnv;
public static LuaEnv GlobleLuaEnv {
get
{
if (_globleLuaEnv==null)
{
_globleLuaEnv=new LuaEnv();
}
return _globleLuaEnv;
}
}
// Use this for initialization
void Awake () {
DontDestroyOnLoad(this);
GlobleLuaEnv.DoString("CS.UnityEngine.Debug.Log('hellowWorld')");
GlobleLuaEnv.Dispose();
Debug.Log(_globleLuaEnv == null);
}
// Update is called once per frame
void Update () {
Debug.Log(_globleLuaEnv == null);
if (Input.anyKeyDown)
{
GC.Collect();
}
}
}
可以看到 测试结果