Xlua入门案例三(UI事件)

C#脚本如下:

[System.Serializable ]
public class Injectionzht
{
public string name;
public GameObject value;
}
[LuaCallCSharp ]
public class zht3 : MonoBehaviour {
#region 字段
public TextAsset luaScript;
public Injectionzht[] injectionszht;
internal static LuaEnv luaEnv = new LuaEnv ();
internal static float lastGCTime = 0;
internal const float GCInterval = 1;//1 second 


private Action luaStart;
private Action luaUpdate;
private Action luaOnDestroy;


private LuaTable scriptEnv;
#endregion
#region 属性
#endregion
#region Unity回调函数
void Awake()
{
scriptEnv = luaEnv.NewTable ();
LuaTable meta = luaEnv.NewTable();
meta.Set("__index", luaEnv.Global);
scriptEnv.SetMetaTable(meta);
meta.Dispose();


scriptEnv.Set("self", this);
foreach (var injection in injectionszht)
{
scriptEnv.Set(injection.name, injection.value);
}


luaEnv.DoString(luaScript.text, "LuaTestzht3", scriptEnv);


Action luaAwake = scriptEnv.Get("awake");
scriptEnv.Get("start", out luaStart);
scriptEnv.Get("update", out luaUpdate);
scriptEnv.Get("ondestroy", out luaOnDestroy);


if (luaAwake != null)
{
luaAwake();
}
}
void Start () {
if (luaStart != null)
{
luaStart();
}
}//Start ()_end

void Update () {
if (luaUpdate != null)
{
luaUpdate();
}
if (Time.time - LuaBehaviour.lastGCTime > GCInterval)
{
luaEnv.Tick();
LuaBehaviour.lastGCTime = Time.time;
}
}//Update ()_end
void OnDestroy()
{
if (luaOnDestroy != null)
{
luaOnDestroy();
}
luaOnDestroy = null;
luaUpdate = null;
luaStart = null;
scriptEnv.Dispose();
injectionszht = null;
}
#endregion
#region 方法
#endregion 
}

Lua脚本如下:


function start()
print("lua start...")
self:GetComponent("Button").onClick:AddListener(function()
print("clicked, you input is '" ..input:GetComponent("InputField").text .."'")
end)

end

挂脚本如下



你可能感兴趣的:(Unity3D,热更新(Lua))