您当前的位置: 首页 >  unity

野奔在山外的猫

暂无认证

  • 2浏览

    0关注

    85博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【Unity】Lua热更新 笔记(二)

野奔在山外的猫 发布时间:2021-04-22 20:07:22 ,浏览量:2

5.7.3 index

如下所示,这是一个基本的表。

father = { house="四合院"}
son = {car="BMW"}

print(son.car, son.house)  --BMW, nil
5.7.3.1 子表读取父表

我们需要在father与son之间添加下列表,表示连接表,特别注意.__index为双_

-- t :自己  key:属性
father = { house="四合院"}
son = {car="BMW"}

father.__index = function(t, key)
	return father[key]
end
--~ father.__index=father

setmetatable(son, father)  --设置son的源表是father,使得son与father有关系
print(son.car, son.house)  --BMW 四合院

简化写法:father.__index=father

5.7.3.2 父表读祖父表
grandfather = { boat="豪华游轮"}
grandfather.__index = grandfather

father = { house="四合院"}
father.__index = father
setmetatable(father, grandfather)

son = {car="BMW"}
setmetatable(son, father)

daughter = {}
setmetatable(daughter, father)

print(son.boat, father.boat) --豪华游轮 豪华游轮

Lua不存在面向对象思想,与类说法。

5.7.4 opp
Fruit = { price = 5}
Fruit.__index = Fruit

--创建实例 Fruit类
function Fruit : new(price)
	local t = {}
	t.price = price
	setmetatable(t, Fruit)
	return t
end

function Fruit : printPrice()
	print("Fruit price :" .. self.price)
end

local f = Fruit : new(10)

print(f.price) --10
f : printPrice() --Fruit price: 10
5.7.5 封装

封装Fruit给Apple使用

--Fruit
Fruit = { price = 5 }
Fruit.__index = Fruit

function Fruit : new(price)
	local t = {}
	t.price = price
	setmetatable(t, Fruit)
	return t
end

function Fruit : printPricr()
	print("Fruit Price:" .. self.price)
end

Apple封装

require "Fruit"  --加载模块

Apple = Friut : new()
Apple.__index = Apple

function Apple : new(price, color)
	local t = {}
	t.price = price
	t.color = color or "red"
	setmetatable(t, Apple)
	return t
end
--输出价格
function Apple : printPrice()
	print("Apple Price:" .. self.price)
end
--输出颜色
function Apple : printColor()
	print("Apple Color:" .. self.color)
end

local a = Apple : new(10, "yellow")
a : printPrice() --Apple Price:10
a : printColor() --Apple Color:yellow
6、其他问题
require "Fruit"
local f = Fruit : new(10)
f : printPrice() --Fruit Price:10

Fruit.printPrice = function(t) 
	t.price = 0
	print("Fruit Price:" .. t.price) --Fruit Price:0
end

f : printPrice() --Fruit Price:0
f = Fruit : new(8)
f : printPrice() --Fruit Price:0 由于上代码将全局变量都改了

--解决方法
require "Fruit"
f = Fruit : new(8)
f : printPrice() --Fruit Price:8
六、热更新原理

Unity的热更新需要涉及到3个目录: 在这里插入图片描述 流程:

  • 操作①仅第一次操作出现,是将游戏包资源文件拷贝至数据目录(后续将不再执行)
  • 操作②请求网络资源,检查是否更新资源
  • 操作③启动游戏程序
6.3 游戏资源目录:

包含Unity工程中StreamingAssets文件夹下的文件。安装游戏之后,这些文件将被复制到目标机器上特定的文件夹内。 注意:不同平台下的目录路径不一。

平台路径Mac OS或WindowsApplication.dataPath + "/StreamingAssets"IOSApplication.dataPath + "/Raw"Android"jar:file://" + Application.dataPath + "!/assets/" 6.2 数据目录

“游戏资源目录”在Andriod、IOS上只读,无法将下载更新的资源放置其中。需建立一个“数据目录”,该目录可读写。 在第一次启动游戏后,程序会将“游戏资源目录”的内容复制到“数据目录中”。操作① 游戏过程中的资源加载,都将从“数据目录”中获取、解包。操作③ 注意:不同平台下的目录路径不一。

平台路径Mac OS或WindowsC:/LuaFramework/"Android或IOSApplication.persistendDataPath + "/LuaFramework""调试模式下Application.dataPath + “/StreamingAssets/” 6.3 网络资源地址

存放游戏资源的网址,游戏开始后,程序会从网络资源地址下载一些更新的文件到数据目录。

此目录下包含不同版本的资源文件,以及用于版本控制的files.txt。程序会优先下载此文件,然后与“数据目录”中文件的MD5码作比较,更新有变化的文件。操作②

LuaFramework的热更新代码定义在Assets\LuaFramework\Scripts\Manager\GameManager.cs【根据实际情况配置路径】

释放资源

//释放资源
void CheckExtractResource()
{
	bool isExists = Dictionary.Exists(Util.DataPath) 
		&& Dictionary.Exists(Util.DataPath + "lua/") 
		&& Dictionary.Exists(Util.DataPath + "lua/");
	if (isExists || AppConst.DebugMode) 
	{
		StartCoroutine(OnUpdateResource());
		return;
	}
	StartCoroutine(OnExtractResource());  //启用释放协议
}

IEnumerator OnExtractResource()
{
	string dataPath = Util.DataPath; //数据目录
	string resPath = Util.AppContentPath(); //游戏包资源目录
	
	if(Directory.Exists(dataPath) && Directory.Delet(dataPath, true))
	{
		Directory.CreateDirectory(dataPath);
	}
}

启用更新资源

IEnumerator OnUpdateMode()
{
	if(!AppConst.UpdateMode)
	{
		OnResourceInited();
		yield break;
	}
	string dataPath = Util.DataPath;  //数据目录
	string url = AppConst.WebUrl;
	string message = string.Empty;
	string random = DateTime.Now.ToString("yyyymmddhhmmss");
	string listUrl = url + "files.txt?v=" + random;
	Debug.LogWarning("LoadUpdate ——> " + listUrl);

	WWW www = new WWW(listUrl);
	yield return www;
	if(www.error != null)
	{
		OnUpdateFaild(string.Empty);
		yield break;
	}
}
关注
打赏
1659777066
查看更多评论
立即登录/注册

微信扫码登录

0.3279s