Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question Memory optimization - Duplicate prefabs referenced multiple times

Discussion in 'Editor & General Support' started by drazuerg, Oct 7, 2023.

  1. drazuerg

    drazuerg

    Joined:
    Jan 31, 2014
    Posts:
    62
    Hello there!

    I'm in the polish phase of my mobile game project and looking for any optimization possible.

    I have an enemy template prefab, and all my enemies in the game are children of this main prefab. The main prefab holds a "PoisonVFX" particle system, also a prefab, that I activate or deactivate at runtime (enemies can deal poison or not depending on the context).
    NMETemplate.png

    Then I have some prefab levels, that are basically a big hierarchy of enemies. All of them of course hold the PoisonVFX, sometimes deactivated.
    EnemiesWaves.png

    Now here is the thing, in the memory profiler, this PoisonVFX particle system is shown hundreds of times !! I'm not sure how to set it up so it's only used once ? Each of them is only 11.8KB but there are so many of them that I believe I could spare something around 10MB of RAM (which comparatively would be HUGE regarding my current optimization efforts).

    MemoryList.png

    Thanks for your help :)
     
    Last edited: Oct 7, 2023
  2. villevli

    villevli

    Joined:
    Jan 19, 2016
    Posts:
    90
    What you can do is instantiate the PoisonVFX prefab containing the particle system at runtime only when you need it to be active so there is no deactivated ones taking memory. So instead of adding the prefab as a child of the enemy prefab beforehand you should add a reference to the PoisonVFX prefab in a script in the enemy prefab and then at runtime when the poison vfx needs to activate instantiate the prefab with the enemy object as it's parent. If the poison needs to deactivate you can destroy the instantiated one to free the memory and instantiate it again if needed later
     
  3. drazuerg

    drazuerg

    Joined:
    Jan 31, 2014
    Posts:
    62
    Hello ! Thanks for your quick reply. That sounds like a reasonable solution :)

    If I kept my current setup but the Poison particle system was not a prefab (but a regular gameobject) it would still take up the same memory space right ?
     
  4. villevli

    villevli

    Joined:
    Jan 19, 2016
    Posts:
    90
    Yes a prefab added into the hierarchy in edit mode (which is called a prefab instance) takes up same memory as a non prefab object with the same content. This is because the prefab instance is actually a copy of the data in the prefab. Like when instantiating it via code it creates a copy. In edit mode the copy is still synced with the source prefab so it seems like a reference to the same prefab data in memory but actually is a copy even at that point. In play mode even that link to the source prefab is lost so it is then same as any object in hierarchy. Assets outside the hierarchy like texture or mesh referenced by e.g. a mesh renderer are not copied and still point to the original data and are shared between mesh renderers using the same assets. Only the mesh renderer itself is a copy because it is part of the gameobject in the hierarchy. So this copying only applies to gameobjects and components that are in the hierarchy and not to referenced assets that are outside the scene or prefab
     
  5. drazuerg

    drazuerg

    Joined:
    Jan 31, 2014
    Posts:
    62
    Thanks a lot for the detailed answer, appreciated :)