xcode - SpriteKit unarchiveFromFile 不会从 Assets 目录和图集中加载纹理

标签 xcode macos swift sprite-kit

我对 SpriteKit 还是个新手,想看看我是否可以使用 Xcode 的 SKS 编辑器构建关卡。当我添加几个带有“Spaceship.png”纹理的 Sprite 并构建模板应用程序时,纹理不会加载。

这是使用 Swift 作为语言并添加提供的“Spaceship.png” Sprite 的原始 OSX 游戏模板的屏幕截图。纹理显示良好: Sprite texture shows fine in the SKScene editor 这是仅对模板进行修改后构建和运行应用程序的结果: enter image description here

调试控制台显示此警告消息: enter image description here 我尝试添加一个 .atlas 文件夹,结果相同。场景只显示红色 X 图标代替 Sprite 。如果添加的 Sprite 只是彩色 Sprite ,它们显示良好。前段时间我废弃了一个应用程序,我曾经在其中加载 SKScene 并将 Sprite Assets 手动添加到我的 SKScene 子类中,并且运行良好。

但是,如果我将纹理(例如“Spaceship.png”)移动到项目的根目录,即不在 Assets 目录或 .atlas 文件夹中,则场景会加载显示良好的纹理。

这是添加到项目根目录的纹理:

enter image description here

这是期望的结果: enter image description here 我尝试通过 enumerateChildNodesWithName(_,usingBlock) 手动将 SKS 文件中加载的资源添加到场景中,如果纹理不在项目文件夹的根目录中,我会得到相同的结果。

这是我尝试手动添加 Assets :

    func applicationDidFinishLaunching(aNotification: NSNotification) {
    /* Pick a size for the scene */
    if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
        let sceneToBePresented = GameScene()

        /* Set the scale mode to scale to fit the window */
        sceneToBePresented.scaleMode = .AspectFill
        sceneToBePresented.size = scene.size

        /* Sprite Kit applies additional optimizations to improve rendering performance */
        self.skView!.ignoresSiblingOrder = true

        self.skView!.showsFPS = true
        self.skView!.showsNodeCount = true
        scene.enumerateChildNodesWithName("*") { node, stop in
            sceneToBePresented.addChild(node.copy() as SKSpriteNode)
        }

        self.skView!.presentScene(sceneToBePresented)
    }
}

我查看了 SpriteKit 类 - SKNodeSKSceneSKTextureSKSPriteNode - 寻找关于路径的任何线索、缓存、预加载或其他任何东西,但无法找到可以使这项工作正常进行的事情。

我正在运行 Xcode 6.1.1,我的目标是 10.9,我使用的语言是 Swift,尽管使用 ObjC 也有同样的行为。这是功能还是错误?还有其他人遇到过类似情况吗?

在此先感谢您的帮助

[更新]

看起来这还没有实现——从 Assets 目录加载纹理——因为开发论坛中的这篇帖子讨论了同样的问题,他的解决方案是将 Assets 目录重命名为与纹理相同的名称。本质上,我发现根文件夹中有图像文件:How do you load sprite textures from Images.xcassets when using SKS scene file ,虽然我设法回滚到工作状态的早期应用程序确实从 .atlas'es 加载纹理,但我似乎无法使用干净的模板来完成!!!

最佳答案

[在这里回答我自己的问题]

据我所知,目前,从 Assets 目录加载纹理 - Images.xcassets - 同时取消归档或反序列化 SKScene 文件在 OSX 上不起作用基于我的尝试和上面引用的 devforumns 帖子。

然而,从图像图集中加载纹理是通过强制 SKTexture 加载图像来实现的。可以通过 preloadWithCompletionHandler(_:)preloadTextures(_:, withCompletionHandler:) 方法强制或“触摸”SKTexture,或者正如我所发现的,只需打印 Sprite 的 SKTexture 的描述即可。

为了任何可能需要进一步帮助的人的利益,这里是一个代码片段,它在解压缩 SKS 文件后预加载纹理:

if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
    for child in scene.children as [SKNode] {
        if child is SKSpriteNode {
            let sprite = child as SKSpriteNode
            sprite.texture?.preloadWithCompletionHandler({ })
            /* or even a simple debug log of the texture */
            /* println(sprite.texture) */
        }
    }
    /* Do your other stuff ... */
    ...
}

如果我说错了请指正。在有人这样做之前,或者 Apple 修复了 SpriteKit 在 iOS 和 OSX 之间的行为之间的差异,我不会寻找其他解决方案,并且会遵循 Murphey 定律:

If it works, don't fix it

关于xcode - SpriteKit unarchiveFromFile 不会从 Assets 目录和图集中加载纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28345805/

相关文章:

ios - 创建 swift 文件的副本

javascript - 从 xcode 链接 css、javascript

iphone - Xcode 中的 Target 到底是什么?

ios - 文本文件到字符串数组代码可以在 Playground 中运行,但不能在 Xcode 项目中运行

ios - 有没有办法覆盖 iOS 13 上下文菜单的(深色/浅色)用户界面样式?

macos - 错误 : Cannot install in Homebrew on ARM processor in Intel default prefix (/usr/local)

android - 无法在 MacOSX 上的 Android Studio 之外启动 Android Emulator

macos - 如何在 macOS 上使用 WKWebView 支持像 Safari 一样的 Zoom

objective-c - Swift 只读外部,读写内部属性

ios - CocoaPods 调试.xcconfig : unable to open file in target