Search Unity

Optimizing code to handle lots of rts style units at once!

Discussion in 'Scripting' started by Sobrtonog, Nov 16, 2019.

  1. Sobrtonog

    Sobrtonog

    Joined:
    Sep 28, 2019
    Posts:
    15
    Hello, I'm doodling with Unity atm and working on a personal rts/moba style project. Currently I have gotten some basic functionalities to work - like being able to move/attack units, hostile units having AI and auto aggroing, etc.

    I wanted to see how many units my game can handle, and I was kind of shocked that even with almost very little code - my game was dropping to 20~40 frames with roughly 400 units in the game. Some of them not even pathing, actually about 60~80% of them being idle and only the remainder being active.

    I was seeking advice on how I can improve the performance of my game - maybe some more experienced individuals can share some knowledge and tips regarding this aspect. Currently all my textures are CPU instances, I've tried changing all of them to GPU instance, but I'm not quite sure if that did anything - I did not see any improvements, so I changed them back for the time being.

    As for my AI code, it is a class with roughly about 200~300 lines that is tacked onto gameObjects. It uses the NavMeshAgent component offered by Unity to handle pathing. One of the things I've made sure to implement, is a timer like condition in the Update() so that the AI functions aren't running every game tick. It's at 0.125f seconds.

    One question that comes to mind - is it better to have a one singularity instance member looping through all the unit as nodes in a list, or is it better to have each unit with it's own class instance attached to the gameObject? Currently I have each class attached to the gameObject, but I can definitely rewrite the system to put them in a list. That brings a more general question about low timed loop functions, if there's a lot of stuff to do in low periodic timer - is it better to store them all in a list or have them as separate class instances?

    If you have any tips and tricks - please let me know! I would also love snippets :).

    (P.S. - I have no debug messages turned on, so that probably isn't it either. My CPU is kind of mediocre, but it can handle several hundred units in other RTS games with minimal frame drop).
     
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    Have you looked at the profiler to see what is taking up the most time?
     
  3. Sobrtonog

    Sobrtonog

    Joined:
    Sep 28, 2019
    Posts:
    15
    This is what it looks like, I've opened up the profiler up a couple times but I'm still not sure what everything is and how to make the best of use of it to be honest.

    According to the chart about less than half of it is animation, and the other half is scripts/rendering.
     

    Attached Files:

    • aa.png
      aa.png
      File size:
      165.8 KB
      Views:
      438
  4. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    Look at the hierarchical view (as opposed to Timeline view), it is more useful for this type of thing.