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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

I need to enable/disable a very large amount of game objects. Which was is fastest?

Discussion in 'Scripting' started by aidinz, May 10, 2017.

  1. aidinz

    aidinz

    Joined:
    Apr 17, 2016
    Posts:
    63
    Hi,

    I have a 3D grid of cubes that can be as 100 ^ 3 (and later on much bigger) and I need to go through them and enable/disable them based on some algorithm but it's very slow as you imagine.

    I tried enable and disable the mesh renderer and box collider but apparently it's even slower than enable/disable the game object.

    I think the final solution is to do it in another thread but it will just gets the lag out of the and will still be very slow.

    Thanks
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Tried doing it in a co routine and foreach loop?
     
  3. aidinz

    aidinz

    Joined:
    Apr 17, 2016
    Posts:
    63
    A
    Coroutine, as stated, just makes the game not seem freeze but the time it requires to process those cubes will remain the same.

    And how foreach helps? It generates Garbage and is always slower than a normal loop and my problem is not the large amount of cubes, it's the process that takes time since I can't not process those cubes but I, potentially, can process them in a way that consumes less time.
     
  4. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Dealing with that many game objects is going to be slow. Do you need them all on screen at the same time. What is it you are wanting to achieve and do they all need colliders?
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Don't. Whatever you are doing, there is a better way then using a million cubes. A million GameObjects will never perform efficiently. Ever.

    There are several options depending on what you are doing. Why not describe your goal in more detail for us?

    If you are building a MineCraft like system, investigate voxels. Combining the entire simulation into a single Mesh and adding and removing triangles from it will consistently out perform a million cubes.

    Another option to look into is particle systems. Particle systems are highly efficient for displaying a large number of similar objects.

    You can do the algorithm to determine which cubes to disable on another thread. You must do the actual disabling on the main thread. Unity isn't thread safe, and will throw a fit if you try and access Unity API from anything other then the main thread.
     
    aidinz and ThermalFusion like this.
  6. aidinz

    aidinz

    Joined:
    Apr 17, 2016
    Posts:
    63
    You are right, voxels should be it.
     
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    How long was it taking to enable/disable 1 million gameobjects? Just curious, as I've never tried anything close to that.
     
  8. aidinz

    aidinz

    Joined:
    Apr 17, 2016
    Posts:
    63
    I've changed the project to 2D since it's for a game jam so it takes a bit to answer your question but actually my current problem is generating them.

    Generating 25x25x25 a prefab consisting of a Unity's own cube with a custom script which is almost empty takes about 12 seconds on my machine (Core i3-2100 @ 3.10Ghz)
     
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Doesn't surprise me for 15,000 GameObjects. I don't think the system was ever designed with those numbers in mind. Especially if you have scripts running on them.

    If you really want to crash the system, try putting a Debug.Log in Start or Update on that single cube. A physics collider and a rigidbody will also cause a massive slow down.

    15,000 objects is a lot to handle.
     
  10. aidinz

    aidinz

    Joined:
    Apr 17, 2016
    Posts:
    63
    A lot of them are disable actually and game is actually running fine and I'm still not using any optimization like pooling. I'm sure it is not the way to do it, but just wanted to pinpoint this.
     
  11. kiriri

    kiriri

    Joined:
    Jan 14, 2011
    Posts:
    107
    1) Disable many gameobjects by disabling the root transform in their hierarchy. Construct your hierarchy in a way that you can disable entire groups of gameobjects at once.
    2) Don't use that many gameobjects. Most efficient way to solve minecraft would be computeShaders with buffers and use OnRenderImage with a Graphics.DrawProcedural() call . This would be 1 drawcall, and you could use the GPU to disable (set size to 0) 15000 gameobjects in no time at all.
    Another problem with 15000 gameobjects is, that Unity automatically tries to batch them into as few drawcalls as possible. This happens on the CPU and becomes more expensive, the more stuff is in your scene.
    Unity also recently implemented Instance Rendering, you might want to check that out.

    Take it from someone who has written such a system before. GOs are not the answer.

    PS: your title has a typo in it