xlua热补丁

在这里插入图片描述
在这里插入图片描述

print("*********第一个热补丁***********")

--直接写好代码 运行 是会报错的
--我们必须做4个非常重要的操作
--1.加特性
--2.加宏 第一次开发热补丁需要加
--3.生成代码
--4.hotfix 注入  --注入时可能报错 提示你要引入Tools

--热补丁的缺点:只要我们修改了热补丁类的代码,我们就需要重新执行第4步!!!
--需要重新点击 注入

--lua当中 热补丁代码固定写法
--xlua.hotfix(类, "函数名", lua函数)

--成员函数 第一个参数 self
xlua.hotfix(CS.HotfixMain, "Add", function(self, a, b)
	return a + b
end)

--静态函数 不用传第一个参数
xlua.hotfix(CS.HotfixMain, "Speak", function(a)
	print(a)
end)

在这里插入图片描述

--lua当中 热补丁代码固定写法
--xlua.hotfix(类, "函数名", lua函数)

--xlua.hotfix(类, {函数名 = 函数, 函数名 = 函数....})
--xlua.hotfix(CS.HotfixMain, {
	--Update = function(self)
	--	print(os.time())
	--end,
	--Add = function(self, a, b )
	--	return a + b
	--end--,
	--Speak = function(a)
	--	print(a)
	--end
--})

xlua.hotfix(CS.HotfixTest, {
	--构造函数 热补丁固定写法[".ctor"]!!!!
	--他们和别的函数不同 不是替换 是先调用原逻辑 再调用lua逻辑
	[".ctor"] = function()
		print("Lua热补丁构造函数")
	end,
	Speak = function(self,a)
		print("ac说" .. a)
	end,
	--析构函数固定写法Finalize
	Finalize = function()
		
	end
})

在这里插入图片描述

print("*********协程函数替换***********")

--xlua.hotfix(类, {函数名 = 函数, 函数名 = 函数....})
--要在lua中配合C#协程函数  那么必使用它
util = require("xlua.util")
xlua.hotfix(CS.HotfixMain, {
	TestCoroutine = function(self)
		--返回一个正儿八经的 xlua处理过的lua协程函数
		return util.cs_generator(function()
			while true do
				coroutine.yield(CS.UnityEngine.WaitForSeconds(1))
				print("Lua打补丁后的协程函数")
			end
		end)
	end
})


--如果我们为打了Hotfix特性的C#类新加了函数内容
--不能只注入  必须要先生成代码 再注入 不然注入会报错
print("*********属性和索引器替换***********")

xlua.hotfix(CS.HotfixMain, {
	--如果是属性进行热补丁重定向
	--set_属性名 是设置属性 的方法
	--get_属性名 是得到属性 的方法
	set_Age = function(self, v)
		print("Lua重定向的属性"..v)
	end,
	get_Age = function(self)
		return 10;
	end,
	--索引器固定写法
	--set_Item 通说索引器设置
	--get_Item 通过索引器获取
	set_Item = function(self, index, v )
		print("Lua重定向索引器,索引:"..index .."值" ..v)
	end,
	get_Item = function(self, index)
		print("Lua重定向索引器")
		return 999
	end
})

在这里插入图片描述

print("*********事件加减替换***********")


xlua.hotfix(CS.HotfixMain, {
	--add_事件名 代表着时间加操作
	--remove_事件名 减操作
	add_myEvent = function(self, del)
		print(del)
		print("添加事件函数")
		--会去尝试使用lua使用C#事件的方法去添加
		--在事件加减的重定向lua函数中
		--千万不要把传入的委托往事件里存
		--否则会死循环
		--会把传入的 函数 存在lua中!!!!!
		--self:myEvent("+", del)
	end,
	remove_myEvent = function(self, del )
		print(del)
		print("移除事件函数")
	end
})
print("*********泛型类的替换***********")

--泛型类 T是可以变化 那lua中应该如何替换呢?
--lua中的替换 要一个类型一个类型的来

xlua.hotfix(CS.HotfixTest2(CS.System.String), {
	Test = function(self, str)
		print("lua中打的补丁:"..str)
	end
})

xlua.hotfix(CS.HotfixTest2(CS.System.Int32), {
	Test = function(self, str)
		print("lua中打的补丁:"..str)
	end
})

========================================================================
unity c#代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using XLua;

[Hotfix]
public class HotfixTest
{
    public HotfixTest()
    {
        Debug.Log("HotfixTest构造函数");
    }

    public void Speak(string str)
    {
        Debug.Log(str);
    }

    ~HotfixTest()
    {

    }
}

[Hotfix]
public class HotfixTest2<T>
{
    public void Test(T str)
    {
        Debug.Log(str);
    }
}


[Hotfix]
public class HotfixMain : MonoBehaviour
{
    HotfixTest hotTest;

    public int[] array = new int[] { 1, 2, 3 };

    //属性
    public int Age
    {
        get
        {
            return 0;
        }
        set
        {
            Debug.Log(value);
        }
    }

    //索引器
    public int this[int index]
    {
        get
        {
            if( index >= array.Length || index < 0)
            {
                Debug.Log("索引不正确");
                return 0;
            }

            return array[index];
        }
        set
        {
            if (index >= array.Length || index < 0)
            {
                Debug.Log("索引不正确");
                return;
            }
            array[index] = value;
        }
    }

    event UnityAction myEvent;

    // Start is called before the first frame update
    void Start()
    {
        LuaMgr.GetInstance().Init();
        LuaMgr.GetInstance().DoLuaFile("Main");

        Debug.Log(Add(10, 20));
        Speak("abc好爱你!");

        hotTest = new HotfixTest();
        hotTest.Speak("嘻嘻嘻嘻");


        //StartCoroutine(TestCoroutine());

        this.Age = 100;
        Debug.Log(this.Age);

        this[99] = 100;
        Debug.Log(this[9999]);

        myEvent += TestTest;
        myEvent -= TestTest;

        HotfixTest2<string> t1 = new HotfixTest2<string>();
        t1.Test("123");

        HotfixTest2<int> t2 = new HotfixTest2<int>();
        t2.Test(1000);
    }

    private void TestTest()
    {

    }

    // Update is called once per frame
    void Update()
    {
        
    }

    IEnumerator TestCoroutine()
    {
        while(true)
        {
            yield return new WaitForSeconds(1f);
            Debug.Log("C#协程打印一次");
        }
    }

    public int Add(int a, int b)
    {
        return 0;
    }

    public static void Speak(string str)
    {
        Debug.Log("哈哈哈哈哈");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值