[Unity framework] Lua code injection into C# code operation in XLua

1. Clean XLua-based framework download address

1. Game framework download address: https://github.com/kof123w/gitWorkSpace/tree/main/XLua
2. XLua official and tutorial address: https://github.com/Tencent/xLua

2. Use steps

1. Operation steps

I. Macro definition: Add HOTFIX_ENABLE to Edit > Project Settings > Player > Other Settings > Scripting Define Symbols

II. Generate code: Execute the 'XLua > Generate Code' menu and wait for Unity to compile

III. Injection: Execute 'XLua > Hotfix Inject In Editor' menu. Successful injection will print 'hotfix inject finish!' or 'had injected!'

( Note ) The version layout of unity2021.3 has been modified, and the Scripting Define Symbols menu is shown in the figure below:
insert image description here

2. Add script

1. Create the script HotFixTest.cs in the game logic code folder:

The following needs to be explained here. When we need to inject Lua code into the C# code class, we need to add a [Hotfix] feature to the corresponding class. The corresponding Lua code injection method needs to add the feature [LuaCallSharp], as follows The code shows:

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. Create the script HotFixTest.cs in the game script management code folder:

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. Initialize CSharpManager.cs in the game startup script:

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. Add a HotFixTest.lua script under the game logic of the Lua script:

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. It is also necessary to add the request and initialization of HotFixTest.lua to the Main.lua script of the Lua script


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. Operating structure

1. Before the button is clicked:

insert image description here

2. After the button is clicked

insert image description here

2. Source code download address

https://github.com/kof123w/gitWorkSpace/tree/main/XLua

Guess you like

Origin blog.csdn.net/qq_41094072/article/details/127020111