问题
Hi I am trying to remove all nodes from my Scenekit scene but I cannot for the life of me figure out a way.
It seems logical to me that there must be a function for doing this automatically but I cannot find it.
In context, I am trying to remove all nodes so I can reset my scene, which will happen reasonably often. Perhaps there is another way of doing this and I would be fine with that, I'm not stuck with having to remove all nodes.
Thanks!
回答1:
Try this (assuming you are using Swift):
rootNode.enumerateChildNodes { (node, stop) in
node.removeFromParentNode()
}
Works for me.
回答2:
For me worked like below:
sceneView.scene.rootNode.enumerateChildNodes { (node, stop) in
node.removeFromParentNode() }
回答3:
you can either create a new scene or call -[SCNNode removeFromParentNode]
on every child node of the scene's rootNode
回答4:
Where you need to remove all of your nodes, call this (if your scene isn't self
, change it):
for (SCNNode *node in [self children]) {
[node removeFromParent]
}
Additionally, if you need to remove each node except for some, call this (say, we don't want to remove 3 nodes, and they're named a
, b
, and c
)
for (SCNNode *node in [self children]) {
if (![node.name isEqualToString:@"a"] && ![node.name isEqualToString:@"b"] && ![node.name isEqualToString:@"c"]) {
[node removeFromParent]
}
}
Hope this helps!
回答5:
the simplest way I found to remove all nodes from a scene is:
self.removeAllChildren()
This worked well for me in XCode version 7.2
来源:https://stackoverflow.com/questions/28738551/how-can-i-remove-all-nodes-from-a-scenekit-scene