10
10
using System ;
11
11
using System . Collections ;
12
12
using System . Collections . Generic ;
13
+ using UnityEngine ;
13
14
using XLua ;
14
15
15
16
namespace GameFramework . Taurus
@@ -20,12 +21,34 @@ public sealed class HotFixManager:GameFrameworkModule,IUpdate
20
21
private LuaEnv _luaEnv = new LuaEnv ( ) ;
21
22
private LuaTable _scriptEnv ;
22
23
24
+ //资源管理器
25
+ private ResourceManager _resource ;
26
+ //lua assetbundle 名称
27
+ private string _luaAssetBundle ;
28
+ //lua脚本前缀
29
+ private string _luaPathPrefix ;
30
+ //lua脚本扩展名
31
+ private string _luaPathExtension ;
32
+
23
33
Action _start ;
24
34
Action _update ;
25
35
Action _close ;
26
36
27
- public void LoadHotFix ( string luaScript )
37
+ /// <summary>
38
+ /// 加载热更新脚本
39
+ /// </summary>
40
+ /// <param name="assetBundle"></param>
41
+ /// <param name="luaScript"></param>
42
+ /// <param name="luaPathPrefix"></param>
43
+ /// <param name="luaPathExtension"></param>
44
+ public void LoadHotFix ( string assetBundle = "hotfix" , string luaScript = "main" ,
45
+ string luaPathPrefix = "Assets/Game/HotFix" , string luaPathExtension = ".lua.txt" )
28
46
{
47
+ _resource = GameFrameworkMode . GetModule < ResourceManager > ( ) ;
48
+ _luaAssetBundle = assetBundle ;
49
+ _luaPathPrefix = luaPathPrefix ;
50
+ _luaPathExtension = luaPathExtension ;
51
+
29
52
_scriptEnv = _luaEnv . NewTable ( ) ;
30
53
31
54
// 为每个脚本设置一个独立的环境,可一定程度上防止脚本间全局变量、函数冲突
@@ -35,14 +58,22 @@ public void LoadHotFix(string luaScript)
35
58
meta . Dispose ( ) ;
36
59
37
60
_scriptEnv . Set ( "self" , this ) ;
38
- _luaEnv . DoString ( luaScript , "HotFixManager" , _scriptEnv ) ;
61
+ _luaEnv . AddLoader ( CustomLoader ) ;
62
+ _luaEnv . DoString ( $ "require '{ luaScript } '", "LuaHotFix" , _scriptEnv ) ;
39
63
_scriptEnv . Get ( "Start" , out _start ) ;
40
64
_scriptEnv . Get ( "Update" , out _update ) ;
41
65
_scriptEnv . Get ( "Close" , out _close ) ;
42
66
43
67
_start ? . Invoke ( ) ;
44
68
}
45
69
70
+ //自定义加载
71
+ private byte [ ] CustomLoader ( ref string filePath )
72
+ {
73
+ string path = System . IO . Path . Combine ( _luaPathPrefix , $ "{ filePath } .lua.txt") ;
74
+ return _resource . LoadAsset < TextAsset > ( _luaAssetBundle , path ) . bytes ;
75
+ }
76
+
46
77
public void OnUpdate ( )
47
78
{
48
79
_update ? . Invoke ( ) ;
@@ -51,6 +82,7 @@ public void OnUpdate()
51
82
public override void OnClose ( )
52
83
{
53
84
_close ? . Invoke ( ) ;
85
+ //_luaEnv?.Dispose();
54
86
}
55
87
}
56
88
}
0 commit comments