1. XLuaベースのフレームワークのダウンロードアドレスをクリーンアップ
1. ゲーム フレームワークのダウンロード アドレス: https://github.com/kof123w/gitWorkSpace/tree/main/XLua
2. XLua の公式およびチュートリアルのアドレス: https://github.com/Tencent/xLua
2.ステップを使用する
1.操作手順
I. マクロ定義: [編集] > [プロジェクト設定] > [プレーヤー] > [その他の設定] > [スクリプト定義シンボル] に HOTFIX_ENABLE を追加します。
II. コードの生成: [XLua > Generate Code] メニューを実行し、Unity がコンパイルされるのを待ちます。
III. インジェクション: [XLua > Hotfix Inject In Editor] メニューを実行します。インジェクションが成功すると、「ホットフィックス インジェクト フィニッシュ!」または「ハッド インジェクト!」が出力されます。
(注) unity2021.3 のバージョン レイアウトが変更され、Scripting Define Symbols メニューが下の図に示されます。
2.スクリプトを追加
1. ゲーム ロジック コード フォルダーにスクリプト HotFixTest.cs を作成します。
ここで説明する必要があるのは、Lua コードを C# コード クラスにインジェクトする必要がある場合、対応するクラスに [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");
end
main.update = function()
print("this mian update function")
end
require('Game/HotFixTest') --初始化HotFixTest.lua脚本
3. 運営体制
1. ボタンがクリックされる前:
2. ボタンクリック後
2. ソースコードダウンロードアドレス
https://github.com/kof123w/gitWorkSpace/tree/main/XLua