本文介绍了SKTextureAtlas 加载和内存管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 SpriteKit 上开发了一个游戏,每个场景都有多个场景至少来自 3 个 TextureAtlas每个TextureAtlas中图像的最大尺寸为60K我的游戏因内存崩溃问题

I developing a Game on SpriteKit and have Multiple Scenes each Scene has From 3 TextureAtlas in Minimum and Maximum Size of an Image in Each TextureAtlas is 60Kmy game Crashes From Memory issue

我在每个场景中所做的就是在头文件中定义动作,例如:

what I do in Each Scene is Defining action in the Header File for Example:

initialise them in -(id)initWithSize:(CGSize)size  Function 


@interface FirstLevel : SKScene
{

    SKAction  *RedBirdAnimation;
}

在实施文件中:

-(id)initWithSize:(CGSize)size{
if(self=[super initWithSize:size])
{[self setupRedBirdActions];}
return self;
}


-(void)setupRedBirdActions{

    SKTextureAtlas *RedBirdAtlas = [SKTextureAtlas atlasNamed:@"RedBird"]; 

    SKTexture *RedBird1 = [RedBirdAtlas textureNamed:@"Redbird_01_iphone.png"];
    SKTexture *RedBird2 = [RedBirdAtlas textureNamed:@"Redbird_02_iphone.png"];
    SKTexture *RedBird3 = [RedBirdAtlas textureNamed:@"Redbird_03_iphone.png"];

    NSArray *atlasTexture = @[RedBird1, RedBird2, RedBird3];

    SKAction* atlasAnimation = [SKAction animateWithTextures:atlasTexture timePerFrame:0.2];

    RedBirdAnimation = atlasAnimation;}

在我的游戏中加载纹理图集是否有最佳实践以防止它因内存而崩溃.

is there Something Like Best Practice to Load Texture Atlas in my game to prevent it from Crashes due to Memory.

我在每个 Skscene 结束时用 Nil 制作所有 SkAction并从 All SkSpriteNode 中删除所有操作

i make all SkAction with Nil at the end of Each Skscene and remove All action from All SkSpriteNode

有什么解决办法

推荐答案

基于 SpriteKit 的游戏内存不足的问题几乎总是由开发人员使用太多纹理导致所有活动 RAM 都被纹理占用所致.我将假设您使用基于标准 SKAction 的动画方法,并且您没有任何由强 refs 或任何东西引起的奇怪内存泄漏.确定纹理消耗的 RAM 量的正确方法是 (WIDTH * HEIGHT * 4/1000),这表示存储纹理所需的内存 kB 数.对于 4096x4096 纹理,即 67108 kB 或大约 68 Megs.将其存储在图集中还是作为单个纹理都没有关系.实际减少 SpriteKit 游戏使用的内存总量的关键是减少每个纹理消耗的内存量.请查看此 SpriteKitFireAnimation 示例的源代码这显示了如何减少每帧的内存使用量可以将非常复杂的 alpha 通道动画从 286 Megs 减少到 130 Megs,该动画在 64 位 iOS 系统上以 60FPS 执行.此方法仅适用于 A7 和更新的 64 位系统.如果您正在寻找质量较低但完全免费的方法,请参阅 此比较使用 SpriteKit 的非常有损的纹理压缩方法.作为最后的努力,可以将每个纹理的宽度和高度减少 1/2,然后在渲染到节点时重新缩放纹理,它看起来不太好,但在运行时使用的内存减少了 4 倍.

The problem of a SpriteKit based game running out of memory is almost always caused by the developer using so many textures that all active RAM is consumed by the textures. I am going to assume that you use the standard SKAction based animation approach and that you don't have any weird memory leaks caused by strong refs or anything. The correct way to determine the amount of RAM consumed by a texture is (WIDTH * HEIGHT * 4 / 1000), this indicates the number of kB of memory required to store the texture. For a 4096x4096 texture, that is 67108 kB or about 68 Megs. It does not matter if this is stored in an atlas or as a single texture. The key to actually reducing the total amount of memory used with a SpriteKit game is the reduce the amount of memory consumed by each texture. Please have a look at the source code for this SpriteKitFireAnimation example which shows how reducing the memory usage of each frame can lead to a reduction from 286 Megs down to 130 Megs for a very complex alpha channel animation that executes at 60FPS on a 64bit iOS system. This approach is for A7 and newer 64 bit systems only. If you are looking for lower quality but totally free approaches, see this comparison of very lossy texture compression approaches with SpriteKit. As a last ditch effort, one can reduce the width and height of each texture by 1/2 and then scale the textures back up when rendering into a node, it does not look as good, but uses 4x less memory at runtime.

这篇关于SKTextureAtlas 加载和内存管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 11:23