使用 .sks 文件来布局复杂对象与 SKScene 文件 [英] Using an .sks file to layout a complex object vs a SKScene file

查看:37
本文介绍了使用 .sks 文件来布局复杂对象与 SKScene 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用SceneEditor来布局&创建 SKSpriteNode 的复杂子类,加载该信息并根据 sks 文件在场景中创建自定义对象?

我的场景是我有一个弹出对话框(只不过是 SKSpriteNodes 的子类),对话框中有很多子项.我希望在场景编辑器中布置它,类似于我布置 SKScene 的方式,然后在需要时将它呈现在我的场景中.

我意识到我可以在我的 GameScene.sks 文件中创建这个对象,但发现这些对象很容易变得混乱,并认为如果每个人都有自己的 sks 文件,这可能是存储这些拨号的更好方法.

我已经尝试扩展 SKSpriteNode 来访问类似于场景文件的文件,但它没有用

if let teamDialog = SKSpriteNode.spriteWithClassNamed(className: "TeamDialog", fileName: "TeamDialog.sks") { }扩展 SKSpriteNode {static func spriteWithClassNamed(className: String, fileName: String) ->SKSpriteNode?{守卫让对话 = SKSpriteNode(fileNamed: fileName) else {返回零}返回对话框}}

解决方案

你可以使用这个委托 https://github.com/ice3-software/node-archive-delegate

或者,像这样在 swift 中 smt:

class func unarchiveNodeFromFile(file:String)->SK节点?{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(s​​elf.classForKeyedUnarchiver(),forClassName:SKNode")让 node = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) 作为 SKNodearchiver.finishDecoding()返回节点} 别的 {返回零}}

Swift 3 的更新并修复类转换:

func unarchiveNodeFromFile(file:String)->SK节点?{if let path = Bundle.main.path(forResource: file, ofType: "sks") {让 fileData = NSData.init(contentsOfFile: 路径)let archiver = NSKeyedUnarchiver.init(forReadingWith: fileData as Data!)archiver.setClass(SKNode.classForKeyedUnarchiver(), forClassName: "SKScene")let node = archiver.decodeObject(forKey: NSKeyedArchiveRootObjectKey) as!SK节点archiver.finishDecoding()返回节点} 别的 {返回零}}

Is it possible to use the SceneEditor to layout & create a complex Subclass of SKSpriteNode, load that information and create a custom object in your scene based on the sks file?

My scenario is that I have a popup dialogs (which are nothing more than subclasses of SKSpriteNodes) with a lot of children on the dialog. I was hoping to lay it out in the Scene editor similar to how I lay out an SKScene, and then present it in my scene when needed.

I realize that I could just create this object in my GameScene.sks file, but find that those can get easily cluttered, and thought it might be a nicer way to store these dialgos if each had their own sks file.

I've tried extending SKSpriteNode to access the file similar to the Scene file but it didn't work

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

解决方案

You can use this delegate https://github.com/ice3-software/node-archive-delegate

Or, smt like this in swift:

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

UPDATE for Swift 3 and fix class casting:

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

这篇关于使用 .sks 文件来布局复杂对象与 SKScene 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆