XLua案例学习

下载 xlua 之后把 asset 文件中的全部文件粘贴到项目文件Asset文件下,将tool粘贴到 asset 同级目录下

然后把 HOTFIX_ENABLE 宏打开 之后 编辑 lua 脚本

更改源代码之后先 Generate Code 然后 HotFix inject in Editor

开发过程:

首先开发业务代码->在所有可能出现问题的类上打上hotfix的标签,在所有lua调用CSharp的方法上打上LuaCallCSharp,在所有CSharp调用Lua的方法上打上CSharpCallLua->打包发布->修改bug时只需要更新Lua文件,修改资源时(声音,模型,贴图,图片,UI)只需要更新ab包。用户只需要去下载lua文件跟ab包。

Lua访问C#中的私有变量方法

在lua文件中这么写:

xlua.private_accessable(CS.Gun)

执行完C#中原本的方法之后执行Lua中写的

复制代码
local util = require 'util'
xlua.private_accessible(CS.Boss)
util.hotfix_ex(CS.Boss,'Start',function(self)
    self.start(self)
    self.m_reduceGold = m_reduceGold - 20
end)

引入 util 这个 util.lua.txt 文件需要放在该lua脚本的平级目录中,在xlua_master 文件中寻找

注意用法

Xlua使用案例:

更改生成的钻石金币奖励之间的空隙

注意:local UnityEngine=CS.UnityEngine 下面代码中的CS.UnityEngine 全部显示为 Unity Engine 省事

源代码:

cs 复制代码
[LuaCallCSharp]
    private void CreatePrize()
    {
        for (int i = 0; i < 5; i++)
        {
            GameObject go = Instantiate(gold, transform.position + new Vector3(-10f + i * 30, 0, 0), transform.rotation);
            go.transform.SetParent(cavas);
            GameObject go1 = Instantiate(diamands, transform.position + new Vector3(0, 30, 0) + new Vector3(-10f + i * 30, 0, 0), transform.rotation);
            go1.transform.SetParent(cavas);
        }
    }

lua脚本:

Lua 复制代码
local UnityEngine=CS.UnityEngine
xlua.hotfix(CS.Treasour,'CreatePrize',function(self)
	for i=0,4,1 do
		local go=UnityEngine.GameObject.Instantiate(self.gold,self.transform.position+UnityEngine.Vector3(-10+i*40,0,0),self.transform.rotation)
		go.transform.SetParent(go.transform,self.cavas)
		local go1=UnityEngine.GameObject.Instantiate(self.diamands,self.transform.position+UnityEngine.Vector3(0,40,0)+UnityEngine.Vector3(-10+i*40,0,0),self.transform.rotation)
		go1.transform.SetParent(go1.transform,self.cavas)
	end
end)

如果有在 lua 中办不到的事情,可以提前在C#中写好,方便lua调用

下面这个获取资源的方法是C#中提前定义好的,方便Lua获取资源

Lua 复制代码
if changeMapTimeval >=50 then
			go=CS.HotFixScript.GetGameObject('SeaWave')
			UnityEngine.GameObject.Instantiate(go)
			canCreateNewFish=false
			changeMapTimeval=0
		else
			changeMapTimeval=changeMapTimeval+UnityEngine.Time.deltaTime
		end

随机数的坑

因为lua中没有 int float之分,而C#中 random函数有int float 为区分的重载,所以容易引起错误

用C#中的函数将得到的随机数向下取整

Lua 复制代码
self.num =UnityEngine.Mathf.Floor(UnityEngine.Random.Range(0, 4)

有时还会专门留下一个空的C#脚本给Lua调用,留几个空方法

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;

[Hotfix]
public class HotFixEmpty : MonoBehaviour
{
    [LuaCallCSharp]
    void Start()
    {
        
    }

    [LuaCallCSharp]
    void Update()
    {
        
    }

    [LuaCallCSharp]
    private void OnTriggerEnter(Collider other)
    {
        
    }

    [LuaCallCSharp]
    private void BehaviourMethod()
    {

    }

lua 脚本

Lua 复制代码
xlua.private_accessible(CS.HotFixEmpty)
xlua.hotfix(CS.HotFixEmpty,'Start',function(self)
	self:Invoke("BehaviourMethod",8)
end)

xlua.hotfix(CS.HotFixEmpty,'Update',function(self)
	self.transform:Translate(-self.transform.right*4*UnityEngine.Time.deltaTime,UnityEngine.Space.World)
end)


xlua.hotfix(CS.HotFixEmpty,'OnTriggerEnter',function(self,other)
	if other.tag~="Untagged" and other.tag~="Wall" then
		UnityEngine.Object.Destroy(other.gameObject)
	end
end)

xlua.hotfix(CS.HotFixEmpty,'BehaviourMethod',function(self)
	CS.Gun.Instance.level=CS.Gun.Instance.level+1
	if CS.Gun.Instance.level==4 then
		CS.Gun.Instance.level=1
	end
	canCreateNewFish=true
	CS.Gun.Instance.changeAudio=true
	UnityEngine.Object.Destroy(self.gameObject)

end)

Lua中没有泛型

Lua 复制代码
itemGo:GetComponent('Gold').bossPrize = true

--有一说一,用text 文本编辑器写代码真的是一种折磨

相关推荐
谙弆悕博士12 小时前
Lua学习笔记
c语言·开发语言·笔记·学习·lua·创业创新·业界资讯
weixin_408099671 天前
触动精灵调用身份证OCR识别API实现智能信息录入(Lua脚本实战)
junit·ocr·lua·自动化脚本·石榴智能·身份证ocr识别·触动精灵
卷毛的技术笔记2 天前
双十一零点扛过10倍流量洪峰:Sentinel与Redis+Lua的分布式限流深度避坑指南
java·redis·分布式·后端·系统架构·sentinel·lua
庞轩px2 天前
第五篇:分布式锁实战——Lua脚本原子操作与库存扣减的强一致性
redis·lua·分布式锁·synchronized·原子性·零超卖
笨鸟先飞的橘猫2 天前
lua——哈希表详细学习
学习·lua·散列表
晴夏。7 天前
unlua实现原理
游戏·ue5·ue4·lua·ue·unlua
晴夏。7 天前
c++调用lua的方法
c++·游戏引擎·lua·ue
chxii7 天前
lua中Table 与 Metatable
lua
xingpanvip8 天前
星盘接口开发文档:组合三限盘接口指南
android·开发语言·前端·python·php·lua
chxii8 天前
lua流程控制语句和table(表)数据结构
开发语言·junit·lua