是否可以使用SceneEditor来布局和创建SKSpriteNode的复杂子类,加载该信息,并基于sks文件在场景中创建自定义对象?
我的场景是,我有一个弹出式对话框(只不过是SKSpriteNodes的子类),对话框上有很多子类。我希望在场景编辑器中布置它,类似于我布置SKScene的方式,然后在需要的时候在我的场景中呈现它。
我意识到我可以只在我的GameScene.sks文件中创建这个对象,但我发现这些对象很容易变得混乱,我想如果每个对话框都有自己的sks文件,那么存储这些对话框可能是一种更好的方式。
我尝试通过扩展SKSpriteNode来访问与场景文件类似的文件,但不起作用
if let teamDialog = SKSpriteNode.spriteWithClassNamed(className: "TeamDialog", fileName: "TeamDialog.sks") { }
extension SKSpriteNode {
static func spriteWithClassNamed(className: String, fileName: String) -> SKSpriteNode? {
guard let dialog = SKSpriteNode(fileNamed: fileName) else {
return nil
}
return dialog
}
}

发布于 2017-01-25 06:05:06
您可以使用此委托https://github.com/ice3-software/node-archive-delegate
或者,像swift中这样的smt:
class func unarchiveNodeFromFile(file:String)-> SKNode? {
if let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks") {
var fileData = NSData.dataWithContentsOfFile(path, options: NSDataReadingOptions.DataReadingMappedIfSafe, error: nil)
var archiver = NSKeyedUnarchiver(forReadingWithData: fileData)
archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKNode")
let node = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as SKNode
archiver.finishDecoding()
return node
} else {
return nil
}
}Swift 3的更新和修复类类型转换:
func unarchiveNodeFromFile(file:String)-> SKNode? {
if let path = Bundle.main.path(forResource: file, ofType: "sks") {
let fileData = NSData.init(contentsOfFile: path)
let archiver = NSKeyedUnarchiver.init(forReadingWith: fileData as Data!)
archiver.setClass(SKNode.classForKeyedUnarchiver(), forClassName: "SKScene")
let node = archiver.decodeObject(forKey: NSKeyedArchiveRootObjectKey) as! SKNode
archiver.finishDecoding()
return node
} else {
return nil
}
}https://stackoverflow.com/questions/41839161
复制相似问题