一、干凈的基于XLua的框架下載地址
1.游戲框架下載地址:https://github.com/kof123w/gitWorkSpace/tree/main/XLua
2.XLua官方與教程地址:https://github.com/Tencent/xLua
二、使用步驟
1.操作步驟
I.宏定義:添加 HOTFIX_ENABLE 到 Edit > Project Settings > Player > Other Settings > Scripting Define Symbols
II.生成代碼:執(zhí)行 ‘XLua > Generate Code’ 菜單,等待Unity編譯完成
III.注入:執(zhí)行 ‘XLua > Hotfix Inject In Editor’ 菜單。注入成功會打印 ‘hotfix inject finish!’ 或者 ‘had injected!’
(注意)unity2021.3的版本布局以及被修改了,Scripting Define Symbols菜單在下圖這里:
2.腳本添加
1.在游戲邏輯代碼文件夾創(chuàng)建腳本HotFixTest.cs:
這里需要說明以下,所有我們需要對C#代碼的類進行Lua代碼注入的時候都需要給對應(yīng)的類添加一個[Hotfix]的特性,對應(yīng)的需要進行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.在游戲腳本管理代碼文件夾創(chuàng)建腳本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虛擬機代碼,運行l(wèi)ua代碼
LuaManager.Instance.runLuaScript();
}
}
4.在Lua腳本的游戲邏輯下添加一個HotFixTest.lua腳本:
hotFixTest = {}
hotFixTest.hotHotFixTest = function()
--參數(shù)1為對應(yīng)的ccharp類,參數(shù)2為對應(yīng)的方法名,參數(shù)3為修改后的函數(shù)體
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");
end
main.update = function()
print("this mian update function")
end
require('Game/HotFixTest') --初始化HotFixTest.lua腳本
三、運行結(jié)構(gòu)
1.按鈕沒有點擊前:
2.按鈕點擊之后
文章來源:http://www.zghlxwxcb.cn/news/detail-479775.html
二、源碼下載地址
https://github.com/kof123w/gitWorkSpace/tree/main/XLua文章來源地址http://www.zghlxwxcb.cn/news/detail-479775.html
到了這里,關(guān)于【Unity框架】XLua中Lua代碼注入C#代碼操作的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!