virtual game controller + SwiftUI warning

Hi,

I've just moved my SpriteKit-based game from UIView to SwiftUI + SpriteView and I'm getting this mesage

Adding 'GCControllerView' as a subview of UIHostingController.view is not supported and may result in a broken view hierarchy. Add your view above UIHostingController.view in a common superview or insert it into your SwiftUI content in a UIViewRepresentable instead.

Here's how I'm doing this

struct ContentView: View {
    @State var alreadyStarted = false
    let initialScene = GKScene(fileNamed: "StartScene")!.rootNode as!  SKScene
    var body: some View {
        ZStack {
            SpriteView(scene: initialScene, transition: .crossFade(withDuration: 1), isPaused: false , preferredFramesPerSecond: 60)
                .edgesIgnoringSafeArea(.all)
                .onAppear {
                    if !self.alreadyStarted {
                        self.alreadyStarted.toggle()
                        initialScene.scaleMode = .aspectFit
                    }
                }
            VirtualControllerView()
                .onAppear {
                    let virtualController = BTTSUtilities.shared.makeVirtualController()
                    BTTSSharedData.shared.virtualGameController = virtualController
                    BTTSSharedData.shared.virtualGameController?.connect()
                }
                .onDisappear {
                    BTTSSharedData.shared.virtualGameController?.disconnect()
                }
        }
    }
}

struct VirtualControllerView: UIViewRepresentable {
    
    func makeUIView(context: Context) -> UIView {
        let result = PassthroughView()
        return result
    }
    
    func updateUIView(_ uiView: UIView, context: Context) {
    }
}

class PassthroughView: UIView {
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        for subview in subviews.reversed() {
            let convertedPoint = convert(point, to: subview)
            if let hitView = subview.hitTest(convertedPoint, with: event) {
                return hitView
            }
        }
        
        return nil
    }
}
Answered by DTS Engineer in 859520022

Hello,

We currently lack API for associating a touch controller with a SpriteKit view or scene.

Please send us an enhancement request.

The warning message can be ignored.

Accepted Answer

Hello,

We currently lack API for associating a touch controller with a SpriteKit view or scene.

Please send us an enhancement request.

The warning message can be ignored.

virtual game controller + SwiftUI warning
 
 
Q