网站运营经验,门户网站如何运营,秦皇岛网站制作方案,做通风工程上哪个网站发布Lua 热更新 - 上篇
下篇链接#xff1a;【Lua热更新】下篇 文章目录 Lua 热更新 - 上篇一、AssetBundle1.理论2. AB包资源加载 二、Lua 语法1. 简单数据类型2.字符串操作3.运算符4.条件分支语句5.循环语句6.函数7. table数组8.迭代器遍历9.复杂数据类型 - 表9.1字典9.2类9.3…Lua 热更新 - 上篇
下篇链接【Lua热更新】下篇 文章目录 Lua 热更新 - 上篇一、AssetBundle1.理论2. AB包资源加载 二、Lua 语法1. 简单数据类型2.字符串操作3.运算符4.条件分支语句5.循环语句6.函数7. table数组8.迭代器遍历9.复杂数据类型 - 表9.1字典9.2类9.3 表的公共操作 多脚本执行10.协同程序11.元表11.1 index11.2 newindex11.3其他操作 12.面向对象12.1封装12.2继承12.3多态 12 .【重要】面向对象汇总13.自带库13.垃圾回收 三、XLua热更新 一、AssetBundle
1.理论 AB包是什么特定于平台的资产压缩包有点类似压缩文件资产包括一些模型、贴图、预设体、音效、材质球等等 AB包作用Resoureces打包时只读无法修改AB包存储位置可以自定义压缩方式自定义后期可以动态更新。AB包可以减少包体大小压缩资源减少初始包大小。可以通过热更新更新脚本或者资源 使用
2. AB包资源加载
同步加载
// 加载AB包
AssetBundle ab AssetBundle.LoadFromFile(Application.streamingAssetsPath /model);
// 加载AB包中资源
// 通过泛型加载
GameObject cube ab.LoadAssetGameObject(cube);
// 通过type加载
GameObject sphere ab.LoadAsset(sphere, typeof(GameObject)) as GameObject;
// AB包不能加载两次会报错
// AssetBundle ab2 AssetBundle.LoadFromFile(Application.streamingAssetsPath /model);异步加载
public Image img;
IEnumerator LoadABRes(string abName, string resNAme)
{// 异步加载ab包AssetBundleCreateRequest abcr AssetBundle.LoadFromFileAsync(Application.streamingAssetsPath / abName . resNAme);yield return abcr;AssetBundleRequest abq abcr.assetBundle.LoadAssetAsync(resNAme, typeof(Sprite));yield return abq;img.sprite abq.asset as Sprite;
}AB包卸载
AssetBundle ab2 AssetBundle.LoadFromFile(Application.streamingAssetsPath /model);
// 卸载所有AB包
// true会把场景上加载的AB包资源也卸载了
AssetBundle.UnloadAllAssetBundles(false);
// 卸载单独的AB包
ab2.Unload(false);二、Lua 语法
1. 简单数据类型
lua所有变量声明不需要申明变量类型lua可以改变数据类型自动识别通过type函数获得类型lua中使用未声明的变量默认是nil
-- 【nil类型】类似 C#中的null
a nil
print(a)
print(type(a))-- 【number类型】表示所有数值类型
a 1
print(a)
print(type(a))
a 10.2
print(a)
print(type(a))-- 【string类型】字符串类型lua中单引号或双引号都是字符串与C#中不同lua中没有char
a 123
print(a)
a 111
print(a)
print(type(a))-- 【boolean类型】
a true
print(a)
print(type(a))2.字符串操作
lua中文占三个字符
-- 获得字符串长度
str hellowrold
print(#str)
str 你好
print(#str)-- 字符串拼接
print(你好..世界)
print(string.format(我%d岁了, 18))-- 转字符串
str tostring(true)
print(str)-- 字符串转ASCII码
a string.byte(Lua, 1)
print(a)
-- ASCII码转字符
str string.char(a)
print(str)3.运算符
字符串可以自动转成number整数相除可以转换为浮点数不支持自增自减 –不支持位运算、三目运算符Lua中 nil 和 false 才是假lua中逻辑运算符支持短路
-- 算数运算符
print(3.14 2) -- 输出5.14
print(3 / 2) -- 输出1.5
print(2 ^ 2) -- 支持幂运算-- 条件运算符
print(100 ~ 5) -- 不等于符号与C#不同! -- 逻辑运算符
-- lua中逻辑运算符支持短路
print(true and false) -- 与
print(true or false) -- ||或
print(not true) -- !非
-- lua中and or可以链接任意类型
print(1 and 2) -- 返回 2
print(nil and 1) -- 返回 nil
-- 使用逻辑运算模拟三目运算符
res (x y) and x or y4.条件分支语句
不支持switch
a 6
-- if 语句
if a 5 thenprint(success)
end-- if-else语句
if a 5 thenprint(succcess!)
elseprint(fault!)
end-- if-else if语句
if a 5 thenprint(是5!)
elseif a 6 thenprint(是6!)
end5.循环语句
-- while
num 0
while num 5 donum num 1
end
print(num)-- do while
num 6
repeatnum num 1
until num 5
print(num)-- for
-- for 变量, 结束值增量值省略默认为1
for i 1, 5, 2 doprint(i)
end6.函数
-- 1.1声明
function myFirstFun()print(我的第一个函数)
end
-- 调用
myFirstFun()-- 2.1有参函数
function testFun1(name)print(我的名字是..name)
end
testFun1(小猫)-- 3.1有返回值函数
-- 3.1.1一个返回值
function testFun2()return yes
end
temp testFun2()
print(temp)
-- 3.1.2多个返回值
function testFun3()return paramete1,paramete2
end
temp1, temp2 testFun3()
print(temp1..temp2)-- 4.1类型
-- 不用写函数名
func1 function()print(这是一个函数)
end
print(type(func1))
func1()-- 5.1变长参数
function testFun4(...)arg {...}for i 1, #arg doprint(arg[i])end
end
testFun4(1, 2, 3)-- 6.1函数嵌套
-- 闭包情况
function myFunC(x)return function(y)return x yend
endmyfunc myFunC(1)
ans myfunc(2)
print(ans ..ans)7. table数组
lua中下标从1开始越界读取最后一个表中存在nil会打断表长
-- 1.1 数组声明
a {1, 2, true, name, nil}
print(a[1]) -- 下标从1开始
print(#a) -- 输出4-- 1.2 多维数组
a {{1, 2, 3}, {4, 5, 6, 7}}
print(a[1][1])
for i 1, #a dob a[i]for j 1, #b doprint(b[j])end
end8.迭代器遍历
pairs遍历所有都能获得建议使用
a {[0] 1, 2, [-1] 3, 4, 5}-- 1.1 ipairs遍历
-- 只能找从1开始下标小于等于0得不到
-- 只能找连续索引的如果中断后面无法获得
for k, v in ipairs(a) doprint(key ..k.. value .. v)
end-- 1.2 pairs遍历
-- 所有都能获得
for k, v in pairs(a) doprint(key ..k.. value .. v)
end
-- 只遍历键
for k in pairs(a) doprint(key ..k)
end9.复杂数据类型 - 表
9.1字典
fruitDic {[apple] 10, [banana] 5}
print(fruitDic[apple])
print(fruitDic.banana) -- 只有字符串可以通过.获得-- 遍历
for k, v in pairs(fruitDic) doprint(k,v) -- 传入多个参数print
end9.2类
lua中不存在面向对象
student {name lily,age 10,sex true,changeSex function(v)student.sex vend,printSex function()print(student.sex)end,printName function(m)print(m.name)end
}student.printSex()
student.changeSex(false)
student.printSex()-- 冒号调用
-- 冒号调用默认第一个参数是调用者
-- .调用不能
student:printName()-- 外部添加函数
-- 方法一
function student.myScore(v)print(v 100)
end-- slef表示默认传入的第一个参数
-- 方法二
function student:printAge()print(self.age)
end
-- 冒号声明冒号调用
student:printAge()9.3 表的公共操作
t1 {{name 小猫, age 1}, {name 小狗, age 2}}
t2 {name 小鸟, age 2}-- 1.1插入
-- t2 插入到 t1 中
table.insert(t1, t2)
print(#t1)-- 1.2删除
-- 删除 t1 的最后一个元素
table.remove(t1)
print(t1[2].name)
-- 删除指定下标
table.remove(t1, 1)
print(t1[1].name)-- 1.3排序
t {5, 6, 1, 3, 9, 2}-- 默认升序
table.sort(t)
print(----降序-----)
for _,v in pairs(t) doprint(v)
end-- 降序
table.sort( t, function(a, b)return a b
end )
print(----降序-----)
for _,v in pairs(t) doprint(v)
end-- 1.4 拼接
tb {ccc, aaa, bbb}
str table.concat( tb, ;)
print(str)多脚本执行
-- 1.1 局部变量
function myFunC()local x 1y 1
endmyFunC()
print(x) -- x是局部变量
print(y) -- y是全局变量-- 1.2 多脚本执行
-- require(脚本名) 执行其他脚本只能加载一次
require(testLua)
-- 获得其他脚本的全局变量,局部变量获取不到
print(myTest)-- 1.3脚本卸载
-- 脚本是否被加载过
isLoad package.loaded[testLua]
print(isLoad)
-- 卸载脚本
package.loaded[testLua] nil-- 1.4 大G表
-- 大G表是一个总的table表存储了所有全局变量在里面
for k,v in pairs(_G) doprint(k, v)
end require 脚本可以 return 一个数据返回出去得到
10.协同程序
fun function()print(使用协程)local i 1while true doprint(i)i i 1coroutine.yield(i)end
end-- 返回一个线程
co coroutine.create(fun)
print(类型..type(co))
coroutine.resume(co) -- 输出1
isOK, temp1 coroutine.resume(co) -- 输出2
print(isOK,temp1) -- 第一个参数返回协程状态第二个参数返回协程返回值-- 返回的是函数
co2 coroutine.wrap(fun)
print(类型..type(co2))
co2() -- 输出1
temp1 co2() -- 输出2
print(temp1) -- 直接返回协程返回值-- 获得协程状态
-- 传入协程对象返回状态dead结束 susdeaded暂停 running进行
back coroutine.status(co)
print(back)11.元表
11.1 index
index取值时使用当子表中找不到某属性时会到元素中__index指定的表去查找索引index支持向上查找原表的index
meta {name father,age 10,
}
sonTable {name son
}
setmetatable(sonTable, meta)
print(sonTable.age) -- 输出 nil-- 设置__index
meta.__index meta
print(sonTable.age) -- 输出 1011.2 newindex
newindex当赋值时如果赋值不存在索引将会赋值到newindex所指向的表中
meta {name father
}
sonTable {
}
setmetatable(sonTable, meta)sonTable.name son
print(sonTable.name) -- 输出sonmeta.__newindex {}
print(sonTable.age) -- 输出nil11.3其他操作
meta {name father,__tostring function(t)return t.nameend,__call function(t)print(t)print(call!)end,__add function(t1, t2)return t1.name..t2.nameend
}
sonTable {name son
}-- 设置元表参数一 子表 参数二 元表
setmetatable(sonTable, meta)-- 1.1 __tostring
-- 当子表被当作字符串使用时会调用tostring方法
-- 默认传递第一个参数为调用者自己
print(sonTable)-- 输出son-- 1.2 __call
-- 子表被当作函数使用时会调用call方法
-- 默认传递第一个参数为调用者自己
sonTable() -- 输出 son call!-- 1.3运算符重载
-- __add 加法
print(sonTable meta) -- 输出sonfather
-- __sub 减法、__mul 乘法、 __div 除法、 __mod 取余、 __pow 幂、
-- __eq 相等 原表一致返回true、__lt 小于、 __le 小于等于、__concat 拼接12.面向对象
12.1封装
-- 1.1 封装
Object {}
Object.id 1-- 冒号调用默认使用调用者作为第一个参数
function Object : new()-- self,获得默认传入的第一个参数-- 返回一个新对象表,使用局部变量-- 设置对象的元表,和__indexlocal obj {}self.__index selfsetmetatable(obj, self)return obj
endlocal my Object : new()
print(my.id) -- 输出1因为使用了__indexmy.id 99
print(my.id) -- 输出99没有使用__newindex不会改变元表
print(Object.id) -- 输出1-- 封装一个函数
function Object : TestFun()print(self.id)
endmy:TestFun() -- 输出99
Object:TestFun() -- 输出112.2继承
-- 为父类写一个继承方法
function Object : subClass(className)-- _G表是一个总表存储所有声明了的全局变量以键值对形式存储-- 在_G中创建一个表_G[className] {}-- 用元表完成继承local obj _G[className]self.__index selfsetmetatable(obj, self)
end-- Person继承Object
Object:subClass(Person)
-- 使用Object中的new对象方法
local p1 Person:new()
print(p1.id)12.3多态 -- Entity 继承 Object
Object : subClass(Entity)
-- 为 Object 添加变量和方法
Object.Speed 0
function Object : GetSpeed()print(这是Object的方法)self.Speed self.Speed 1print(self.Speed)return self.Speed
end-- Player 继承 Entity
Entity : subClass(Player)-- 创建Player 对象 p1
local p1 Player:new()
p1:GetSpeed() -- 输出这是Object的方法-- 子类Player重写方法
function Player : GetSpeed()print(这是Player重写的方法)return self.Speed
end
p1:GetSpeed() -- 输出这是Player重写的方法-- 构建base保留父类方法调用 如图-- 创建Player 对象 p2
local p2 Player:new()
p2:GetSpeed() -- 输出这是Player重写的方法-- 【注意】有误的
p1.base:GetSpeed() -- 输出Speed 1
p2.base:GetSpeed() -- 输出Speed 2
-- 【注意】正确的使用点调用否则使用的是父类的变量
function Player : GetSpeed()self.base.GetSpeed(self)
end
p1:GetSpeed()
p2:GetSpeed()
-- 或者
p1.base.GetSpeed(p1)
p2.base.GetSpeed(p2)
12 .【重要】面向对象汇总
-- 基类
Object {}
-- 实例化对象方法
function Object : new()local obj {}self.__index selfsetmetatable(obj, self)return obj
end
-- 继承方法
-- className:类名(字符串)
function Object : subClass(className)_G[className] {}local obj _G[className]self.__index selfobj.base selfsetmetatable(obj, self)
end13.自带库
-- 时间
-- 系统时间
print(os.time())
print(os.time({year 2024, month 12, day 17}))
local nowTime os.date(*t)
for k, v in pairs(nowTime) doprint(k, v)
end-- 数学运算
print(math.abs(-11)) -- 绝对值
print(math.deg(math.pi)) -- 弧度转角度
print(math.cos(math.pi)) -- 三角函数
print(math.floor(2.6)) -- 向下取整
print(math.ceil(2.6)) -- 向上取整
print(math.max(1, 2))
print(math.min(1, 2))
print(math.modf(1.2)) -- 返回整数和小数部分
print(math.pow(2, 2)) -- 幂运算
math.randomseed(os.time()) -- 更换随机数种子
print(math.random(100)) -- 随机数
print(math.random(100)) -- 随机数
print(math.sqrt(2)) -- 开方-- 路径
print(package.path)13.垃圾回收
三、XLua热更新
下篇链接【Lua热更新】下篇 文章转载自: http://www.morning.smj78.cn.gov.cn.smj78.cn http://www.morning.rqxch.cn.gov.cn.rqxch.cn http://www.morning.snmsq.cn.gov.cn.snmsq.cn http://www.morning.dtgjt.cn.gov.cn.dtgjt.cn http://www.morning.nmyrg.cn.gov.cn.nmyrg.cn http://www.morning.bkpbm.cn.gov.cn.bkpbm.cn http://www.morning.kfhm.cn.gov.cn.kfhm.cn http://www.morning.rghkg.cn.gov.cn.rghkg.cn http://www.morning.zxqqx.cn.gov.cn.zxqqx.cn http://www.morning.mlzyx.cn.gov.cn.mlzyx.cn http://www.morning.qhrsy.cn.gov.cn.qhrsy.cn http://www.morning.bxqry.cn.gov.cn.bxqry.cn http://www.morning.zljqb.cn.gov.cn.zljqb.cn http://www.morning.hmmnb.cn.gov.cn.hmmnb.cn http://www.morning.zxdhp.cn.gov.cn.zxdhp.cn http://www.morning.fppzc.cn.gov.cn.fppzc.cn http://www.morning.qcwrm.cn.gov.cn.qcwrm.cn http://www.morning.dnycx.cn.gov.cn.dnycx.cn http://www.morning.tbjtm.cn.gov.cn.tbjtm.cn http://www.morning.gynlc.cn.gov.cn.gynlc.cn http://www.morning.nicetj.com.gov.cn.nicetj.com http://www.morning.xkyfq.cn.gov.cn.xkyfq.cn http://www.morning.rwmqp.cn.gov.cn.rwmqp.cn http://www.morning.qjfkz.cn.gov.cn.qjfkz.cn http://www.morning.fbzyc.cn.gov.cn.fbzyc.cn http://www.morning.rqkzh.cn.gov.cn.rqkzh.cn http://www.morning.btlmb.cn.gov.cn.btlmb.cn http://www.morning.qpsft.cn.gov.cn.qpsft.cn http://www.morning.mflqd.cn.gov.cn.mflqd.cn http://www.morning.nbhft.cn.gov.cn.nbhft.cn http://www.morning.knlyl.cn.gov.cn.knlyl.cn http://www.morning.gcqs.cn.gov.cn.gcqs.cn http://www.morning.rpkl.cn.gov.cn.rpkl.cn http://www.morning.cpmwg.cn.gov.cn.cpmwg.cn http://www.morning.pkpqh.cn.gov.cn.pkpqh.cn http://www.morning.jjnql.cn.gov.cn.jjnql.cn http://www.morning.djpps.cn.gov.cn.djpps.cn http://www.morning.lydtr.cn.gov.cn.lydtr.cn http://www.morning.fthqc.cn.gov.cn.fthqc.cn http://www.morning.wfykn.cn.gov.cn.wfykn.cn http://www.morning.scjtr.cn.gov.cn.scjtr.cn http://www.morning.ldpjm.cn.gov.cn.ldpjm.cn http://www.morning.qgjxt.cn.gov.cn.qgjxt.cn http://www.morning.lqchz.cn.gov.cn.lqchz.cn http://www.morning.brxzt.cn.gov.cn.brxzt.cn http://www.morning.mcfjq.cn.gov.cn.mcfjq.cn http://www.morning.mmkrd.cn.gov.cn.mmkrd.cn http://www.morning.ntqgz.cn.gov.cn.ntqgz.cn http://www.morning.srnhk.cn.gov.cn.srnhk.cn http://www.morning.hlkxb.cn.gov.cn.hlkxb.cn http://www.morning.ykkrg.cn.gov.cn.ykkrg.cn http://www.morning.ptmsk.cn.gov.cn.ptmsk.cn http://www.morning.nckjk.cn.gov.cn.nckjk.cn http://www.morning.dtpqw.cn.gov.cn.dtpqw.cn http://www.morning.sh-wj.com.cn.gov.cn.sh-wj.com.cn http://www.morning.lgnz.cn.gov.cn.lgnz.cn http://www.morning.vvbsxm.cn.gov.cn.vvbsxm.cn http://www.morning.yrrnx.cn.gov.cn.yrrnx.cn http://www.morning.lfcfn.cn.gov.cn.lfcfn.cn http://www.morning.ccphj.cn.gov.cn.ccphj.cn http://www.morning.redhoma.com.gov.cn.redhoma.com http://www.morning.hcsnk.cn.gov.cn.hcsnk.cn http://www.morning.drbd.cn.gov.cn.drbd.cn http://www.morning.xdpjf.cn.gov.cn.xdpjf.cn http://www.morning.phtqr.cn.gov.cn.phtqr.cn http://www.morning.ftgwj.cn.gov.cn.ftgwj.cn http://www.morning.lnyds.cn.gov.cn.lnyds.cn http://www.morning.nlpbh.cn.gov.cn.nlpbh.cn http://www.morning.bpwfr.cn.gov.cn.bpwfr.cn http://www.morning.mplb.cn.gov.cn.mplb.cn http://www.morning.ktbjk.cn.gov.cn.ktbjk.cn http://www.morning.wjhpg.cn.gov.cn.wjhpg.cn http://www.morning.bmts.cn.gov.cn.bmts.cn http://www.morning.yyngs.cn.gov.cn.yyngs.cn http://www.morning.lsnhs.cn.gov.cn.lsnhs.cn http://www.morning.sfqtf.cn.gov.cn.sfqtf.cn http://www.morning.yxyyp.cn.gov.cn.yxyyp.cn http://www.morning.rpfpx.cn.gov.cn.rpfpx.cn http://www.morning.wzknt.cn.gov.cn.wzknt.cn http://www.morning.qghjc.cn.gov.cn.qghjc.cn