Overlay SKScene is not showing

2019-05-06 18:37发布

I'm trying to overlay an SKScene over a SCNScene. When I run my app on the simulator and an iPhone6+, the overlayScene(SKScene) is shown as intended, but when I tried running it on the iPhone5 (tried 2 different devices) the overlayScene does not appear.

Does anyone have an idea why? Both device run on iOS 9.2.1. I have only tested the app on those two models. Here's my code inside the GameViewController.Swift

    //Configure the view
    sceneView = self.view as? SCNView

    //Configure the scene
    sceneView.scene = gameScene
    sceneView.playing = true
    sceneView.backgroundColor = UIColor.blackColor()
    sceneView.antialiasingMode = SCNAntialiasingMode.Multisampling4X

    let overlayScene = OverlayScene(size: self.view.bounds.size)
    sceneView.overlaySKScene = overlayScene

Thanks!

1条回答
Anthone
2楼-- · 2019-05-06 19:24

I got this solution from the developer forum mentioned by @StephenT above, but I thought some code might help.

In the stock/template code provided by Xcode, the Storyboard specifies the top level view as being an SKView, and that the initial SCNScene should be added to that via an SCNView object. This meant that when I added an SKView to the SCNScene, I experienced the problem whereby the SKView would not be shown on any devices that use OpenGL (as opposed to Metal).

So the solution that works for me is:

Main.storyboard -> ViewController -> view

should be set to be an SCNView, not an SKView.

Then, in the ViewController:

- (void) viewDidLoad {
    [super viewDidLoad];

    // Configure the root view.
    //
    SCNView *scnView = (SCNView*)self.view;

    WorldSceneView *world = [[WorldSceneView alloc] initWithFrame:self.view.frame];

    // Create and configure the scene.
    //
    HUDScene *hudScene = [HUDScene hudSceneWithSize:scnView.bounds.size andHUDDelegate:world];

    // Associate the HUD SKScene with the SCNView.
    //    
    world.overlaySKScene = hudScene;

    [scnView addSubview:world];
}

This works a treat for me, on both Metal and OpenGL devices, as well as the Simulator.

查看更多
登录 后发表回答