Subclassing SCNScene and SceneKit Editor

拈花ヽ惹草 提交于 2019-12-02 13:16:12

I have a workaround for that problem. Not nice but it works. I instantiate a SCNScene(named: "art.scnassets/testScene.scn") then I instantiate a TheSubclassScene() and I clone the rootNode of the scene and add it as a child node to the subclass scene.

let testScene = SCNScene(named:"art.scnassets/testScene.scn")!
let subclassScene = TheSubclassScene()
subclassScene.rootNode.addChildNode(testScene.rootNode.clone())

.scn files are just SCNScene instances archived and written to disk using NSKeyedArchiver. What + sceneNamed: does is that it simply unarchives the scene with a NSKeyedUnarchiver.

What you could do then, is to try to use - setClass:forClassName: to instantiate a subclass instead of a SCNScene.

That being said, SCNScene is not really meant to be subclassed. Instead you could implement your logic in the view controller or a game controller (possibly a direct subclass of NSObject). This controller will also likely conform to SCNSceneRendererDelegate to implement your game logic.

Note: don't give your custom subclass a name starting with "SCN"; that prefix is for Apple.

The only difference I can see is calling SCNSceneSubclass(named:) vs SCNSceneSubclass(). Without seeing your code, my hunch is that your implementation of SCNSceneSubclass(named:) does not call through to your subclass's initialization, and thus misses some key steps.

Can we see a bit of your subclass's source code?

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!