返回介绍

ParticleSystem.GetParticles 获取粒子

发布于 2019-12-18 15:38:10 字数 4067 浏览 1016 评论 0 收藏 0

JavaScript => public function GetParticles(particles: Particle[]): int;
C# => public int GetParticles(Particle[] particles);

Parameters 参数

particlesParticle buffer that is used for writing particle state to. The return value is the number of particles written to this array.
用来写入粒子状态的粒子缓存器,返回值是已写入该数组的粒子数量。

Returns 返回值

int The number of particles written to the input particle array (the number of particles currently alive).

返回粒子数组的粒子数量(当前活着的粒子数量)。

Description 描述

Get the particles of this particle system.

获取粒子系统的粒子。

This method is allocation free as long the input “particles” array is preallocated once (see example below). Note that only a small part of the particles array might be used as this depends on how many particles are currently alive in the particle system when calling GetParticles().

这个方法是获取输入参数particles数组一次性预先分配的长度。注意,当调用GetParticles()时,数组可能只是一小部分,取决于当前活着的粒子。

See Also: Particle, SetParticles.

JavaScript:

#pragma strict
 
@script RequireComponent(ParticleSystem)
 
var m_System : ParticleSystem;
var m_Particles : ParticleSystem.Particle[];
public var m_Drift = 0.01f;
 
function LateUpdate() {
 
	InitializeIfNeeded();
 
	// GetParticles is allocation free because we reuse the m_Particles buffer between updates
	var numParticlesAlive = m_System.GetParticles(m_Particles);
 
	// Change only the particles that are alive
	for (var 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);
}
 
function InitializeIfNeeded() {
	if (m_System == null)
		m_System = this.GetComponent.<ParticleSystem>();
 
	if (m_Particles == null || m_Particles.Length < m_System.maxParticles)
		m_Particles = new ParticleSystem.Particle[m_System.maxParticles];
}

C#:

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.maxParticles)
			m_Particles = new ParticleSystem.Particle[m_System.maxParticles]; 
	}
}

ParticleSystem

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文