Search Unity

Quickly find all particles on the scene without using GetComponent

Discussion in 'Scripting' started by Lesnikus, Mar 20, 2018.

  1. Lesnikus

    Lesnikus

    Joined:
    Aug 29, 2015
    Posts:
    47
    I need to quickly find all particles on the scene without using GetComponent. Perhaps in the engine there is some pool of all existing particles at the moment to have direct access to them? Like Scene.GetRootGameObjects () to find all root objects, but for particles or objects with particle systems.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    why?
     
  3. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,299
    There is no global list of particles that you can access.
    If you want all of them then Object.FindObjectsOfType will do the job. If performance is an issue then manage your own list with a script.
     
  4. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,796
    I'm confused, if you move the parent transform, the particle systems will also move, so what the issue?

    To get around having to call a ton of GetComponent, have a list and add and remove to it as you create new particle systems, so that way you'll have like one GetComponent every once in a while, which should be fine.
     
    karl_jones likes this.
  5. Lesnikus

    Lesnikus

    Joined:
    Aug 29, 2015
    Posts:
    47
    I move all the objects on the scene in one frame. This is necessary to make an endless world and overcome the inaccuracy of the float. There are two lists in the script: ordinary root objects and objects with a particle system. Now I search in each script for any events of creating objects (Instantiate, new GameObject, CreatePrimitive), deleting objects (Destroy) and manipulating the hierarchy (gameObject.parent) and accordingly add objects to the list or delete from it. Then, when the player moves too far from the zero coordinates, I move the whole world to zero. I plan to simplify this system so that I do not have to climb into every script. I will search all root objects with help Scene.GetRootGameObjects () and move them in one frame. This should not affect performance much, as the search only occurs on root objects. But in many child objects will be particle systems, which I also need to move. I can not use the GetComponent on the whole scene, because it will hit the performance. What is the way to move all particles on the scene, without using heavy operations, like GetComponent?

    I need to search for all the objects and move them all, including all particles, in one frame, so the Object.FindObjectsOfType does not fit. It's a pity, apparently i'll have to come up with another solution
     
  6. Lesnikus

    Lesnikus

    Joined:
    Aug 29, 2015
    Posts:
    47
    If the particles are in the world coordinate system, their movement does not depend on the movement of the parent (like fire or sparks). Therefore, you need to find each particle separately and move it
     
  7. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,299
    Do as AcidArrow says and manage your own list. Also be aware that world space particles won't work as expected with this technique however you could move them manually if necessary or use a custom space, which could be your world pivot etc.
     
  8. Lesnikus

    Lesnikus

    Joined:
    Aug 29, 2015
    Posts:
    47
    What kind of custom space are you talking about? Is this what I described?
     
  9. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,299
    richardkettlewell likes this.