本文介绍了如何在Swift中的SCNScene上覆盖SKScene?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

互联网上有一些叠加教程,在某些时候都使用
overlaySKScene

There are some overlay tutorials on the Internet, all usingoverlaySKScene at some point.

这在我的项目中是不可能的,因为(我猜)我的视图在任何时候都没有使用 SCNView 的构造函数( overlaySKScene 是其中的一部分。

That is somehow not possible in my project, as (I guess) my view does not use the constructor of SCNView at any point (which overlaySKScene is a part of).

在ViewController的viewDidLoad中,基本上只创建了MainScene:

In the ViewController's viewDidLoad, basically only the MainScene is created:

viewDidLoad() {
self.sceneView = MainScene(view: self.view)) }

...这里(注意:SCNScene而不是SCNView):

...which goes here (note: SCNScene instead of SCNView):

class MainScene: SCNScene, SCNPhysicsContactDelegate {...
init(view:UIView) {
scnView = view as! SCNView
super.init()
scnView.scene = self; (...) }

场景完美创建,我现在想要覆盖SKScene。有谁知道怎么做?

The scene is perfectly created, I would now like to overlay a SKScene. Does anyone know how?

推荐答案

SpriteKit叠加在SceneKit视图上,而不在SceneKit场景上。这有点令人困惑,因为你在一个视图上覆盖一个场景。

The SpriteKit overlay goes on the SceneKit view, not on the SceneKit scene. This is a bit confusing because you're overlaying a scene on a view.

我看到几个可能的错误来源:

I see several possible error sources:

self.sceneView = MainScene(view: self.view))

定义为返回 SCNScene 。您将其分配给需要 SCNView 的房产。

as defined returns an SCNScene. You're assigning that to a property that expects an SCNView.

该行

scnView = view as! SCNView

将崩溃,除非 view 正确返回已连接 SCNView 实例。但是你写的 init 定义需要 UIView

will crash unless view returns a properly connected SCNView instance. But the init definition you've written expects a UIView.

某处,您需要将您的视图设为 SCNView 。该视图,因为它符合协议 SCNSceneRenderer ,将有一个 overlaySKScene 属性( overlaySKScene 来自该协议,而不是来自 SCNView )。您可以在此处分配 SKScene 实例。

Somewhere, you need to have your view be an SCNView. That view, because it conforms to protocol SCNSceneRenderer, will have an overlaySKScene property on it (overlaySKScene comes from that protocol, not from SCNView). That's where you can assign your SKScene instance.

如果您已经这样做了,那么您的代码看起来就像

If you have done that, then your code would look something like

scnView.scene = self
scnView.overlaySKScene = theSKScene

我有一个简单的例子, SKScene 覆盖在 SCNView上

I have a simple example of an SKScene overlaid on an SCNView at https://github.com/halmueller/ImmersiveInterfaces/tree/master/Tracking%20Overlay

参见

See also How do I create a HUD on top of my Scenekit.scene.

这篇关于如何在Swift中的SCNScene上覆盖SKScene?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!