获取对场景编辑器中创建的粒子系统的引用
Get reference to a Particle System created in Scene Editor
我在场景编辑器(不是粒子编辑器)中创建了一个粒子系统,它被命名为"particles"(这是默认名称)。
回到 ViewController,我正在尝试获取对该粒子系统的引用并更改它的一些属性。
但我不明白为什么这不起作用:
let particleSystem = SCNParticleSystem(named: "particles", inDirectory: "")
particleSystem?.isAffectedByGravity = true
我知道可以在场景编辑器中设置重力,但我只是将其用作测试以查看对粒子系统的引用是否正常工作。不是。
我错过了什么或做错了什么?
额外的努力:
按照 Rickster 的建议,试试这个:
let particleSystems = scene.particleSystems
let myParticleSystem = particleSystems?[0]
myParticleSystem?.isAffectedByGravity = true
print(particleSystems)
现在有这个问题:
我的想法(尽管是错误的)是数组的 0 位置将拥有我在这个场景中唯一的粒子系统。
正如 docs for that initializer 所建议的那样,它用于加载它们自己文件中的粒子系统。当您在场景编辑器中制作粒子系统时,粒子系统不是它自己的文件——它是您正在制作的场景文件的一部分。
要找到它...好吧,首先您必须加载该文件代表的 SCNScene
对象。假设您已经这样做了,您将需要两种方法之一来访问粒子系统,具体取决于您在编辑器中的定义方式:
如果粒子系统附加到节点 - 将其放置在场景中的直接方法 - 您可以使用场景的 rootNode
property to get at the scene contents, then use methods like this one to find the node containing the particle system using the name you assigned it in the editor. (You did give it a name in the editor, right? If not, go do that... or worst case, there's always this method 允许您根据任何测试搜索节点你可以编码。)
如果没有,请使用场景的 particleSystems
属性 查找所有未附加到节点的系统。
这是我得到的现场图:
以及获取对分配给粒子 SCNNode 实例的粒子系统的引用的代码:
在 Swift 2.3(及更早版本)中
let particlesNode:SCNNode = scene.rootNode.childNodeWithName("particles", recursively: true)!
let particleSystem:SCNParticleSystem = (particlesNode.particleSystems?.first)!
在Swift3.0
let particlesNode:SCNNode = scene.rootNode.childNode(withName: "particles", recursively: true)!
let particleSystem:SCNParticleSystem = (particlesNode.particleSystems?.first)!
替代数组索引语法
let particleSystem:SCNParticleSystem = (particlesNode.particleSystems?[0])!
我在场景编辑器(不是粒子编辑器)中创建了一个粒子系统,它被命名为"particles"(这是默认名称)。
回到 ViewController,我正在尝试获取对该粒子系统的引用并更改它的一些属性。
但我不明白为什么这不起作用:
let particleSystem = SCNParticleSystem(named: "particles", inDirectory: "")
particleSystem?.isAffectedByGravity = true
我知道可以在场景编辑器中设置重力,但我只是将其用作测试以查看对粒子系统的引用是否正常工作。不是。
我错过了什么或做错了什么?
额外的努力:
按照 Rickster 的建议,试试这个:
let particleSystems = scene.particleSystems
let myParticleSystem = particleSystems?[0]
myParticleSystem?.isAffectedByGravity = true
print(particleSystems)
现在有这个问题:
我的想法(尽管是错误的)是数组的 0 位置将拥有我在这个场景中唯一的粒子系统。
正如 docs for that initializer 所建议的那样,它用于加载它们自己文件中的粒子系统。当您在场景编辑器中制作粒子系统时,粒子系统不是它自己的文件——它是您正在制作的场景文件的一部分。
要找到它...好吧,首先您必须加载该文件代表的 SCNScene
对象。假设您已经这样做了,您将需要两种方法之一来访问粒子系统,具体取决于您在编辑器中的定义方式:
如果粒子系统附加到节点 - 将其放置在场景中的直接方法 - 您可以使用场景的
rootNode
property to get at the scene contents, then use methods like this one to find the node containing the particle system using the name you assigned it in the editor. (You did give it a name in the editor, right? If not, go do that... or worst case, there's always this method 允许您根据任何测试搜索节点你可以编码。)如果没有,请使用场景的
particleSystems
属性 查找所有未附加到节点的系统。
这是我得到的现场图:
以及获取对分配给粒子 SCNNode 实例的粒子系统的引用的代码:
在 Swift 2.3(及更早版本)中
let particlesNode:SCNNode = scene.rootNode.childNodeWithName("particles", recursively: true)!
let particleSystem:SCNParticleSystem = (particlesNode.particleSystems?.first)!
在Swift3.0
let particlesNode:SCNNode = scene.rootNode.childNode(withName: "particles", recursively: true)!
let particleSystem:SCNParticleSystem = (particlesNode.particleSystems?.first)!
替代数组索引语法
let particleSystem:SCNParticleSystem = (particlesNode.particleSystems?[0])!