java lua热更新_XLua_热更新学习四,使用lua脚本热更新

上一篇搭建好了lua虚拟环境(点击查看),本篇利用搭建好的虚拟环境尝试着进行XLua热更新

1.先创建项目,写好业务代码(例子)

1.1创建Button,和脚本NewBehaviourScript(挂在Button上)

1.2代码如下

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;

using XLua;

[Hotfix] //[Hotfix]一定要有

public class NewBehaviourScript : MonoBehaviour {

public GameObject hong;

public GameObject Canvass;

void Start () {

transform.GetComponent().onClick.AddListener(OnButtonClick);

}

[LuaCallCSharp]//[LuaCallCSharp] lua调用C#

public void OnButtonClick()

{

GameObject ga = Instantiate(hong, transform.position + new Vector3(0, -100f, 0), transform.rotation);

ga.transform.SetParent(Canvass.transform);

}

}

2.打开上篇创建的lua脚本fish.lua.txt 编写补丁代码

print('更改预设物生成位置')

--C#命名空间

local UnityEngine = CS.UnityEngine

--补丁 参数1:需要更改的C#类名。参数2:需要更改的方法名。参数3:lua方法(更改的内容)

xlua.hotfix(CS.NewBehaviourScript,'OnButtonClick',function(self)

local go = UnityEngine.GameObject.Instantiate(self.hong,UnityEngine.transform.Position+UnityEngine.Vector3(100,100,0),self.transform.rotation)

go.transform.SetParent(go.transform,self.Canvass.transform)

end)

3.打开Unity进行以下操作

5880f7aa5719

4.运行测试

5880f7aa5719

热更前的生成位置

5880f7aa5719

热更后的生成位置

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值