Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
340 views
in Technique[技术] by (71.8m points)

ios - How can I overlay a SKScene over a SCNScene in Swift?

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

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).

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

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

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

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)) 

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

The line

scnView = view as! SCNView

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

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...