Godot的文件系统和Unity的Assetbundle设计上并不一样,所以误导了一段时间。 我开始以为pckPacker就是类似打包AB的脚本,但是并不是。
1 2 3 4 5 6 7 |
var packer = new PckPacker(); packer.PckStart("test.pck"); packer.AddFile("res://Atlas/a.png.import", "Atlas/a.png.import"); packer.AddFile("res://.godot/imported/a.png-bb8a5c4a3aefa9bd4dda7c06f9f81f63.ctex", "res://.godot/imported/a.png-bb8a5c4a3aefa9bd4dda7c06f9f81f63.ctex"); packer.Flush(); |
注意看第二个Addfile方法,看到这里是不是感觉很好奇,为什么还要加这个路径?
原因是pck内部就是这样保存的。可以下载GodotPCKExplorer工具,这样就能看到pck里保存的文件结构了。如果用PckPacker就需要按照它的格式打包出资源,可以用GodotPCKExplorer反复查看构建出来的pck
PckPacker这个类并不会找依赖,比如你要打包一个场景,它并不是把场景里用的都给你打包好,而需要自己来找依赖,可以写个递归脚本动态查找。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
void GetDependencies(PckPacker packer,string url) { foreach (var full in ResourceLoader.GetDependencies(url)) { GD.Print(full); var splits = full.Split("::"); if (splits.Length > 2) { var path = splits[2].Replace("res://", ""); var import =$"{path}.import"; packer.AddFile(import, import); GD.Print(import); ConfigFile config = new ConfigFile(); config.Load(import); var importPath = config.GetValue("remap", "path", "").AsString(); if (!string.IsNullOrEmpty(importPath)) { packer.AddFile(importPath, importPath); GD.Print(importPath); } GetDependencies(packer,path); } } } |
这里也是误导我的地方,我以为godot也需要像assetbundle一样创建很多,其实godot更像是一种append的方式。只要把修改过的资源重新打包到pck中,并且 ProjectSettings.LoadResourcePack重新加载一下它,后续相同的加载资源代码就会加载出新资源。
1 2 3 4 5 6 7 8 |
//老资源 GetNode<TextureRect>("TextureRect").Texture = ResourceLoader.Load<Texture2D>("res://Atlas/a.png"); GD.Print(OS.GetUserDataDir()); //可下载目录 //新资源 var success = ProjectSettings.LoadResourcePack("user://test.pck"); GetNode<TextureRect>("TextureRect").Texture = ResourceLoader.Load<Texture2D>("res://Atlas/a.png"); |
所以如果下载热更的话,只需要在游戏启动开始把所有新的pck重新加载就好。
现在问题就来了,如何找到差异呢?目前来看感觉godot的思路还是需要做二进制diff,steam平台因为默认就支持二进制diff,所以可以每次都打包一个整包,但是移动端就需要开发者自己来做了。目前来看正确的做法是要先Export中选择需要热更差异的资源,如果需要脚本设置可以用脚本修改右边的.cfg文件,在项目的根目录下。 最终点击Export Pck/Zip导出。
zip后会进行压缩,文件大小会小,但是每次加载资源需要解压速度会慢。
- 本文固定链接: https://www.xuanyusong.com/archives/5166
- 转载请注明: 雨松MOMO 于 雨松MOMO程序研究院 发表
捐 赠写博客不易,如果您想请我喝一杯星巴克的话?就进来看吧!