Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Scripting SetParticle() and DeleteParticle() functions

Discussion in '5.3 Beta' started by chanfort, Oct 22, 2015.

  1. chanfort

    chanfort

    Joined:
    Dec 15, 2013
    Posts:
    641
    I noticed that there is "Particles: System Upgrade" set on the roadmap to arrive with Unity 5.3. So I was wondering if there is implemented a function, which would allow to set (add) a single particle onto the ParticleSystem? At the moment there is possible to use only ParticleSystem.SetParticles(), which sets all particles for the entire system. However, it's very performance heavy to run particle systems where number of particles frequently changing in time. So I was curious if there are coming some updates for this in v5.3 ?
     
  2. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    I think you should be already able to do this using ParticleSystem.Emit?
    5.3 will come with a more flexible version of this function, but there is already a basic version available in the current versions of Unity.
     
  3. chanfort

    chanfort

    Joined:
    Dec 15, 2013
    Posts:
    641
    Looks interesting. What about deletion? Is there a way to unset individual particle before its lifetime ends?
     
  4. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    There isn't currently any way to delete a single particle, because the particles do not have a fixed memory location in their arrays, so you can never guarantee that the particle you are interested in is at a particular index.
     
    IgorAherne likes this.
  5. chanfort

    chanfort

    Joined:
    Dec 15, 2013
    Posts:
    641
    Ok, what about possibility just to hide unwanted particle?
     
  6. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    It depends how you would identify it as unwanted? I would presume you would need to call GetParticles and assess each particle to determine one as being unwanted (eg position goes out of bounds etc). At which point you can kill the particle before calling SetParticles.

    Have you got an example use case for wanting to hide/kill a particle that you can identify without inspecting them all using GetParticles?
     
  7. chanfort

    chanfort

    Joined:
    Dec 15, 2013
    Posts:
    641
    I don't need to call GetParticles, as I have usually
    Code (CSharp):
    1. ParticleSystem.Particle[] m_Particles;
    array being modified separately from particle system. I do some small modifications to this array on every update call, but only a few particles gets these modifications. Lets say I predict that my particle will move for the next 1 second with velocity v. However, after few updates I detect that the point position and velocity does not match the prediction. Then I need to update it's trajectory to a new one. In one update I am usually getting 1 - 2 particles out of several thousands which mismatch prediction and it leads that I need to call SetParticles in this case. Now Emit() would solve the problem, as I could emit these 1-2 changed particles with their different trajectories. However, I also need to delete (hide) old particle, which still moves along old trajectory.

    And it's not just trajectories - I have multiple particle systems running with different textures - sometimes I need to move particle from one system to another and a way to do that is to delete particle from old system and to emit it as a new particle onto the new system.

    In fact I am looking forward how to remake SpriteManager rendering part to make sprites being rendered through Unity particle system, as SpriteManager can handle only 1000 - 2000 sprites, while particle system seems to be able to work with around 10000 particles being rendered.
     
  8. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    If you are maintaining your own particle array, it will be important to call SetParticles with the whole array every frame.
    This is because internally, the unity particle system may reorder its own array, so if you decide to delete particle #3, unity may have moved that particle to a different slot internally, so your particle at slot #3 might be in a different location internally.

    This is because, when a particle dies, Unity just moves the particle at the end of the list into the slot of the dead particle, to efficiently keep the data contiguous.

    It sounds like you are already solving your problem in the best way I can think of.

    All the best,
    Richard
     
    IgorAherne likes this.