Post

Replies

Boosts

Views

Activity

Reply to SwiftUI and Spritekit integration
It is possible. You'll need to create a SpriteKitContainer.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@*/     } } After that, you need to create your skscene that will be something like this: 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)     } } And this is how you will call the spritekitscene in your ContentView: import SwiftUI struct ContentView: View {     var body: some View {         SpriteKitContainer(scene: SpriteScene())     } } struct ContentView_Previews: PreviewProvider {     static var previews: some View {         ContentView()     } } PS: you'll only see the spritekitscene working in the simulator, it won't work in the preview
Sep ’20