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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to get performance in large 2D worlds?

Discussion in '2D' started by Shedletsky, Jun 7, 2022.

  1. Shedletsky

    Shedletsky

    Joined:
    Apr 21, 2014
    Posts:
    20
    I'm learning Unity and I'm tinkering around trying to make a base building game.

    I'd like to have:
    * A large 2D tilemap (300x300ish)
    * 1000s of animated trees

    I know how to code but not how to select the right parts of Unity to make rendering performant.

    I'm stuck on how to render animated trees. For my first attempt I created a 2D sprite and did some simple IK bone animation on it. Then I made it a prefab and created 1000 of them with C# and put them in my scene. It was cool how fast it was to do this, but the performance is not cool.

    I can think of many reasons why this might be slow. I'm pretty sure when I do things this way I am adding 1000 animation controller scripts to my scene. Depending on how animation gets turned into draw calls by Unity, maybe that is slow also. Maybe I need to add some culling. Maybe there is a way to have 1 animation script controlling 1000 trees at once. Maybe I need to use GPU instancing. Dunno.

    upload_2022-6-7_12-49-27.png

    I eventually want to add a ton of animated characters to this map also, so I'm trying to understand a solution that will let me play 2D animations on a bunch of NPCs at once where there is a little bit more variability than currently with these trees.

    I read somewhere that Rimworld implements its own 2D map renderer that works at the level of batching geometry up for the gfx card. Do I need to do this? I was hoping that the new Tilemap stuff would solve most of my problems for me.

    Most of the Unity demos I've seen assume I want to make a platformer game or some other small thing where you don't have to think about scale or performance as much.

    I'd like to work in a world of abstraction where I create a bunch of prefabs and Instantiate them on top of my tilemap, but maybe that is infeasible. So my question is, if I were a Unity expert, what would my rendering strategy here be?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    Everything about optimization in computers is about not actually doing the work that the user thinks you are doing.

    To this end you can:

    - just not do the work (eg, trees out of sight just don't exist)

    - do less work for large parts of it (eg trees out of sight are considered but disabled)

    - fake it in some other way: turn your trees into something simpler (pixels? particles?) when you are sufficiently far from them.

    For something like 1000 animated trees, perhaps find out how many you could ever possibly see and create that many, then have a tree manager that recycles those trees to everywhere they need to be, and for tree locations offscreen it does not have a tree.

    Yeah, wouldn't we all. Unfortunately code runs on real live hardware on a real live system.

    Screen Shot 2022-06-07 at 1.46.53 PM.png

    As seen here in full at time = 16:05:



    Plan and act appropriately.
     
    coatline likes this.
  3. Shedletsky

    Shedletsky

    Joined:
    Apr 21, 2014
    Posts:
    20
    That's good advice, but also sort of orthogonal to my question, which is about setting up an appropriate render pipeline to handle many animations, hopefully in Unity's native format.

    It can be simultaneously true that my game should: 1) avoid doing work and 2) do something better than making everything a GameObject with their own AnimationController.

    So I'm mostly interested in answers to #2.
     
  4. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    883
    Before you start looking for solutions, have a look at the profiler to understand what more exactly eats up most of your frame time. Once you know this, you can start looking into ways to optimise these areas.
     
    Kurt-Dekker likes this.
  5. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    if you really want 1000s objects at the same time you should try ECS

    tho that is hard, so you should do the easy thing that is simply, dont load objects or terrain that are outside of the camera like every game does
     
  6. SeerSucker69

    SeerSucker69

    Joined:
    Mar 6, 2021
    Posts:
    65
    You only need to animate the game objects on your screen.

    I have a map say, 20 by 20 game objects. When you scroll, just update the sprites in these game objects and animate them. Your map could be limitless or procedurally generated without losing any performance.
     
  7. PuppyPolice

    PuppyPolice

    Joined:
    Oct 27, 2017
    Posts:
    116
    Its possible to just use one animator and then have the tree prefabs get/point to the sprite from the tree with the animation controller.
     
  8. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,323
    For simple tree animations, you likely want to use a vertex shader. That should drastically reduce CPU time.
     
    Ted_Wikman likes this.