|
本文基于tolua里C#与lua互调,在此基础上添加了对象的注册。具体看代码:- using System;
- using UnityEngine;
- using UnityEditor;
- using LuaInterface;
- public class LuaBehaviour : MonoBehaviour
- {
- public string LuaFullPath;
- public string LuaName;
- private LuaState m_Lua;
- private int m_ClassRef = -1;//the lua class ref
- private int m_BindLuaRef = -1;//the lua class bind class ref
- private int m_StartRef = -1;//the 'Start' function of lua
- private int m_OnDestroyRef = -1;// the 'OnDestroy' function of lua
- public GameObject tempObj;
- public LuaBehaviour()
- {
- }
- void Awake()
- {
- m_Lua = LuaInstance.Instance.LuaState;
- if (!m_Lua.LuaDoFile(GameConfig.LuaPath + LuaFullPath))
- {
- Debug.LogError("load file failed, file name is " + LuaName);
- }
- m_Lua.LuaNewTable();
- m_Lua.LuaPushValue(-1);
- m_BindLuaRef = m_Lua.ToLuaRef();
- m_Lua.LuaRawSetI(LuaIndexes.LUA_REGISTRYINDEX, m_BindLuaRef);
- m_Lua.LuaGetGlobal(LuaName);
- m_ClassRef = m_Lua.ToLuaRef();
- if (m_ClassRef == LuaDLL.LUA_REFNIL)
- {
- Debug.LogError("not find the ref object");
- }
- m_Lua.LuaGetGlobal(LuaName);
- m_Lua.LuaRawSetI(LuaIndexes.LUA_REGISTRYINDEX, m_ClassRef);
- m_Lua.LuaRawGetI(LuaIndexes.LUA_REGISTRYINDEX, m_ClassRef);
- m_Lua.LuaRawGetI(LuaIndexes.LUA_REGISTRYINDEX, m_BindLuaRef);
- m_Lua.LuaSetMetaTable(-2);
- m_Lua.LuaGetGlobal(LuaName);
- m_Lua.LuaGetField(-1, "Start");
- m_StartRef = m_Lua.ToLuaRef();
- if (m_StartRef == LuaDLL.LUA_REFNIL)
- {
- Debug.LogError("not find the ref object");
- }
- m_Lua.LuaGetField(-1, "Start");
- m_Lua.LuaRawSetI(LuaIndexes.LUA_REGISTRYINDEX, m_StartRef);
- m_Lua.LuaGetField(-1, "OnDestroy");
- m_OnDestroyRef = m_Lua.ToLuaRef();
- if (m_OnDestroyRef == LuaDLL.LUA_REFNIL)
- {
- Debug.LogError("not find the ref object");
- }
- m_Lua.LuaGetField(-1, "OnDestroy");
- m_Lua.LuaRawSetI(LuaIndexes.LUA_REGISTRYINDEX, m_OnDestroyRef);
- RegisterParams();
- }
- void Start()
- {
- m_Lua.LuaRawGetI(LuaIndexes.LUA_REGISTRYINDEX, m_StartRef);
- m_Lua.LuaRawGetI(LuaIndexes.LUA_REGISTRYINDEX, m_ClassRef);
- m_Lua.PCall(1, 0);
- m_Lua.LuaPop(1);
- }
- void OnDestroy()
- {
- m_Lua.LuaRawGetI(LuaIndexes.LUA_REGISTRYINDEX, m_OnDestroyRef);
- m_Lua.PCall(0, 0);
- m_Lua.LuaPop(1);
- }
- private void RegisterParams()
- {
- m_Lua.LuaRawGetI(LuaIndexes.LUA_REGISTRYINDEX, m_ClassRef);
- m_Lua.PushObject(tempObj);
- m_Lua.LuaSetField(-2, "tempObj");
- }
- }
复制代码 在上一篇的基础上,我添加了一个LuaInstance类用于管理LuaState的使用,使用RegisterParam方法将C#中的对象注册到lua,并在lua中调用。本功能主要是解决NGUI对象的动态添加问题,比方,以后游戏上线后,可能会在预制体中添加个label,但是我们不能更新C#,所以只能通过动态注册到lua中。 lua中的使用- --Created By: xikuo yu
- --Time: 2016-12-24
- FirstLua = {}
- setmetatable(FirstLua,{})
- function FirstLua:Start()
- print("FirstLua Start")
- if self.tempObj ~= nil then
- print("tempObj is not nil")
- print( self.tempObj.tag )
- self.tempObj:SetActive(false);
- else
- print("tempObj is nil")
- end
- end
- function FirstLua:OnDestroy()
- print("FirstLua OnDestroy")
- end
- function FirstLua.Function1()
- if self == nil then
- print("FirstLua Function1 self is nil")
- else
- print("FirstLua Function1")
- end
- end
- function Function2()
- print("Function2")
- end
复制代码 关于调用:
tolua为我们将类的属性添加了get与set,在lua中使用时,通过点操作符,获取C#注册过来的对象,使用分号操作符,获取C#中Wraper文件中注册的方法。
以后可能打算用反射的方式来添加方法的注册,使热更新做的更彻底,缺点是性能会有损失。
结果:
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|