问题描述
网上有一些overlay教程,都是用的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
覆盖在
I have a simple example of an SKScene
overlaid on an SCNView
at https://github.com/halmueller/ImmersiveInterfaces/tree/master/Tracking%20Overlay
另请参阅 我该怎么做在我的 Scenekit.scene 之上创建一个 HUD.
这篇关于如何在 Swift 中将 SKScene 覆盖在 SCNScene 上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!