Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by gromiczek · Jun 11, 2021 at 01:38 PM · performanceparticlesparticlesystemplayemit

Manually and efficiently placing thousands of particles

I have a Shuriken system based on this answer to another question about manually producing particles. However, it's very costly to run Emit() and Play() after placing each particle. I've written an alternate method that assigns the various positions to the particle themselves (rather than singly via emitparams) and then calls Emit() and Play(). It does perform much faster, but all the particles end up at 0,0,0 instead of their respective positions. I've logged out their positions to make sure they are correct and they are, but I'm not sure why this method is not working. How do I set the positions of individual particles correctly in this more efficient method?

I am rendering many of these pools and around 5-6k particles at a time, so I need a more performant method than one at a time via emitparams.

Below in the code you'll see: - The old, inefficient but working method in the script below is CreateParticle() - The new, more performant method that doesn't yet render the positions correctly is CreateParticles()

 using UnityEngine;

 [RequireComponent(typeof(ParticleSystem))]
 public class ParticlePool : MonoBehaviour
 {

     int _lastParticleIndex = 0;  // keeps track of our oldest particle (for deletion)
 
      ParticleSystem _particleSystem;             // our object's particle system
      ParticleSystem.Particle[] _particles;    // our reusable array of particles
      ParticleSystem.EmitParams _emitParams;   // reusable emitparams
      int _maxParticles = 0;       // total number of particles in our scene before re-using
      private float _maxValue;
      private bool _primed;


      private void Awake()
      {
          if (_primed == false)
          {
              Initialize(); // initialize all of our member variables
              _primed = true;
          }
      }

      public void ResetParticles() // used to erase all previous particles written to the system
      {
          for (int i = 0; i < _particles.Length; i++)
          {
              _particles[i].remainingLifetime = -1;
              _particles[i].startLifetime = 1;
          }
          _lastParticleIndex = 0;    // keep track of oldest particle - now, of course, reset to the beginning

          // have to re-write
              _particleSystem.SetParticles(_particles, _particles.Length);  // write those pesky particles back into our ParticleSystem
  }

      public void CreateParticles(Vector3[] particlePositions, float[] particleTransparencies, Color32 color)
      {
          for (int i = 0; i < particlePositions.Length; i++)
          {
              if (i > _maxParticles)
                  break;
          
              color.a = (byte) (particleTransparencies[i] * 255);
          
              var particle = _particles[i];
              particle.position = particlePositions[i];
              particle.startLifetime = _maxValue;
              particle.startColor = color;
              _particles[i] = particle;
          }
          _particleSystem.SetParticles(_particles, _particles.Length);
          _particleSystem.Emit(_particles.Length);
          _particleSystem.Play();
      }
  
      public void CreateParticle(Vector3 position, Color32 color)
      {
          // if we're at our particle count limit, kill our oldest particle.
      
          if (_lastParticleIndex >= _maxParticles)
          {
              // set lifetime to -1 to kill the particle
              _particles[_lastParticleIndex].remainingLifetime = -1;
              // we need to reset start lifetime to a normal value, too or the particle will still have infinite lifetime
              _particles[_lastParticleIndex].startLifetime = 1;
 
              _lastParticleIndex++;    // keep track of oldest particle
              if (_lastParticleIndex >= _maxParticles) _lastParticleIndex = 0;
 
              // have to re-write
              _particleSystem.SetParticles(_particles, _particles.Length);  // write those pesky particles back into our ParticleSystem
          }

          // set up params for this particle, you can use whatever you want (see unity docs for EmitParams for what's available)
          _emitParams.position = position;
          _emitParams.startColor = color;
          _emitParams.startLifetime = _maxValue; // float.MaxValue makes the particle's lifetime infinite

          _particleSystem.Emit(_emitParams, 1);
          _particleSystem.Play();
      }

      void Initialize()
      {
          if (_particleSystem == null)
          {
              _particleSystem = GetComponent<ParticleSystem>();
              _particleSystem.transform.parent = gameObject.transform;
           }
      
          _maxParticles = _particleSystem.main.maxParticles;
          _maxValue = float.MaxValue;
 
          if (_particles == null || _particles.Length < _particleSystem.main.maxParticles)
              _particles = new ParticleSystem.Particle[_particleSystem.main.maxParticles];
      }
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

139 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Particle System won't play on awake 0 Answers

Mesh particle system per-vertex overhead in ParticleSystem.GeometryJob 0 Answers

Emit Random Particles? 2 Answers

performance question about particles and instantiating 1 Answer

ParticleSystem.Play() not working 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges