Search Unity

Question General Best Practices

Discussion in 'General Graphics' started by Dneubern, Feb 26, 2023.

  1. Dneubern

    Dneubern

    Joined:
    Mar 2, 2019
    Posts:
    24
    I've seen some assets in the Asset Store that made me wonder if their application is generally the best practice.
    I.e.: Humanoid Characters, all different from one another mesh-wise, and all loaded in a single FBX. The control of what's visible is making meshes active / disabled.
    1. Wouldn't it be best, loading each of these meshes separately, as they are needed?

    Another example are character modifications such as Hats, beards and so on. All already loaded in a single file, controlled using hidden meshes.

    Another Question concerns animations.
    2. Are unused animations built and loaded in the memory?
    If so, how to disable these animations from being built/loaded? Deleting them, or changing their animation controller?

    Shaders.
    3. Concerning shaders. Are all unused shaders, just as above concerning animations, built and loaded to memory? If so, how do I disable them, so I can achieve the best performances?

    Thank you all for your time :)
     
  2. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,928
    In Unity, any assets that are not referenced by something that's actually used in a scene that's part of the build are just ignored (unless they're in a Resources folder). Not just "not loaded in memory", but not included in the build at all. This includes shaders and animation clips.

    Note this is a double-edged sword: if your scripts happen to load stuff at runtime by name, and the things being loaded are otherwise unused (not directly referenced by something that's part of the scene), you script will work fine in-editor but will blow up with a null ref exception in the build.
     
  3. Dneubern

    Dneubern

    Joined:
    Mar 2, 2019
    Posts:
    24
    So, If I have a Scene, with an animation controller in it. Even if some of the animations specified in the controller are never called, are they loaded in the memory?

    Also i.e.: This is from the AdvancedPeopleSystem2.
    Screenshot 2023-02-27 at 13.21.33.png

    One FBX file has several of the possible customization.
    This also happens with the Low Poly Animated People
    Screenshot 2023-02-27 at 13.22.49.png


    My question is, even if I only use one of these meshes, are all meshes (even those hidden) still being loaded in the memory during gameplay?
     
  4. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,928
    Correct. If you're never going to use some states in the controller, just delete them. This way the animation clips referenced by these states will not be loaded.

    Technically, if the meshes are referenced in the scene (ie, there's GameObjects referencing them even if they're disabled) you're "using" them as far as Unity is concerned, because there's no way Unity can know if later on you will decide to enable these GameObjects during your game. They will still be included in the build and loaded.

    Merely hiding/disabling a GameObject will not unload meshes rendered by it, that would be terrible for performance: imagine you wrote a script that makes the mesh "blink" by disabling/enabling the GO or its renderer component several times per second, loading the mesh from disk and then unloading it every single time would mean a lot of roundtrips to your hard drive.

    Even if a GameObject is disabled, the mesh will still be around in memory unless you explicitly destroy it. If you're not interested in rendering some of these at any point during your game, why not just delete the GameObjects entirely instead of disabling them? This way they're not even in the scene/prefab to begin with.

    I mean, assets will often provide all customization options out of the box. It's up to you to delete the stuff you're not going to use at all.

    Also note that for Unity, meshes are meshes, materials are materials, and clips are clips no matter how many FBX files they were imported from. As far as Unity is concerned, a single FBX containing two meshes, and two FBX with a single mesh and each result in the exact same thing once imported: two mesh assets.
     
    Last edited: Feb 27, 2023
  5. Dneubern

    Dneubern

    Joined:
    Mar 2, 2019
    Posts:
    24
    Thank you so much for the answers... Only have one more then.

    As far as Objects go... Imagine I have three Instances of the same FBX loaded in the Scene.
    Each FBX has the same 10 meshes, and I currently only use 3 meshes. The GameObject has the 10 meshes referenced to it, but 9 of them are hidden.

    In the memory, are 30 meshes loaded, or only 10 meshes loaded?
     
  6. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    If the meshes are not modified in any way, then 10 meshes will be loaded.
    Duplicates are created when you start messing with them (e.g. calling MeshFilter.mesh: Unity - Scripting API: MeshFilter.mesh (unity3d.com))
     
  7. Dneubern

    Dneubern

    Joined:
    Mar 2, 2019
    Posts:
    24
    Thank you for the answers! It was a great help :D
    Have a good week everyone!