Skip to content

Instantly share code, notes, and snippets.

@ermik
Created June 14, 2019 00:25
Show Gist options
  • Save ermik/1efd98c86fe666e82e7295283356aeaf to your computer and use it in GitHub Desktop.
Save ermik/1efd98c86fe666e82e7295283356aeaf to your computer and use it in GitHub Desktop.
GKScene extension for framework-local GameplayKit scene assets
/// 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)
}
}
@ermik
Copy link
Author

ermik commented Jun 14, 2019

Currently this only works on real devices.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment