7

我现在已经为下面的问题提交了一个错误。有人有好的解决方法吗?

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

我使用textureFromNodein generateTexture,这似乎是我的问题的根本原因。如果我使用精灵的纹理,代码可以工作,并且可以看到两艘宇宙飞船。

此代码在 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

雨东更新:

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

-(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;
}

解决方案/解决方法:

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

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

2 回答 2

8

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

我最近致力于 SKTextures 的序列化/反序列化。

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

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

  2. 尝试 PrefersOpenGL(在您的配置中将其设置为“YES”作为布尔自定义属性)。这是 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 文档的启发,该文档说:“纹理数据在以下情况下加载:

调用纹理对象的 size 方法。

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

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

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

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

...我已经破解了一个从 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())

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

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

我希望这可以帮助你。我想我在使用 SKTextures 时遇到的困难比大多数人都多,因为我的应用程序非常依赖它们。

于 2015-11-06T08:40:42.753 回答
2

我在 Xcode 7 中测试了你的代码,发现texture返回的generateTexture是 null。这就是为什么您无法从文件中加载任何内容,甚至没有保存任何内容的原因。

尝试使用NSLog来记录你的纹理或精灵的描述。例如添加这一行generateTexture

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

您将在控制台中获得什么:

纹理:“(空)”(300 x 300)

和你s1dict1代码一样:

s1:名称:'(null)' 纹理:['(null)' (300 x 300)] 位置:{100, 100} 比例:{1.00, 1.00} 大小:{100, 100} 锚点:{0.5, 0.5 } 旋转:0.00

dict1: { object = "'(null)' (300 x 300)"; }

你可以在 iOS 8 和 iOS 9 上进行这些测试,你可能会得到不同的结果。

我不知道为什么你添加SKShapeNode到 ascene然后从scene. 一种解决方法是为您的 设置纹理SKShapeNode,并且您的代码应该可以正常工作。

shapeNode.fillTexture = [SKTexture textureWithImageNamed:@"Spaceship"];
SKTexture *texture = shapeNode.fillTexture;
return texture;

更新:

textureFromNode在 iOS 9 中不能按预期工作,这很烦人。我试图通过反复试验来解决它,但最后没有运气。因此,我问你是否会考虑制作整个屏幕的快照并将其设置为缩略图。这是我今天取得的进展,希望你能从中得到启发。

我创建了一个场景,其中包含SKLabelNodeSKSpriteNodedidMoveToView在我单击屏幕上的任意位置后,snapshot将调用并将缩小的屏幕截图保存在文档文件夹中。我在这里使用了代码。

- (UIImage *)snapshot
{
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0.5);
    [self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
    UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return snapshotImage;
}

由于缩略图被保存为UIImage因此加载它为精灵的纹理应该很容易完成。一个示例项目演示了整个过程,它适用于 iOS 8 和 9。

于 2015-10-01T05:35:33.563 回答