从另一个包中加载一个spritekit场景? [英] Load a spritekit scene from another bundle?
问题描述
我正在使用swift 4.2制作SpriteKit框架,并希望包含一些用于场景和动作的.sks文件.我尝试使用以下代码从捆绑包中加载场景:
I am making a SpriteKit framework using swift 4.2 and want to include some .sks files for scenes and actions. I have tried to load the scene from the bundle using the code below:
class func newGameScene() -> GameScene {
guard let gameScenePath = Bundle(for: self).path(forResource: "GameScene", ofType: "sks") else { assert(false) }
guard let gameSceneData = FileManager.default.contents(atPath: gameScenePath) else { assert(false) }
let gameSceneCoder = NSKeyedUnarchiver(forReadingWith: gameSceneData)
guard let scene = GameScene(coder: gameSceneCoder) else { assert(false) }
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
return scene
}
我加载了场景并展示了它. (此代码主要来自苹果公司用于SpriteKit的模板,因为我正在测试此问题.)
I load the scene and present it. (This code is mostly from Apple's template for SpriteKit as Im testing this issue.)
guard let view = view else {
return nil
}
let scene = GameScene.newGameScene()
view.presentScene(scene)
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
return nil
在这种情况下,GameScene.sks和代码与Apples模板相同.该代码和.sks资产位于动态框架中,并导入到另一个项目中.
The GameScene.sks and the code is unchanged from Apples template in this case. This code and the .sks assets are in the dynamic framework and imported into another project.
当框架将场景加载到视图中时,我通过了它,它显示了fps和节点数,但没有显示"Hello,World!".文本.
When having the framework load the scene into a view I pass it, it shows the fps and node count but not the "Hello, World!" text.
在下面的代码(同样也是从模板复制的)中,一个断点表示鼠标悬停时不会调用这些代码.
In the code below, also copied from the template, a break point shows that these are not called when mousing down.
#if os(OSX)
// Mouse-based event handling
extension GameScene {
override func mouseDown(with event: NSEvent) {
if let label = self.label {
label.run(SKAction.init(named: "Pulse")!, withKey: "fadeInOut")
}
self.makeSpinny(at: event.location(in: self), color: SKColor.green)
}
override func mouseDragged(with event: NSEvent) {
self.makeSpinny(at: event.location(in: self), color: SKColor.blue)
}
override func mouseUp(with event: NSEvent) {
self.makeSpinny(at: event.location(in: self), color: SKColor.red)
}
}
#endif
我知道这必须与SpritKit如何加载场景但找不到解决方案有关.我必须使用NSKeyedUnarchiver,因为SpritKit内置了文件初始化程序:
I know it must have to do with how SpritKit loads the scene but cannot find a solution. I have to use an NSKeyedUnarchiver becuase SpritKit's built in file initializer:
GameScene(fileNamed: "GameScene")
仅从主捆绑包加载.
现在,在上文中,我假设可以使用编码器加载文件,但是Tomato指出,最有可能没有使用编码器保存sks.在这种情况下,可能无法使用apple提供的api从sprite-kit中的另一个包中加载sks文件.答案可能不包括编码器.
Now in the above I assumed that the file can be loaded by using a coder but Tomato made the point that sks most likely was not saved using a coder. In that case, It may be impossible to load an sks file from another bundle in sprite-kit using the provided api from apple. The answer may not include coders.
推荐答案
就像我认为let gameSceneCoder = NSKeyedUnarchiver(forReadingWith: gameSceneData)
不能为您创建合适的编码器一样.
Just as I thought let gameSceneCoder = NSKeyedUnarchiver(forReadingWith: gameSceneData)
was not creating a proper coder for you.
只需
guard let scene = NSKeyedUnarchiver.unarchiveObject(with: gameSceneData) as? SKScene
else{
assert(false)
}
这将为您正确地取消存档文件.
This will unarchive the file properly for you.
注意,如果要使用GameScene,请确保在SKS文件的自定义类中设置了GameScene
Note, if you want to use GameScene, make sure GameScene is set in the custom class of the SKS file
这篇关于从另一个包中加载一个spritekit场景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!