Search Unity

MeshFilter references in prefabs. How do they work?

Discussion in 'Prefabs' started by jc_crash, Jan 27, 2020.

  1. jc_crash

    jc_crash

    Joined:
    Jul 30, 2019
    Posts:
    21
    Hi all,

    I have a question more about how MeshFilter references are working. I have a lot of model prefabs which obviously have MeshRenderers and MeshFilters. The MeshFilter for each holds a reference to a mesh I have generated which all reside in a Unity folder.

    I want to be able to swap out the .mesh files in the mesh directory with lower resolution mesh files of an identical name and have the MeshFilter component on the prefabs hold their mesh references. Currently, the MeshFilters lose their reference and tell me their mesh is missing. Then when I swap the originals back in, the references return.

    How are the references generated? Are the mesh file names hashed and then referenced that way? Are the file contents hashed?

    At this stage my best option is to write an editor function to swap all the meshes.

    Thanks!
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    The same way all references to assets in Unity work, by guid.

    Each asset has a .meta file next to it. That file contains a guid value. Whenever anything references that file, it's the guid that's referenced. So if you change the content of an asset, but don't touch it's .meta file, you should keep a reference.

    This gets a bit more complicated when there's several assets in one file (like in an .fbx file that contains meshes, materials, animations, etc.), but that doesn't sound like what you're dealing with.


    So, in theory, you should be fine as long as you overwrite the mesh file you're editing. If you're deleting it, and then putting a file with the same name there, you'll lose the reference.

    If that doesn't help, please post the code you use to change the file, and I'll see if there's anything obvious. We swap files like that all the time, so it should work for sure.
     
  3. jc_crash

    jc_crash

    Joined:
    Jul 30, 2019
    Posts:
    21
    Hi Baste,

    Thanks for the insight! Your hint about the .meta files made me click...duh!

    We had done some tests where we generated equivalent meshes in another location within the project, copied the existing ones out of the project, and put the new ones in their place (all in file explorer, not Unity). Then when clicking back to Unity, the prefabs would seem to hold their references to the new objects. However if one replacement mesh is missing, all references would be missing which I found rather strange.

    But nonetheless, thanks for the pointer. We just need to kick out the .mesh files, replace them with the new .mesh files, all while keeping the existing .mesh.meta files in place. That way the link is not lost.

    Thanks!