From f8d4540cdf14fdf7044bdefe0ee57434453e61bd Mon Sep 17 00:00:00 2001 From: Tiny Date: Tue, 17 Aug 2021 20:57:10 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90Lua=E6=A1=86=E6=9E=B6=E3=80=91XluaMana?= =?UTF-8?q?ger=E7=AE=A1=E7=90=86=E5=99=A8=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Script/Xlua/XLuaManager.cs | 66 ++++++++++++++++++++++++++++--- 1 file changed, 61 insertions(+), 5 deletions(-) diff --git a/Assets/Script/Xlua/XLuaManager.cs b/Assets/Script/Xlua/XLuaManager.cs index 2ea23e3..3d8d2c0 100644 --- a/Assets/Script/Xlua/XLuaManager.cs +++ b/Assets/Script/Xlua/XLuaManager.cs @@ -11,8 +11,12 @@ namespace ZFramework.XLua { private LuaEnv _luaEnv = null; private LuaFunction _luatick = null; + private LuaFunction _luaDispatchClientEvent = null; + private LuaTable _luaEventDispatcher = null; private const string ServiceMgrScriptName = "ServiceModuleManager"; + private const string MainScriptName = "Main"; + private const string EventDispatcherScriptName = "EventDispatcher"; private void Start() { @@ -53,8 +57,13 @@ namespace ZFramework.XLua private void GameStartup() { - + LoadScript(ServiceMgrScriptName); _luatick = _luaEnv.Global.GetInPath($"package.loaded.{ServiceMgrScriptName}.Tick"); + LoadScript(MainScriptName); + _luaEventDispatcher = _luaEnv.Global.GetInPath($"package.loader.{EventDispatcherScriptName}"); + _luaDispatchClientEvent = + _luaEnv.Global.GetInPath( + $"package.loaded.{EventDispatcherScriptName}.DispatchClientEvent"); } /// @@ -62,10 +71,18 @@ namespace ZFramework.XLua /// public void LoadScript(string scriptName) { - SaveDoString($"require('{scriptName}')"); + SafeDoString($"require('{scriptName}')"); } - - private void SaveDoString(string scriptContent) + + /// + /// 清楚require的缓存 + /// + public void CleanRequiredCache(string scriptName) + { + SafeDoString($"package.loaded['{scriptName}'] = nil"); + } + + private void SafeDoString(string scriptContent) { if (_luaEnv!= null) { @@ -84,11 +101,50 @@ namespace ZFramework.XLua private void Update() { - if (_luaEnv != null) + if (_luatick != null) { _luatick.Action(Time.deltaTime,Time.unscaledTime); } } + + protected override void OnDestroy() + { + CleanUp(); + base.OnDestroy(); + } + + //清理虚拟机 + private void CleanUp() + { + if (_luaEnv!= null) + { + CleanRequiredCache(AssetManager.LuaPackageName); + if (_luatick != null) + { + _luatick.Dispose(); + _luatick = null; + } + + if (_luaEventDispatcher!=null) + { + _luaEventDispatcher.Dispose(); + _luaEventDispatcher = null; + } + + if (_luaDispatchClientEvent!= null) + { + _luaDispatchClientEvent.Dispose(); + _luaDispatchClientEvent = null; + } + + if (_luaEnv != null) + { + _luaEnv.Dispose(); + _luaEnv = null; + } + + } + } } } -- GitLab