UnityRef is currently in early development. Some features may be incomplete and/or not functioning.

UNITYREF

Your Pit Stop For Solving ANYTHING in Unity

assets

[Procedural] Automate LOD Generation for Editor Generated Meshes

Solution

procedural generationgeometryeditormesh generation3D modeling

Unity 2022.3.x - Unity 6.3.x

Published Mon, May 4

Issue

 Developers frequently encounter performance bottlenecks when using procedural geometry because the standard LODGroup system is often perceived as being locked to imported assets. Without a programmatic way to generate Mesh levels of detail, custom-generated meshes remain unoptimized at long distances, impacting rendering budgets and memory usage.

Explanation

The MeshLodUtility class allows developers to simplify geometry programmatically. This is essential for procedural tools or editor scripts that produce high-fidelity geometry requiring optimization. By invoking these methods within your script, the optimization process is integrated into the asset creation pipeline.

  1. Reference the source Mesh you wish to optimize.
  2. Define the reduction thresholds and parameters for the MeshLodUtility logic.
  3. Call the GenerateMeshLods method to return an array of simplified Mesh objects.
  4. Attach a LODGroup component to the GameObject containing your script results.
  5. Map the generated Mesh objects to the appropriate LOD levels in the LODGroup array.

Additional Tips:

  • Use AssetDatabase.AddObjectToAsset to parent the new MeshLodUtility outputs to the original mesh file to keep the Project window organized.
  • High vertex counts may increase generation time; ensure the source Mesh is marked as readable in the import settings or memory before processing.
  • The MeshLodUtility creates brand new Mesh instances that must be manually destroyed if not saved to the AssetDatabase to prevent memory leaks.

TL;DR

The MeshLodUtility class provides a programmatic interface to calculate and generate Mesh levels of detail for assets created within the Unity Editor.


Related Posts Haven't quite found a solution to your problem? We think these posts might help you.

Content inspired by a Unity discussion post.