当前位置: 首页 > news >正文

【Lua热更新】上篇

Lua 热更新 - 上篇

下篇链接:【Lua热更新】下篇

文章目录

  • `Lua` 热更新 - 上篇
    • 一、`AssetBundle`
      • 1.理论
      • 2. AB包资源加载
    • 二、`Lua` 语法
      • 1. 简单数据类型
      • 2.字符串操作
      • 3.运算符
      • 4.条件分支语句
      • 5.循环语句
      • 6.函数
      • 7. table数组
      • 8.迭代器遍历
      • 9.复杂数据类型 - 表
        • 9.1字典
        • 9.2类
        • 9.3 表的公共操作
      • 多脚本执行
      • 10.协同程序
      • 11.元表
        • 11.1 index
        • 11.2 newindex
        • 11.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.LoadAsset<GameObject>("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
  • 整数相除可以转换为浮点数
  • 不支持自增自减++ –
  • 不支持位运算、三目运算符
  • Luanilfalse 才是假
  • 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 y

4.条件分支语句

  • 不支持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!")
end

5.循环语句

-- 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)
end

6.函数

-- 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
end

8.迭代器遍历

  • 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)
end

9.复杂数据类型 - 表

9.1字典
fruitDic = {["apple"] = 10, ["banana"] = 5}
print(fruitDic["apple"])
print(fruitDic.banana) -- 只有字符串可以通过.获得-- 遍历
for k, v in pairs(fruitDic) doprint(k,v) -- 传入多个参数print
end
9.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) -- 输出 10
11.2 newindex
  • newindex当赋值时,如果赋值不存在索引,将会赋值到newindex所指向的表中
meta = {name = "father"
}
sonTable = {
}
setmetatable(sonTable, meta)sonTable.name = "son"
print(sonTable.name) -- 输出sonmeta.__newindex = {}
print(sonTable.age) -- 输出nil
11.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() -- 输出1
12.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)
end

13.自带库

-- 时间
-- 系统时间
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.mrgr.cn/news/80985.html

相关文章:

  • TCP长连接、HTTP短轮询、HTTP长轮询、HTTP长连接、WebSocket的区别
  • vue的知识点
  • 防御保护作业二
  • 单片机之基本元器件的工作原理
  • BiGRU双向门控循环单元多变量多步预测,光伏功率预测(Matlab完整源码和数据)
  • 【MQ】Spring3 中 RabbitMQ 的使用与常见场景
  • Restaurants WebAPI(三)——Serilog/
  • BenchmarkSQL使用教程
  • 使用RTP 协议 对 H264 封包和解包
  • 使用“NodeMCU”、“红外模块”实现空调控制
  • Day12 梯度下降法的含义与公式
  • php各个版本的特性以及绕过方式
  • 基础电路的学习
  • 在VBA中结合正则表达式和查找功能给文档添加交叉连接
  • 分析excel硕士序列数据提示词——包含对特征的筛选
  • k8s迁移——岁月云实战笔记
  • JWT令牌与微服务
  • Pytorch | 利用MI-FGSM针对CIFAR10上的ResNet分类器进行对抗攻击
  • GTID详解
  • (耗时4天制作)详细介绍macOS系统 本博文含有全英版 (全文翻译稿)
  • 本地计算机上的MySQL服务启动后停止(connection refused: connect)解决一系列数据库连接不上的问题
  • 在UE5中调用ImGui图形界面库
  • [创业之路-202]:任正非管理华为的思想与毛泽东管理党、军队、国家的思想的相似性与差异性
  • Linux——卷
  • 【day09】面向对象编程进阶
  • 汽车IVI中控开发入门及进阶(43):NanoVG