文章目录
一、干净的基于XLua的框架下载地址二、使用步骤1.操作步骤2.脚本添加1.在游戏逻辑代码文件夹创建脚本HotFixTest.cs:2.在游戏脚本管理代码文件夹创建脚本HotFixTest.cs:3.在游戏启动脚本中对CSharpManager.cs进行初始化:4.在Lua脚本的游戏逻辑下添加一个HotFixTest.lua脚本:5.还需要再Lua脚本的Main.lua脚本中添加对HotFixTest.lua的请求和初始化 三、运行结构1.按钮没有点击前:2.按钮点击之后 二、源码下载地址一、干净的基于XLua的框架下载地址
1.游戏框架下载地址:/kof123w/gitWorkSpace/tree/main/XLua
2.XLua官方与教程地址:/Tencent/xLua
二、使用步骤
1.操作步骤
I.宏定义:添加 HOTFIX_ENABLE 到 Edit > Project Settings > Player > Other Settings > Scripting Define Symbols
II.生成代码:执行 ‘XLua > Generate Code’ 菜单,等待Unity编译完成
III.注入:执行 ‘XLua > Hotfix Inject In Editor’ 菜单。注入成功会打印 ‘hotfix inject finish!’ 或者 ‘had injected!’
(注意)unity.3的版本布局以及被修改了,Scripting Define Symbols菜单在下图这里:
2.脚本添加
1.在游戏逻辑代码文件夹创建脚本HotFixTest.cs:
这里需要说明以下,所有我们需要对C#代码的类进行Lua代码注入的时候都需要给对应的类添加一个[Hotfix]的特性,对应的需要进行Lua代码注入方法需要添加特性[LuaCallSharp],如以下代码所示:
using System.Collections;using System.Collections.Generic;using UnityEngine;using XLua;[Hotfix]public class HotFixTest : MonoBehaviour{void Start(){hotFixTest();} [LuaCallCSharp]public void hotFixTest(){Debug.Log("我是C#代码的输出");}void OnGUI(){if (GUI.Button(new Rect(10, 10, 300, 80), "Hotfix")){LuaManager.Instance.GetLuaEnv().DoString(@"hotFixTest.hotHotFixTest()");hotFixTest();} } }
2.在游戏脚本管理代码文件夹创建脚本HotFixTest.cs:
using System.Collections;using System.Collections.Generic;using UnityEngine;public class CSharpManager:UnitySingleton<CSharpManager>{private GameObject mainCamera; public override void Awake(){//父类单例管理base.Awake();//初始化摄像机initCamera();//子类扩展添加脚本注册this.gameObject.AddComponent<HotFixTest>();}/// <summary>/// 初始化摄像机/// </summary>private void initCamera() {GameObject go = new GameObject();go.AddComponent<Camera>();go.AddComponent<AudioListener>();go.name = "mainCamera";mainCamera = go;}/// <summary>/// 外界获取摄像机代码/// </summary> public Camera GetMainCamera() {return mainCamera.GetComponent<Camera>();}}
3.在游戏启动脚本中对CSharpManager.cs进行初始化:
using System.Collections;using System.Collections.Generic;using UnityEngine;public class GameStarter : UnitySingleton<GameStarter>{public override void Awake(){base.Awake();//初始化游戏框架this.gameObject.AddComponent<LuaManager>();this.gameObject.AddComponent<CSharpManager>();//资源管理于初始化 }private void Start(){//进入游戏this.StartCoroutine(this.GameStart()); }/// <summary>/// 检查热更新携程/// </summary> IEnumerator checkHotUpdate() {yield return 0;}IEnumerator GameStart() {yield return this.StartCoroutine(this.checkHotUpdate());//进入Lua虚拟机代码,运行lua代码 LuaManager.Instance.runLuaScript();}}
4.在Lua脚本的游戏逻辑下添加一个HotFixTest.lua脚本:
hotFixTest = {}hotFixTest.hotHotFixTest = function()--参数1为对应的ccharp类,参数2为对应的方法名,参数3为修改后的函数体xlua.hotfix(CS.HotFixTest,'hotFixTest',function(self)print("我是lua打印出来的")end)end--这里主要释放掉修改的方法的注入hotFixTest.disposeHotFixTest = function()xlua.hotfix(CS.HotFixTest,'hotFixTest',nil) end
5.还需要再Lua脚本的Main.lua脚本中添加对HotFixTest.lua的请求和初始化
main = {}main.awake = function()print("this mian awake function");endmain.update = function()print("this mian update function")endrequire('Game/HotFixTest') --初始化HotFixTest.lua脚本
三、运行结构
1.按钮没有点击前:
2.按钮点击之后
二、源码下载地址
/kof123w/gitWorkSpace/tree/main/XLua