I want to create a star field and I thought I would use Shuriken.
I can type “Infinity” in duration and it gets replaced by 1000000. Not really infinite, but ok. The problem however is that now all the “over Lifetime” effects go from 0 to 1000000 seconds and are in turn impossible to use.
Do I have to do this through scripting? Is there no way through the editor to have a particle with infinite or close to infinite life, but still have it cycle through effects in a set interval. Like every second it should change color. Like a separate cycle duration or something which all the “over Lifetime” can use.
For anyone else wondering about the same I did not find a way of doing this through the editor GUI, but this seems to work:
void Update () {
particleCount = particleSystem.GetParticles(particles);
for (int i = 0; i < particles.Length; i++) {
if (particles[i].lifetime <= 0.1f) {
particles[i].lifetime = particles[i].startLifetime;
}
}
particleSystem.SetParticles(particles, particleCount);
}
That is, when a particle is almost dead I just set its life back to its starting life so it runs its cycle endlessly. This way my particles never die, but still cycle through all the effects like color over lifetime, size over lifetime etc. as per usual. The reason I check for 0.1 and not 0 is because my particles never reached 0. They got to an arbitrary value between 0 and 0.1 and then disappeared. Not sure why.
Using something like a random force over lifetime with this creates some very dynamic particles which almost look like ants.
I think this option should be a part of the standard Shuriken GUI as well. It enables a host of different effects not possible without scripting today. Like a permanent starfield for instance, which is the effect I’m creating.
Anyone interested in updated system version of this (works on Unity 5.3), here is the full code:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(ParticleSystem))]
public class InfiniteParticleLife : MonoBehaviour {
ParticleSystem m_System;
ParticleSystem.Particle[] m_Particles;
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 < m_Particles.Length; i++) {
if (m_Particles[i].lifetime <= 0.1f) {
m_Particles[i].lifetime = m_Particles[i].startLifetime;
}
}
// Apply the particle changes to the particle system
m_System.SetParticles(m_Particles, numParticlesAlive);
}
void InitializeIfNeeded()
{
// Grab the Particle System component of the object if not already done so
if (m_System == null)
m_System = GetComponent<ParticleSystem>();
// Update the m_Particles with currently existing particles from the system
// Use 'Max Particles' in Particle System to control particle count in scene! - Otherwise endless number of particles
if (m_Particles == null || m_Particles.Length < m_System.maxParticles)
m_Particles = new ParticleSystem.Particle[m_System.maxParticles];
}
}