Version: 2022.2
public int GetParticles (out Particle[] particles);
public int GetParticles (out Particle[] particles, int size);
public int GetParticles (out Particle[] particles, int size, int offset);
public int GetParticles (out NativeArray<Particle> particles);
public int GetParticles (out NativeArray<Particle> particles, int size);
public int GetParticles (out NativeArray<Particle> particles, int size, int offset);

参数

particles 输出粒子缓冲区,其中包含当前粒子状态。
size 从粒子系统中读取的元素数。
offset 活动粒子列表中的偏移(从其复制粒子)。

返回

int 写入输入粒子数组的粒子数(当前存活的粒子数)。

描述

获取该粒子系统的粒子。

只要预先分配一次输入“粒子”数组(参见以下示例),此方法不进行任何分配。该方法在调用时,只获取粒子系统中当前处于活动状态的粒子,因此它可能只获取粒子数组的一小部分。

另请参阅:ParticleSetParticles

using UnityEngine;

[RequireComponent(typeof(ParticleSystem))] public class ParticleFlow : MonoBehaviour { ParticleSystem m_System; ParticleSystem.Particle[] m_Particles; public float m_Drift = 0.01f;

private void LateUpdate() { InitializeIfNeeded();

// GetParticles is allocation free because we reuse the m_Particles buffer between updates int numParticlesAlive = m_System.GetParticles(m_Particles);

// Change only the particles that are alive for (int i = 0; i < numParticlesAlive; i++) { m_Particles[i].velocity += Vector3.up * m_Drift; }

// Apply the particle changes to the Particle System m_System.SetParticles(m_Particles, numParticlesAlive); }

void InitializeIfNeeded() { if (m_System == null) m_System = GetComponent<ParticleSystem>();

if (m_Particles == null || m_Particles.Length < m_System.main.maxParticles) m_Particles = new ParticleSystem.Particle[m_System.main.maxParticles]; } }