Search Unity

Discussion Found out a fact that boosted my fps by more than twice!

Discussion in 'Prefabs' started by Ts1nd, Mar 10, 2023.

  1. Ts1nd

    Ts1nd

    Joined:
    Jan 24, 2021
    Posts:
    108
    So I have been stacking all character prefabs/bodies that are in my game in one prefab and keep them disabled. When initializing given character, I would enable only the prefab that is associated with it while still keeping the rest disabled. I have hundreds of characters/bodies in my game meaning every character in scene has hundreds of disabled gameobjects (because only 1 is used at a time). I thought it would absolutely not affect the performance since they are disabled and would only increase memory usage by tiny bit... but was very wrong. Restructuring my code so that each "body" instantiates into the character when needed and keeping only 1 at a time without any disabled objects, boosted my fps by more than twice!
    How is that possible? Why disabled gameobject affects the performance? Do you have some similar tips so I and others who read it can benefit?
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,445
    Interesting findings.

    This is a common approach done by a lot of character packs, like @syntystudios figures. I never measured the performance impact, but I did make a quick MonoBehavior called "CleanupOnStart" which destroys disabled objects with MeshRenderers. If you plan to pool NPCs and flip between different character bodies on the same object, it takes a bit more work to avoid having so many disabled complex objects.

    Disabled objects are still updated for their transforms. I am surprised at your results because like you I would think the SkinnedMeshRenderers would be inert. Can you try your timing tests with three columns:
    • enabled 1 character with 1 SkinnedMeshRenderer
    • enabled 1 character with 1 enabled and 10 disabled children, each with 1 enabled SkinnedMeshRenderer
    • enabled 1 character with 1 enabled and 10 disabled children, each with 1 disabled SkinnedMeshRenderer
    Run it through the profiler, and do a good FPS test in a build without WaitForSync for each.