Using an .sks file to layout a complex object vs a SKScene file

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.

  • How do you make a static class in swift?
  • Ineligible device iPad Air2 on Xcode 6.0?
  • `-webkit-overflow-scrolling: touch` broken for initially offscreen elements in iOS7
  • Problems animating a countdown in swift sprite kit
  • How do I use the UISearchBar and UISearchDisplayController
  • Handling Image Store to Firebase with Swift
  • 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
        }
    }
    

    enter image description here

    Solutions Collect From Internet About “Using an .sks file to layout a complex object vs a SKScene file”

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