Search Unity

Question Mesh changes for custom importers are not persisted

Discussion in 'Asset Importing & Exporting' started by konikun, May 16, 2022.

  1. konikun

    konikun

    Joined:
    May 11, 2011
    Posts:
    3
    Hi y'all, I'm trying to figure out what is the correct way of creating a custom mesh importer that consistently produces the same results both on the editor and in builds.
    My goal:
    • I want to reduce memory consumption of some of the meshes in our project by discarding, on import time, vertex streams that I don't use (for example, for collider meshes, I don't need color / uv / tangent streams).
    What we have:
    • We have an AssetPostProcessor and we go through imported meshes and discard streams based on some heuristic (for example, mesh name).
    • Besides that, I'm using the memory profiler to see the size that a specific mesh occupies in memory in a build and compare between cases with / without the AssetPostProcessor
    Example:
    Code (CSharp):
    1.  
    2. static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
    3.     {
    4. bool anyAssetModified = false;
    5.         foreach (string asset in importedAssets)
    6.         {
    7.             var objects = AssetDatabase.LoadAllAssetsAtPath(asset);
    8.             foreach (var obj in objects)
    9.             {
    10.                 if (!(obj is Mesh mesh))
    11.                     continue;
    12.  
    13.                 if (mesh.name.Contains("_coll"))
    14.                 {
    15.                     mesh.uv = null;
    16.                     mesh.colors = null;
    17.                     mesh.tangents = null;
    18.                     mesh.UploadMeshData(false);
    19.                     EditorUtility.SetDirty(mesh);
    20.                     anyAssetModified = true;
    21.                 }
    22.             }
    23.         }
    24.         if (anyAssetModified)
    25.         {
    26.             AssetDatabase.SaveAssets();
    27.         }
    28. }
    When forcing a "reimport" on the editor, it seems to work and I see less streams on the inspector.
    upload_2022-5-16_10-40-50.png
    But as soon as I close unity and open it again, the original ones are back.
    upload_2022-5-16_10-41-42.png

    I found a workaround though: by forcing asset reimports when I'm making a build it seems like those changes are kept (from looking at the memory impact, the mesh occupies less space indeed).

    But is this the way to do asset post processing for meshes? I feel like I'm missing something here.
    I also wonder in what priority order is our "custom postprocessing" applied in relation to the default one?
    For example, if I do "mesh.tangents = null" in my post processor, but on the inspector I have "Tangents" set to "Calculate", does it undo what I just did?

    Thanks :)