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

Commit d2af6f5

Browse files
committed
增加luabehaviour
1 parent 23c492c commit d2af6f5

File tree

8 files changed

+141
-8
lines changed

8 files changed

+141
-8
lines changed

Assets/Game/HotFix/UI.lua.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
UI={}
2+
3+
-- 打开ui
4+
function UI:Open(name)
5+
6+
end
7+
8+
9+
--关闭ui
10+
function UI:Close(name,destory)
11+
12+
end

Assets/Game/HotFix/UI.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/Scripts/Editor/HotFixBuildEditor.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ private static void DelecteLuaTxtFile(object sender, FileSystemEventArgs e)
5050
private static void UpdateLuaTxtFile(object sender, FileSystemEventArgs e)
5151
{
5252
string path = Path.Combine(_hotFixPath, Path.GetFileName(e.Name) + ".txt");
53-
Debug.Log(path);
5453
File.Copy(e.FullPath, path, true);
5554
Debug.Log(".lua==>.lua.txt 转换完成");
5655
}

Assets/Game/Scripts/Lua/UI.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
UI={}
2+
3+
-- 打开ui
4+
function UI:Open(name)
5+
6+
end
7+
8+
9+
--关闭ui
10+
function UI:Close(name,destory)
11+
12+
end

Assets/Game/Scripts/Lua/UI.lua.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/GameFramework/HotFix/HotFixManager.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace GameFramework.Taurus
1818
public sealed class HotFixManager:GameFrameworkModule,IUpdate
1919
{
2020
//lua的环境变量
21-
private LuaEnv _luaEnv = new LuaEnv();
21+
public LuaEnv LuaEnv = new LuaEnv();
2222
private LuaTable _scriptEnv;
2323

2424
//资源管理器
@@ -55,17 +55,17 @@ public void LoadHotFix(string assetBundle="hotfix",string luaScript="main",
5555
_luaPathPrefix = luaPathPrefix;
5656
_luaPathExtension = luaPathExtension;
5757

58-
_scriptEnv = _luaEnv.NewTable();
58+
_scriptEnv = LuaEnv.NewTable();
5959

6060
// 为每个脚本设置一个独立的环境,可一定程度上防止脚本间全局变量、函数冲突
61-
LuaTable meta = _luaEnv.NewTable();
62-
meta.Set("__index", _luaEnv.Global);
61+
LuaTable meta = LuaEnv.NewTable();
62+
meta.Set("__index", LuaEnv.Global);
6363
_scriptEnv.SetMetaTable(meta);
6464
meta.Dispose();
6565

6666
_scriptEnv.Set("self", this);
67-
_luaEnv.AddLoader(CustomLoader);
68-
_luaEnv.DoString($"require '{luaScript}'", "LuaHotFix", _scriptEnv);
67+
LuaEnv.AddLoader(CustomLoader);
68+
LuaEnv.DoString($"require '{luaScript}'", "LuaHotFix", _scriptEnv);
6969
_scriptEnv.Get("Start", out _start);
7070
_scriptEnv.Get("Update", out _update);
7171
_scriptEnv.Get("Close", out _close);
@@ -88,7 +88,7 @@ public void OnUpdate()
8888
//每隔一段时间对lua进行一次GC回收
8989
if (Time.time - _lastGCTime > _luaTickInterval)
9090
{
91-
_luaEnv.Tick();
91+
LuaEnv.Tick();
9292
_lastGCTime = Time.time;
9393
}
9494
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright>
3+
// Copyright (c) 2018 Zhang Yang. All rights reserved.
4+
// </copyright>
5+
// <describe> #lua的运行脚本# </describe>
6+
// <email> [email protected] </email>
7+
// <time> #2019年2月7日 00点03分# </time>
8+
//-----------------------------------------------------------------------
9+
using System;
10+
using System.Collections;
11+
using System.Collections.Generic;
12+
using UnityEngine;
13+
using XLua;
14+
15+
namespace GameFramework.Taurus
16+
{
17+
public class LuaBehaviour : MonoBehaviour
18+
{
19+
/// <summary>
20+
/// 需要赋值的物体
21+
/// </summary>
22+
public Injection[] injections;
23+
//lua脚本
24+
private string _luaScript;
25+
26+
private Action _start;
27+
private Action _close;
28+
private Action _update;
29+
private Action _enable;
30+
private Action _disable;
31+
32+
/// <summary>
33+
/// 运行lua的脚本
34+
/// </summary>
35+
public void Run(string luaScript)
36+
{
37+
if (string.IsNullOrEmpty(_luaScript) && !string.IsNullOrEmpty(luaScript))
38+
{
39+
LuaTable scriptEnv = GameMode.HotFix.LuaEnv.NewTable();
40+
41+
// 为每个脚本设置一个独立的环境,可一定程度上防止脚本间全局变量、函数冲突
42+
LuaTable meta = GameMode.HotFix.LuaEnv.NewTable();
43+
meta.Set("__index", GameMode.HotFix.LuaEnv.Global);
44+
scriptEnv.SetMetaTable(meta);
45+
meta.Dispose();
46+
47+
scriptEnv.Set("self", this);
48+
foreach (var injection in injections)
49+
{
50+
scriptEnv.Set(injection.name, injection.value);
51+
}
52+
53+
GameMode.HotFix.LuaEnv.DoString($"require '{luaScript}'", luaScript, scriptEnv);
54+
scriptEnv.Get("Start", out _start);
55+
scriptEnv.Get("Update", out _update);
56+
scriptEnv.Get("Close", out _close);
57+
scriptEnv.Get("Enable", out _enable);
58+
scriptEnv.Get("Disable", out _disable);
59+
60+
_start?.Invoke();
61+
62+
_luaScript = luaScript;
63+
}
64+
65+
}
66+
67+
private void OnEnable()
68+
{
69+
_enable?.Invoke();
70+
}
71+
72+
private void OnDisable()
73+
{
74+
_disable?.Invoke();
75+
}
76+
private void Update()
77+
{
78+
_update?.Invoke();
79+
}
80+
private void OnDestroy()
81+
{
82+
_close?.Invoke();
83+
}
84+
}
85+
}

Assets/GameFramework/HotFix/LuaBehaviour.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.

0 commit comments

Comments
 (0)