Search Unity

Question Improving efficiency by getting Get/SetParticles working

Discussion in 'General Graphics' started by gromiczek, Jun 24, 2022.

  1. gromiczek

    gromiczek

    Joined:
    Jul 19, 2019
    Posts:
    25
    Hi - have a particle system that draws pictures with still particles, and I refresh these pictures periodically. However, I've only ever been able to make them from my position arrays using EmitParams and Emit and calling this for one particle at a time. However, this is now proving too inefficient with 1000s of particles. I've often tried to get Get/SetParticles so that I can create many at a time, but have never been able to get it to work. I'd appreciate a nudge in the right direction so i can finally bring up the project's efficiency. I did note this thread that demonstrates it's possible, but am not sure how to fix what I have below.

    Currently the script below produces particles but all at 0,0,0, instead of their correct positions. I have sanity checked the positions array and particle positions - they are correct, but the particles are not rendered at those positions.



    Code (CSharp):
    1. public void CreateParticles(Vector3[] particlePositions, float[] particleTransparencies, Color32 color)
    2.    {
    3.  
    4.        for (int i = 0; i < particlePositions.Length; i++)
    5.        {
    6.            if (i > _maxParticles)
    7.                break;
    8.      
    9.            color.a = (byte) (particleTransparencies * 255);
    10.  
    11.            var particle = _particles;
    12.            particle.position = particlePositions;
    13.            particle.startLifetime = _maxValue;
    14.            particle.startColor = color;
    15.            _particles = particle;
    16.      
    17.        }
    18.  
    19.        if (_particles.Length > particlePositions.Length)
    20.        {
    21.            for (var j = 0; j + particlePositions.Length < _particles.Length; j++)
    22.            {
    23.                _particles[j + particlePositions.Length].remainingLifetime = -1;
    24.                _particles[j + particlePositions.Length].startLifetime = 1;
    25.            }
    26.        }
    27.        _particleSystem.SetParticles(_particles);
    28.        _particleSystem.Emit(_particles.Length);
    29.    }
     
  2. joshuacwilde

    joshuacwilde

    Joined:
    Feb 4, 2018
    Posts:
    731
    Why not just use instanced drawing if you are having such a headache from particles? Doesn't sound like you really need the particle effects anyway?
     
  3. gromiczek

    gromiczek

    Joined:
    Jul 19, 2019
    Posts:
    25
    Thanks @joshuacwilde - perhaps that is a good idea. The particles are a range of little hand drawn hatch marks that I scanned - it gives the image a hand-drawn feel. Is something like this possible with instanced drawing? I'll explore instancing further...