Swift:如何处理内存中的许多纹理 [英] Swift : How to handle a lot of textures in memory

查看:122
本文介绍了Swift:如何处理内存中的许多纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的游戏中有很多角色,因此我有很多纹理.加载纹理图集(包含约5种不同的图像纹理)时,它会增加内存使用量并将其保持在该数量.因此,更多的纹理只会不断增加该数量,直到有时应用程序崩溃.我不需要一次所有的字符,当我需要它们时我该如何加载一些字符纹理,而当我不需要它们时又要重新分配其他字符纹理,但病态需要能够将其恢复.

I have a lot of characters in my game and because of that I have so many textures. When a texture atlas is loaded (containing about 5 different image textures) it increases the memory use and keeps it there at that amount. So the more textures just keeps driving that number up and up until sometimes the application crashes. I don't need all the characters at once, how can i maybe load some character textures when I need them and deallocate the others when i don't, but ill need to be able to bring it back.

推荐答案

规则1

首先,您不需要在内存中手动加载纹理图集. 使用此代码创建精灵时

Rule 1

First of all you don't need to manually load in memory your texture atlas. When you create a sprite with this code

let sprite = SKSpriteNode(imageNamed: "Dog")

SpriteKit将查找名为Dog的图像,如果找不到,则会自动在所有纹理图集中查找图像.

SpriteKit looks for an image named Dog and if it cannot find it then it automatically looks for the image inside all your texture atlases.

当在纹理图集内找到图像时,整个纹理图集会自动加载到内存中.

When the image is found inside a texture atlas the whole texture atlas is automatically loaded in memory.

这就是为什么您可以避免手动加载纹理图集的原因.

This is why you can avoid manually loading the texture atlas.

规则2

不要担心从内存中删除纹理图集

Rule 2

Don't bother about removing the texture atlas from memory

当停止使用给定纹理集内的所有图像时,它将自动从内存中删除.如果系统具有足够"的内存,这种情况不会立即发生,但最终会发生.

When you stop using all the images inside a given texture atlas it is automatically removed from memory. This could not happen immediately if the system has "enough" memory but it will happen eventually.

这就是为什么您不需要从内存中手动删除纹理图集的原因

This is why you don't need to manually remove a texture atlas from memory

你能做什么?

您应该按照游戏逻辑将纹理分组为多个纹理图集.

What can you do?

You should group your textures into several texture atlases following the logic of your game.

如果您有30个纹理,并且您知道将仅同时使用1 ... 10 OR 11 ... 20 OR 21 ... 30,则创建3个纹理图集,如下所示:

If you have 30 textures and you know that only the 1...10 OR 11...20 OR 21...30 will be used at the same time then create 3 texture atlases like follow:

  • TextureAtlas1:图像从1到10
  • TextureAtlas2:图像从11到20
  • TextureAtlas3:图像从21到30

这将使SpriteKit的工作更加有效.

This will make the SpriteKit work more effective.

这篇关于Swift:如何处理内存中的许多纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆