Search Unity

mesh generation/release VS mesh loading from disk/release

Discussion in 'General Discussion' started by mailfromthewilds, Apr 4, 2021.

  1. mailfromthewilds

    mailfromthewilds

    Joined:
    Jan 31, 2020
    Posts:
    217
    generally generation is better cus you save lot of size. eg your game doesnt need to be 2gb with all the meshes, it can generate them when needed, and release when not needed.


    my question is about memory though (not in editor, in player build).
    theoretically you would be thinking if you release mesh the memory will free but maybe thats not the case?


    for sure thats not the case with EDITOR so why would it be the case with player?


    i was thinking about dynamically loading and releasing the mesh when needed/not needed anymore.
    BUT if it doesnt release then theres no point in designing such system im affraid

    (and the best choice would be to load all meshes into memory at first frame, never release them, and never load them again)

    so how is it? will mesh release when you want them to, in player build?
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,566
    Generally, generation is worse, because you'll have to use specialized tools (see .kkreiger), you'll have to write those tools, none of existing 3d editors will support those tools and on top of that the geometry likely will load slower compared to just slurping it off an SSD. So you actually lose quite a lot and gain little.

    Regarding release of mesh resources, common pattern for all dynamically created geometry is to destroy it directly yourself with Destroy. Basically it is a UnityEngine.Object - based class, meaning it is not expected to be released with a GarbageCollector and you should babysit its lifetime yourself.

    Unfortunately, which resources are managed by GC and which aren't - that part isn't clear cut in documentation. There was a discussion about that recently: https://forum.unity.com/threads/is-...n-memory-so-gc-couldnt-relocate-them.1084220/ although @superpig provided a good explanation:

    One possible pattern for dynamically created resources is to create them within "OnEnable" and destroy them within "OnDisable". This works reliably enough.
     
  3. mailfromthewilds

    mailfromthewilds

    Joined:
    Jan 31, 2020
    Posts:
    217
    you mean using mesh file is Object class and you use destroy on it? will be casting to object needed?

    yeah i didnt really take tools into account
     
  4. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,566
    It's not "Object" but UnityEngine.Object. Those are wrappers around internal C++ objects within the engine.

    Casting is not needed. Given that Mesh derives from UnityEngine.Obejct, it will be accepted by any method that takes UnityEngine.Object.

    https://docs.unity3d.com/ScriptReference/Object.Destroy.html