Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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
1
Question by Kenzel · Oct 03, 2013 at 08:08 PM · c#particlesparticlesystemshuriken

(Shuriken) Accessing Particle[] Array Created in Inspector?

Hi,

I'm trying to grab the Particle[] array attached to a Particle System with the variables set in Inspector. Apparently, ParticleSystem.GetParticles() doesn't work because the argument for that function requires a Particle[], which is what I'm trying to get. The examples I've found all create a new Particle[] array within the script, rather than getting the list of particles from the existing ParticleSystem settings.

I've tried:

 Particle[] particles = particleSystem.GetParticles();

But the input argument of GetParticles is a Particle[]. Is there a way to get an array of particles emitted by a ParticleSystem that was created in Inspector, instead of in a script?

Thanks for the help!

Comment
Add comment · Show 2
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
avatar image Jamora · Oct 03, 2013 at 08:12 PM 0
Share

Is there a question here? You need to make the Particle[] yourself, then let the GetParticles() fill it for you.

At least I think that's what you want to know?

avatar image Kenzel · Oct 03, 2013 at 08:23 PM 0
Share

Well, I've created a ParticleSystem GameObject in Unity, but I want to get the list of particles created by this ParticleSystem as an array to use in C#. How can I achieve this?

GetParticles() doesn't seem to work, but maybe I'm using it wrong. The documentation contains no example.

3 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by Jamora · Oct 03, 2013 at 08:38 PM

You need to first create an array of Particles. The size of that array should be how many particles you want. Then, you pass that array on as the parameter for GetParticles(), which then fill the array for you.

 Particle[] allParticles = new Particle[particleSystem.particleCount];
 particleSystem.GetParticles(allParticles);
 
 foreach(Particle p in allParticles){
     //work your magic
 }
Comment
Add comment · Show 1 · Share
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
avatar image Kenzel · Oct 03, 2013 at 09:42 PM 1
Share

Ahh, thanks! This way of doing things feels really counter-intuitive (at least, for someone co$$anonymous$$g from Java).

avatar image
2

Answer by tarahugger · Jul 31, 2014 at 08:27 AM

Jamora's answer is great; Here's some extensions for working with it in a more familiar way.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 using Particle = UnityEngine.ParticleSystem.Particle;
 
 public static class ParticleSystemExtensions
 {
     /// <summary>
     /// Method that takes a Particle argument
     /// </summary>
     public delegate Particle UpdateParticleDelegate (Particle particle);
 
     /// <summary>
     /// Get particles Array
     /// </summary>
     public static Particle[] GetParticles (this ParticleSystem particleSystem)
     {
         var particles = new Particle[particleSystem.particleCount];
         particleSystem.GetParticles (particles);                
         return particles;
     }
     
     /// <summary>
     /// Set particles Array
     /// </summary>
     public static void SetParticles (this ParticleSystem particleSystem, Particle[] particles)
     {
         particleSystem.SetParticles (particles, particles.Count ());
     }    
 
     /// <summary>
     /// Applies the given function to each particle in a ParticleSystem
     /// </summary>
     /// <param name="func">Function to perform on each particle</param>
     public static void UpdateParticles (this ParticleSystem particleSystem, UpdateParticleDelegate func)
     {
         var particles = new Particle[particleSystem.particleCount];
         var particleCount = particleSystem.GetParticles (particles);        
         int i = 0;
         while (i < particleCount) {
             particles [i] = func.Invoke (particles [i]);
             i++;
         }
         particleSystem.SetParticles (particles, particleCount);
     }    
 }    

This adds an overloads for parameterless constructor and set without particle count. And you can use the update method like this:

 void LateUpdate ()
 {        
     _particleSystem.UpdateParticles (particle => {
         particle.color = Color.red;    
         particle.size = 10;
         return particle;
     });        
 }



Comment
Add comment · Share
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
avatar image
0

Answer by titulus · Jul 31, 2014 at 10:55 AM

I have not tested this, but would try something like this, especially on mobile platforms:

 Particle particles = new Particle[maxNumOfParticles];
 
 Update() // or any update method
 {
     int numOfWritedParticles =  particleSystem.GetParticles(particles);
 
     for(int i = 0; i < numOfWritedParticles; i++)
     {
         Particle par = particles[i];
         par.size = 10f;
     }
 }

By doing this way you don't allocate memory for new particle array in every Update method call

Comment
Add comment · Share
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

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

17 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

Related Questions

What could cause OnParticleCollision to fail? 1 Answer

Spawn particles in grid pattern? 1 Answer

The further the distance, the longer particle lifetime. How? 0 Answers

Particle Burst on Button Press 1 Answer

Particle system draw order with two particle systems 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