Search Unity

How to manage the scene in a free roaming game?

Discussion in 'Scripting' started by uiniti, May 26, 2015.

  1. uiniti

    uiniti

    Joined:
    Feb 4, 2015
    Posts:
    74
    My game has a big area with several buildings that the player can explore.

    I have one main scene plus several secondary scenes that are loaded and destroyed when needed.
    How can I improve the memory usage?

    Is Unity loading and unloading objects when they are SetActive(false)? is Unity doing the same when object are culled?
    Should I destroy and reload them? When I'm supposed to call UnloadUnusedAssets?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    I have some, though probably not all of the relevant answers:

    No. Inactive GameObjects are still there in memory.

    Objects that are culled are simply not visible; they are still in memory, and even more, things like script Updates and animations are still running.

    Be aware that instantiating an object, especially a big complicated one like a building with game areas inside, is going to cause a freeze when it happens. You might get around this slightly by using LoadLevelAdditiveAsync, but there's still probably going to be a freeze when the objects are finally added into the scene. That said, if you don't have enough memory to keep the whole thing loaded, it's probably necessary.
     
  3. uiniti

    uiniti

    Joined:
    Feb 4, 2015
    Posts:
    74
    so how am I supposed to manage it?

    Are there good tutorial for streaming a level?
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  5. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    Keeping in mind StarManta's answers, your goal is to reduce runtime memory use, right? (Occlusion culling helps speed up rendering, but it has very little impact on memory use.)

    If you can't keep it all in memory, you'll have to load and destroy as necessary. In lieu of a full streaming system like SECTR Stream, you can put trigger colliders around different "zones." When the player enters a zone's trigger collider, load its content using Application.LoadLevelAssitiveAsync. The content should be a separate scene, ideally grouped under a single GameObject. When the player exits the trigger collider, destroy that GameObject to unload the zone content.

    In practice, you'll want to load and unload neighboring zones, not the zone the player is in. Otherwise content will pop into view right in front of the player.

    Any custom scripts on your zone content GameObjects should do a minimum of work in Awake. If you have to do processing in Start, run it as a coroutine and distribute the work over several frames to help minimize the freeze when the GameObject is added to the scene at the end of the load.
     
    uiniti likes this.
  6. uiniti

    uiniti

    Joined:
    Feb 4, 2015
    Posts:
    74
    My fear is that doing that will cause huge dips in the fps because he's loading and destroying stuff.
     
  7. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    A balance probably needs to be struck if there are huge numbers of objects. Portions of the scene can be loaded and unloaded. Objects can be "recycled" with an object pooling system to prevent the need to destroy / load everything. With pooling your memory use would be fairly consistent and you'd put less stress on the GC.
     
    uiniti likes this.
  8. uiniti

    uiniti

    Joined:
    Feb 4, 2015
    Posts:
    74
    I was already counting that, what I was asking is if anybody already tried to do it and what kind of performances they got.
    Streaming big assets doesn't seem to work very well with unity.