Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Optimising UI particle system

Discussion in 'Editor & General Support' started by TiggyFairy, May 21, 2023.

  1. TiggyFairy

    TiggyFairy

    Joined:
    Dec 22, 2019
    Posts:
    413
    So I'm building a basic UI particle system for my game. It honestly doesn't have to do much, but I've gotten it to the point I'd like to optimise it a little. I think if I can get (say) 9000 particles moving smoothly onscreen at once I can be fairly sure that any computer can handle a couple of hundred. I've found removing even tiny amounts of code from the particle control system makes things faster when there's this many. So, could anyone make suggestions, and tell me if any of these things would hinder me:
    • Each particle is actually a gameobject with a cloned RawImage. There are no scripts attached. It's all done via a core manager. I feel like there must be a way to instance them, rather than have them all be unique objects. I'm planning to use emoji size images, say 16x16 or 32x32 as I'm sure image size probably matters. For sparkles, it'll be smaller.
    • Each image is sent to the start location, then tweened to the end location. This makes it smooth, gives me easy curved flight, and gives me extra features, but I'm not sure how much power it's using. Probably a lot more than than regular transform.
    • Each particle is spawned by a centralised coroutine that repeats through the particles once per frame. I could actually make it once per two frames or so. Or I could have the particles spawned in batches using different coroutines maybe? I feel like multi-threading might help solve some of this.
    Also, if anyone has any tips on making my particles nova out from a particular point and stop in a circle I would really love that. My current solution to that (adding random numbers to the end Vector) is not great. Here's my current - very dodgy - maths for the spray.

    Code (CSharp):
    1.  
    2. float spray = Mathf.PI * 2 / pc.particles;
    3. Vector2 targ = new Vector2(Mathf.Sin(spray * i) * (pc.maxSpread * pk.particle[i].rct.sizeDelta.x), Mathf.Cos(spray * i) * (pc.maxSpread * pk.particle[i].rct.sizeDelta.y));
    4. pk.particle[i].rct.DOJump(new Vector2(targ.x + UnityEngine.Random.Range(-pc.noise, pc.noise), targ.y + UnityEngine.Random.Range(-pc.noise, pc.noise)), 100f, pc.jumps, Options.uiSpeed / speed).SetEase(Ease.OutQuad);
    5.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Any reason you're not using the Unity particle system? It's pretty powerful!
     
  3. TiggyFairy

    TiggyFairy

    Joined:
    Dec 22, 2019
    Posts:
    413
    I thought that was only for 3D stuff?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    No, not at all. Besides, ALL stuff in Unity is 3D, even the 2D! Yes, it's true, but I digress.

    The main concern with mixing stuff like a ParticleSystem and UI is simply the layering and draw ordering.

    Here's some scribblings about layering in Unity:

    Three (3) primary ways that Unity draws / stacks / sorts / layers / overlays stuff:

    https://forum.unity.com/threads/orthographic-camera-rendering-order.1114078/#post-7167037

    In short, as far as the Standard Rendering Pipeline,

    1. The default 3D Renderers draw stuff according to Z depth - distance from camera.

    2. SpriteRenderers draw according to their Sorting Layer and Sorting Depth properties

    3. UI Canvas Renderers draw in linear transform sequence, like a stack of papers

    If you find that you need to mix and match items using these different ways of rendering, and have them appear in ways they are not initially designed for, you need to:

    - identify what you are using
    - search online for the combination of things you are doing and how to to achieve what you want.

    There may be more than one solution to try.

    For instance, you may even use multiple co-located cameras to more-explicitly control apparent draw ordering.

    Additional reading in the official docs:

    https://docs.unity3d.com/Manual/2DSorting.html

    And SortingGroups can also be extremely helpful in certain circumstances:

    https://docs.unity3d.com/Manual/class-SortingGroup.html

    Other rendering pipelines may have other ways of layering (HDRP and URP). Go see the latest docs for details.

    EDIT: there's also tons of Youtube tutorials... just search unity particles in ui to see
     
    Last edited: May 21, 2023
    DevDunk and TiggyFairy like this.
  5. TiggyFairy

    TiggyFairy

    Joined:
    Dec 22, 2019
    Posts:
    413
    Well, I wish I'd known this earlier lol Thank you

    I'm hearing a dedicated UI camera might help with this? Though I'm in HDRP, so I don't know if that's going to work tbh.
     
    Last edited: May 21, 2023
  6. TiggyFairy

    TiggyFairy

    Joined:
    Dec 22, 2019
    Posts:
    413
    I really can't work out how to make this work with the UI. Nothing I click in the particle system seems to affect the game, but I can see the particle system working in the scene window. Any ideas what I could do? I can't find anything working online.

    upload_2023-5-22_15-44-51.png