如何从scenekit场景中删除所有节点?

时间:2022-10-31 11:53:08

Hi I am trying to remove all nodes from my Scenekit scene but I cannot for the life of me figure out a way.

嗨,我正试图从Scenekit场景中删除所有节点,但我始终无法找到一个方法。

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!

谢谢!

5 个解决方案

#1


3  

you can either create a new scene or call -[SCNNode removeFromParentNode] on every child node of the scene's rootNode

您可以在场景的rootNode的每个子节点上创建一个新场景或调用-[SCNNode removeFromParentNode]

#2


17  

Try this (assuming you are using Swift):

试试这个(假设你正在使用Swift):

rootNode.enumerateChildNodes { (node, stop) in
        node.removeFromParentNode()
    }

Works for me.

为我工作。

#3


5  

For me worked like below:

我的工作如下:

sceneView.scene.rootNode.enumerateChildNodes { (node, stop) in
node.removeFromParentNode() }

#4


1  

Where you need to remove all of your nodes, call this (if your scene isn't self, change it):

在需要删除所有节点的地方,调用这个(如果您的场景不是self,请更改它):

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)

另外,如果需要删除除某些节点之外的每个节点,可以调用这个(例如,我们不想删除3个节点,它们被命名为a、b和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


-2  

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

在XCode版本7.2中,这对我很有效。

#1


3  

you can either create a new scene or call -[SCNNode removeFromParentNode] on every child node of the scene's rootNode

您可以在场景的rootNode的每个子节点上创建一个新场景或调用-[SCNNode removeFromParentNode]

#2


17  

Try this (assuming you are using Swift):

试试这个(假设你正在使用Swift):

rootNode.enumerateChildNodes { (node, stop) in
        node.removeFromParentNode()
    }

Works for me.

为我工作。

#3


5  

For me worked like below:

我的工作如下:

sceneView.scene.rootNode.enumerateChildNodes { (node, stop) in
node.removeFromParentNode() }

#4


1  

Where you need to remove all of your nodes, call this (if your scene isn't self, change it):

在需要删除所有节点的地方,调用这个(如果您的场景不是self,请更改它):

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)

另外,如果需要删除除某些节点之外的每个节点,可以调用这个(例如,我们不想删除3个节点,它们被命名为a、b和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


-2  

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

在XCode版本7.2中,这对我很有效。