Skip to content

Commit 8172f8d

Browse files
committed
xlua custom loader
1 parent 4d61eac commit 8172f8d

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

Assets/Game/Scripts/State/LoadHotfixState.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,14 @@ namespace GameFramework.Taurus
1616
[GameState]
1717
public class LoadHotfixState:GameState
1818
{
19-
#region 属性
20-
private string _luaScriptPath = "Assets/Game/HotFix/Main.lua.txt";
21-
#endregion
22-
23-
19+
2420
public override void OnInit()
2521
{
2622
}
2723

2824
public override void OnEnter(params object[] parameters)
2925
{
30-
string luaScript = GameMode.Resource.LoadAsset<TextAsset>("hotfix", _luaScriptPath).text;
31-
GameMode.HotFix.LoadHotFix(luaScript);
26+
GameMode.HotFix.LoadHotFix();
3227
}
3328

3429
public override void OnExit()

Assets/GameFramework/HotFix/HotFixManager.cs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System;
1111
using System.Collections;
1212
using System.Collections.Generic;
13+
using UnityEngine;
1314
using XLua;
1415

1516
namespace GameFramework.Taurus
@@ -20,12 +21,34 @@ public sealed class HotFixManager:GameFrameworkModule,IUpdate
2021
private LuaEnv _luaEnv = new LuaEnv();
2122
private LuaTable _scriptEnv;
2223

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+
2333
Action _start;
2434
Action _update;
2535
Action _close;
2636

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")
2846
{
47+
_resource = GameFrameworkMode.GetModule<ResourceManager>();
48+
_luaAssetBundle = assetBundle;
49+
_luaPathPrefix = luaPathPrefix;
50+
_luaPathExtension = luaPathExtension;
51+
2952
_scriptEnv = _luaEnv.NewTable();
3053

3154
// 为每个脚本设置一个独立的环境,可一定程度上防止脚本间全局变量、函数冲突
@@ -35,14 +58,22 @@ public void LoadHotFix(string luaScript)
3558
meta.Dispose();
3659

3760
_scriptEnv.Set("self", this);
38-
_luaEnv.DoString(luaScript, "HotFixManager", _scriptEnv);
61+
_luaEnv.AddLoader(CustomLoader);
62+
_luaEnv.DoString($"require '{luaScript}'", "LuaHotFix", _scriptEnv);
3963
_scriptEnv.Get("Start", out _start);
4064
_scriptEnv.Get("Update", out _update);
4165
_scriptEnv.Get("Close", out _close);
4266

4367
_start?.Invoke();
4468
}
4569

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+
4677
public void OnUpdate()
4778
{
4879
_update?.Invoke();
@@ -51,6 +82,7 @@ public void OnUpdate()
5182
public override void OnClose()
5283
{
5384
_close?.Invoke();
85+
//_luaEnv?.Dispose();
5486
}
5587
}
5688
}

0 commit comments

Comments
 (0)