Hello,
Do you know if you can use the swiftUI framework with Spritekit framework ? like using the SwiftUI interface to display spritekit scenes?
Thanks !
Hello,
Do you know if you can use the swiftUI framework with Spritekit framework ? like using the SwiftUI interface to display spritekit scenes?
Thanks !
This should definitely be possible since SKView is an UIView, but it should not be very easy 😟.
Check this documentation on how to merge SwiftUI and UIViews.
Hope they make this task easier in the near future.
Cheers
It sure is possible, and might get much better when the final Xcode 11 is going to be released.
Take a look at my long answer here:
Code Block swift import SwiftUI import SpriteKit struct SpriteKitContainer: UIViewRepresentable { typealias UIViewType = SKView var skScene: SKScene! init(scene: SKScene) { skScene = scene self.skScene.scaleMode = .resizeFill } class Coordinator: NSObject { var scene: SKScene? } func makeCoordinator() -> Coordinator { let coordinator = Coordinator() coordinator.scene = self.skScene return coordinator } func makeUIView(context: Context) -> SKView { let view = SKView(frame: .zero) view.preferredFramesPerSecond = 60 view.showsFPS = true view.showsNodeCount = true return view } func updateUIView(_ view: SKView, context: Context) { view.presentScene(context.coordinator.scene) } } struct SpriteKitContainer_Previews: PreviewProvider { static var previews: some View { /*@START_MENU_TOKEN@*/Text("Hello, World!")/*@END_MENU_TOKEN@*/ } }
Code Block swift import UIKit import SpriteKit class SpriteScene: SKScene { /*change the code below to whatever you want to happen on skscene*/ override func didMove(to view: SKView) { physicsBody = SKPhysicsBody(edgeLoopFrom: frame) } override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { guard let touch = touches.first else { return } let location = touch.location(in: self) let box = SKSpriteNode(color: .red, size: CGSize(width: 50, height: 50)) box.position = location box.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 50, height: 50)) addChild(box) } }
Code Block swift import SwiftUI struct ContentView: View { var body: some View { SpriteKitContainer(scene: SpriteScene()) } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }