gpt4 book ai didi

ios - 将生成的 SKTexture 保存到文件

转载 作者:可可西里 更新时间:2023-11-01 05:27:48 26 4
gpt4 key购买 nike

I've now filed a bug for the issue below. Anyone with a good workaround?

我尝试将 SKTexture 保存到文件中,然后再次加载它,但我没有成功。可以将以下代码片段复制到 Xcode 启动项目中的 GameScene.m

我在 generateTexture 中使用了 textureFromNode,这似乎是我的问题的根本原因。如果我使用来自 sprite 的纹理,代码可以运行,并且可以看到两艘宇宙飞船。

此代码在 iOS 8 中有效,但在 Xcode7 和 iOS 9 中停止工作。我只想在提交错误报告之前验证这是一个错误。我担心的是我对 NSKeyedArchiver 做错了什么。

它在模拟器和设备上都会发生。

#import "GameScene.h"

@implementation GameScene

// Generates a texture
- (SKTexture *)generateTexture
{
SKScene *scene = [[SKScene alloc] initWithSize:CGSizeMake(100, 100)];

SKShapeNode *shapeNode = [SKShapeNode shapeNodeWithRectOfSize:CGSizeMake(50, 50)];
shapeNode.position = CGPointMake(50, 50);
shapeNode.strokeColor = SKColor.redColor;
shapeNode.lineWidth = 10;
[scene addChild:shapeNode];

SKTexture *texture = [self.view textureFromNode:scene];
//SKTexture *texture = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"].texture; // This works!

return texture;
}

// Just generate a path
- (NSString *)fullDocumentsPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *yourFileName = [documentsDirectory stringByAppendingPathComponent:@"fileName"];

return yourFileName;
}

- (void)didMoveToView:(SKView *)view
{
self.scaleMode = SKSceneScaleModeResizeFill;

// Verify that the generateTexture method indeed produces a valid texture.
SKSpriteNode *s1 = [SKSpriteNode spriteNodeWithTexture:[self generateTexture]];
s1.position = CGPointMake(100, 100);
[self addChild:s1];

// Start with saving the texture.
NSString *fullName = [self fullDocumentsPath];
NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];
if ([fileMgr fileExistsAtPath:fullName])
{
[fileMgr removeItemAtPath:fullName error:&error];
assert(error == nil);
}
NSDictionary *dict1 = [NSDictionary dictionaryWithObject:[self generateTexture] forKey:@"object"];
bool ok = [NSKeyedArchiver archiveRootObject:dict1 toFile:fullName];
assert(ok);

// Read back the texture and place it in a sprite. This sprite is not shown. Why?
NSData *data = [NSData dataWithContentsOfFile:fullName];
NSDictionary *dict2 = [NSKeyedUnarchiver unarchiveObjectWithData:data];
SKTexture *loadedTexture = [dict2 objectForKey:@"object"];
SKSpriteNode *s2= [SKSpriteNode spriteNodeWithTexture:loadedTexture];
NSLog(@"t(%f, %f)", loadedTexture.size.width, loadedTexture.size.height); // Size of sprite & texture is zero. Why?
s2.position = CGPointMake(200, 100);
[self addChild:s2];
}

@end

Update for Yudong:

这可能是一个更相关的示例,但假设场景由 4 个层组成,有很多 Sprite 。比赛结束后,我想存储比赛结束场景的缩略图。该图像将用作按钮上的纹理。按下该按钮将开始重播比赛的电影。会有很多带有旧游戏图像的按钮,所以我需要将每个图像存储在文件中。

-(SKTexture*)generateTexture
{
SKScene *scene = [[SKScene alloc] initWithSize:CGSizeMake(100, 100)];

SKSpriteNode *ship = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
ship.position = CGPointMake(50, 50);
[scene addChild:ship];

SKTexture *texture = [self.view textureFromNode:scene];

NSLog(@"texture: %@", texture);

return texture;
}

The solution/work around:

受 Russells 代码的启发,我做了以下事情。有用!

CGImageRef  cgImg = texture.CGImage;
SKTexture *newText = [SKTexture textureWithCGImage:cgImg];

最佳答案

我已经对 SKTextures 进行了大量试验/修改。我的游戏使用 SKTextures。它是用 Swift 编写的。具体来说,我在 textureFromNode 和 textureFromNode:crop: 以及从纹理创建 SKPhysicsBodies 方面遇到了很多问题。这些方法在 ios 8 中运行良好,但 Apple 在发布 ios 9.0 时完全破坏了它们。在 ios 9.0 中,纹理返回为 nil。那些零纹理从纹理中破坏了 SKPhysicsBodies。

我最近从事 SKTextures 的序列化/反序列化工作。

您可能会调查的一些关键想法/线索是:

  1. 运行 ios 9.2。 Apple Staff 提到很多问题已得到修复。 https://forums.developer.apple.com/thread/17463我发现 ios 9.2 对 SKTextures 有帮助,但并没有解决所有问题,尤其是序列化问题。

  2. 尝试 PrefersOpenGL(在您的配置中将其设置为“YES”作为 bool 自定义属性)。这是 Apple 员工在 Apple Dev Forums 中关于 PrefersOpenGL 的帖子。 https://forums.developer.apple.com/thread/19683我观察到 ios 9.x 似乎默认使用 Metal 而不是 OpenGL。我发现 PrefersOpenGL 有助于解决 SKTexture 问题,但仍然无法使我的 SKShaders 工作(用 GLSL 编写)。

  3. 当我尝试在 ios 9.2 上使用 SKTextures 序列化/反序列化节点时,我得到的是白色框而不是可见纹理。受 Apple SKTexture 文档的启发,“纹理数据在以下情况下加载:

调用纹理对象的大小方法。

调用另一个需要纹理大小的方法,例如创建一个使用纹理对象的新 SKSpriteNode 对象。

其中一种预加载方法被调用(请参阅预加载纹理数据。)

纹理数据在以下情况准备渲染:

使用纹理的 Sprite 或粒子是正在渲染的节点树的一部分。”

...我破解了一个从 CGImage() 调用创建辅助纹理的变通方法:

        // ios 9.2 workaround for white boxes on serialization
let img = texture!.CGImage()
let uimg = UIImage(CGImage: img)
let ntex = SKTexture(image: uimg)
let sprite = SKSpriteNode(texture: ntex, size: texture!.size())

所以现在我以这种方式创建的 SKSpriteNode 似乎可以很好地序列化/反序列化。顺便说一句,仅调用 size() 或使用原始纹理创建 SKSpriteNode 似乎不足以将纹理具体化到内存中。

  1. 您没有询问有关 textureFromNode:crop: 的问题,但无论如何我都会添加观察结果以防万一它对您有帮助:我发现此方法在 ios 8 中有效(尽管裁剪参数非常棘手并且似乎需要标准化with UIScreen.mainScreen().scale) 在ios 9.0中,这个方法根本不起作用(返回nil)。在 ios 9.2 中,此方法现在可以使用(它现在返回一个非零纹理)但是随后从纹理创建节点不需要大小标准化。此外,要使序列化/反序列化工作,我发现您最终必须执行上面的 #3。

希望对您有所帮助。我想我对 SKTextures 的挣扎比大多数人都多,因为我的应用程序非常依赖它们。

关于ios - 将生成的 SKTexture 保存到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32701576/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com