Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

Commit 158fdca

Browse files
committed
初步测试xlua的流程
1 parent 5700f46 commit 158fdca

File tree

10 files changed

+127
-5
lines changed

10 files changed

+127
-5
lines changed

Assets/Game/HotFix.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Game/HotFix/Main.lua.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function Start()
2+
print("lua start")
3+
end
4+
5+
function Update()
6+
print("lua update...")
7+
end
8+
9+
function Close()
10+
print("lua close")
11+
end

Assets/Game/HotFix/Main.lua.txt.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Game/Main.asmdef

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"name": "Main",
33
"references": [
44
"GameFramework",
5-
"ThirdParty"
5+
"ThirdParty",
6+
"xLua"
67
],
78
"optionalUnityReferences": [],
89
"includePlatforms": [],

Assets/Game/Scripts/Base/GameMode.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class GameMode : MonoBehaviour
2828
public static SystemManager System;
2929
public static NetworkManager Network;
3030
public static PoolManager Pool;
31+
public static HotFixManager HotFix;
3132

3233
/// <summary>
3334
/// 当前程序集
@@ -78,6 +79,7 @@ IEnumerator Start()
7879
System= GameFrameworkMode.GetModule<SystemManager>();
7980
Network= GameFrameworkMode.GetModule<NetworkManager>();
8081
Pool = GameFrameworkMode.GetModule<PoolManager>();
82+
HotFix = GameFrameworkMode.GetModule<HotFixManager>();
8183
#endregion
8284

8385
#region resource
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright>
3+
// Copyright (c) 2018 Zhang Yang. All rights reserved.
4+
// </copyright>
5+
// <describe> #加载热更新状态# </describe>
6+
// <email> [email protected] </email>
7+
// <time> #2019年2月4日 16点10分# </time>
8+
//-----------------------------------------------------------------------
9+
10+
using System.Collections;
11+
using System.Collections.Generic;
12+
using UnityEngine;
13+
14+
namespace GameFramework.Taurus
15+
{
16+
[GameState]
17+
public class LoadHotfixState:GameState
18+
{
19+
#region 属性
20+
private string _luaScriptPath = "Assets/Game/HotFix/Main.lua.txt";
21+
#endregion
22+
23+
24+
public override void OnInit()
25+
{
26+
}
27+
28+
public override void OnEnter(params object[] parameters)
29+
{
30+
string luaScript = GameMode.Resource.LoadAsset<TextAsset>("hotfix", _luaScriptPath).text;
31+
GameMode.HotFix.LoadHotFix(luaScript);
32+
}
33+
34+
public override void OnExit()
35+
{
36+
}
37+
38+
public override void OnUpdate()
39+
{
40+
}
41+
42+
public override void OnFixedUpdate()
43+
{
44+
}
45+
}
46+
}

Assets/Game/Scripts/State/LoadHotfixState.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Game/Scripts/State/PreloadState.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public class PreloadState : GameState
1818
public override void OnEnter(params object[] parameters)
1919
{
2020
base.OnEnter(parameters);
21+
UnityEngine.Debug.Log("Preload -- > HotFix");
22+
//测试 直接切换到热更新状态里面去
23+
ChangeState<LoadHotfixState>();
2124
}
2225

2326
public override void OnExit()

Assets/GameFramework/Framework.asmdef

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"name": "GameFramework",
33
"references": [
4-
"ThirdParty"
4+
"ThirdParty",
5+
"xLua"
56
],
67
"optionalUnityReferences": [],
78
"includePlatforms": [],

Assets/GameFramework/HotFix/HotFixManager.cs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,50 @@
77
// <time> #2019年2月4日 15点11分# </time>
88
//-----------------------------------------------------------------------
99

10+
using System;
1011
using System.Collections;
1112
using System.Collections.Generic;
13+
using XLua;
1214

1315
namespace GameFramework.Taurus
1416
{
15-
public sealed class HotFixManager:GameFrameworkModule
17+
public sealed class HotFixManager:GameFrameworkModule,IUpdate
1618
{
17-
19+
//lua的环境变量
20+
private LuaEnv _luaEnv = new LuaEnv();
21+
private LuaTable _scriptEnv;
1822

19-
public override void OnClose()
23+
Action _start;
24+
Action _update;
25+
Action _close;
26+
27+
public void LoadHotFix(string luaScript)
2028
{
29+
_scriptEnv = _luaEnv.NewTable();
30+
31+
// 为每个脚本设置一个独立的环境,可一定程度上防止脚本间全局变量、函数冲突
32+
LuaTable meta = _luaEnv.NewTable();
33+
meta.Set("__index", _luaEnv.Global);
34+
_scriptEnv.SetMetaTable(meta);
35+
meta.Dispose();
36+
37+
_scriptEnv.Set("self", this);
38+
_luaEnv.DoString(luaScript, "HotFixManager", _scriptEnv);
39+
_scriptEnv.Get("Start", out _start);
40+
_scriptEnv.Get("Update", out _update);
41+
_scriptEnv.Get("Close", out _close);
42+
43+
_start?.Invoke();
2144
}
2245

46+
public void OnUpdate()
47+
{
48+
_update?.Invoke();
49+
}
50+
51+
public override void OnClose()
52+
{
53+
_close?.Invoke();
54+
}
2355
}
2456
}

0 commit comments

Comments
 (0)