如果以后使用SKTextureAtlas,Sprite Kit是否会多次加载纹理图集? [英] Does Sprite Kit load a texture atlas multiple times if I use SKTextureAtlas later?
问题描述
这让我思考.
游戏开始,我用-spriteNodeWithImageNamed:
方法创建精灵.稍后进行动画制作,我创建了一个SKTextureAtlas对象.有人说它更快,因为-spriteNodeWithImageNamed:
首先会在您的应用程序捆绑包中查找png,然后再查找地图集.
Game starts and I create sprites with -spriteNodeWithImageNamed:
method. Later for animation, I create a SKTextureAtlas object. Some people say it is faster because -spriteNodeWithImageNamed:
will first look in your app bundle for a png and after this it will look into the atlas.
但是我不清楚:如果以后创建SKTextureAtlas,这会知道已经加载的地图集图像吗?或者它会变得愚蠢,只是再次加载该图像?
But what is unclear to me: If I create a SKTextureAtlas later will this know about the already loaded atlas image or will it be dumb and just load the image again?
如果我在同一图集的多个节点中创建SKTextureAtlas对象.它会多次加载地图集吗?我是否必须确保仅对一个地图集使用一个SKTextureAtlas实例?
And if I create SKTextureAtlas object in multiple nodes for same atlas. Will it load the atlas multiple times? Must I make sure I use only one SKTextureAtlas instance for any atlas?
推荐答案
的确,spriteNodeWithImageNamed:
将首先在捆绑软件中查找文件.如果找不到捆绑软件文件,它将检查捆绑软件中可用图集中是否存在具有该名称的图像.
It's true that spriteNodeWithImageNamed:
will look for the file in the bundle first. If it can't find a bundle file, it will check if an image with that name exists in an atlas available in the bundle.
如果Sprite Kit在任何地图集中都找到了具有该名称的图像,它将自动加载该地图集,以便在使用spriteNode/initWithImageNamed:
初始值设定项时将所述图像用作sprite.这样一来,无需图集就可以轻松开始开发,之后再将文件添加到图集中.
If Sprite Kit finds an image with that name in any atlas, it will automatically load that atlas in order to use said image as the sprite when using the spriteNode/initWithImageNamed:
initializers. This makes it easy to start developing without atlases and later adding files to an atlas.
我建议从一开始就使用地图集,因为可能会有细微的差异,并且您将能够更实际地评估应用程序的性能.
I recommend to use atlases from the start because there can be subtle differences and you'll be able to assess the performance of your app more realistically.
是的,Sprite Kit非常聪明,不会重新加载内存中已经存在的资源.它也不会创建具有相同图集的新实例,而是将向您返回指向已存在的相同名称的图集的指针.
Yes, Sprite Kit is clever enough to not reload a resource that is already in memory. It will also not create a new instance of the same atlas but rather it will return you the pointer to the already existing atlas of the same name.
Sprite Kit显然还采用了一种缓存机制,因此,即使删除了对资源文件的最后一个强引用,该文件仍将保留在内存中.但是,我认为没有人对Sprite Kit最终从内存中释放缓存实例的方式,时间和顺序进行深入分析.
Sprite Kit also employs a caching mechanism apparently, so even if the last strong reference to a resource file has been removed the file will remain in memory. However I don't think anyone has done an in-depth analysis of how and when and in what order Sprite Kit eventually releases cached instances from memory.
长话短说:依靠Sprite Kit为您做正确的事.
Long story short: rely on Sprite Kit to do the right thing for you.
这篇关于如果以后使用SKTextureAtlas,Sprite Kit是否会多次加载纹理图集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!