Created
June 14, 2019 00:25
-
-
Save ermik/1efd98c86fe666e82e7295283356aeaf to your computer and use it in GitHub Desktop.
GKScene extension for framework-local GameplayKit scene assets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Adds a convenience initializer for loading scenes from framework targets. | |
public extension GKScene { | |
/// Loads the bundle-specific SpriteKit scene file, creating a GKScene object containing the SpriteKit scene and associated GameplayKit objects. | |
/// - Parameters: | |
/// - filename: The name of a scene file in the given bundle. | |
/// - bundle: The bundle containing the scene file. | |
/// | |
/// - Returns: A new GameplayKit scene. | |
/// | |
/// Use this initializer to load SpriteKit scenes (.sks files) that are not local to your application's main bundle. | |
convenience init?(fileNamed filename: String, in bundle: Bundle) { | |
guard | |
let main = Bundle.main.resourcePath, | |
let full = bundle.url(forResource: filename, withExtension: "sks")?.path | |
else { | |
return nil | |
} | |
let relative = full.replacingOccurrences(of: "\(main)/", with: "") | |
self.init(fileNamed: relative) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Currently this only works on real devices.