Search Unity

Efficiency - Activating/Deactivating MANY game objects at different times

Discussion in 'Scripting' started by Jakeiiii, May 24, 2019.

  1. Jakeiiii

    Jakeiiii

    Joined:
    Jul 7, 2014
    Posts:
    256
    Ok so I'm working on a somewhat-large open world game with various enemies and objects. I've created a chunk system that is actually rather efficient; the world is basically divided up into a 3D grid and a single object checks the characters position relative to each chunk. If in range the objects within that grid will be activated, and when out of range it deactivates. The entire world is also made up of additive scenes (around 9 in total, so it's like a 3x3 grid, it's not a massive game) and so each scene has its own chunk system. For each system, it checks each chunk at a different time (basically every [time between each distance check] / [number of chunks]) so no two chunks in each scene are checked at the same time.

    So this way I've made it so that as few objects are getting activated/deactivated at one time via the chunk system. However each object has it's own activation system based on distance to the character to work as a LODGroup in a way. Each object gets switched between a Full version (with high poly mesh, components, etc.) and a Low version (just a low poly mesh) so that at a distance the objects don't take up any processing power other than to just have a low poly mesh stay there and no other components running; hence why I went with this idea rather than LODGroups which still have the components functioning. I did think that this additional activating/deactivating would have been overkill but it has proven to improve performance over using LODGroups.

    I have tried pooling the objects but having so many objects active at once, somewhere in the game, unfortunately made things worse.

    My question is how can I optimise this further? I'm working on a non-desktop platform that is relatively low/mid-range and I'm having trouble maintaining a consistent 60fps. It's very close to it but at points it does dip below and causes missing input (I'm using Rewired for input and have tried to use different update loops to solve this but unfortunately that hasn't helped). If anyone has any ideas on any flaws in this set up, any suggestions in improving it further or guidance as to how to fix the input issue regardless of framerate then that would be especially helpful.

    Graphically it's not very demanding as it doesn't use any realtime lighting or high polygon-counts or intense visual effects and it's clear that this activating/deactivating is what's causing the spikes in performance. Also I'm not sure if it would be best to leave Static objects out of this (like trees) or is that not related to activating/deactivating objects?

    Any help is appreciated!
     
  2. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    112
  3. Jakeiiii

    Jakeiiii

    Joined:
    Jul 7, 2014
    Posts:
    256
    Thanks for the reply! Does this just deal with animations/renderers etc.? I need my objects to have basically all of their components disabled at a distance (like colliders, renderers and monobehaviors) and not just their visual components, hence why I'm having to deactivate the objects (low/full versions) entirely.

    EDIT: Trying a different idea in the meantime. Instead of the objects activating/deactivating their full/low versions, they will simply enable/disable all of the components within those children. As for the chunks, they will either disable the script that handles the full/low versions or activate/deactivate the object if they don't use that script (some just use LODGroups).
     
    Last edited: May 24, 2019
  4. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    112
    You get an array of objects based on the culling, it doesn't disable anything on its own. With the array of objects, you can do anything you want.