How do you make some debug rendering in editormode (game not playing) which takes the depth buffer into account? I need a couple of transparent triangles to visualize a tool for the level designers.
I consider using GameObjects with MeshFilter, MeshRenderer and HideFlags.HideAndDontSave.
I think this solution will work though it is slightly akward. Is it possible to avoid using GameObjects?
I found Graphics.DrawMesh which solves the problem without using GameObjects in playmode. However in editormode (!Application.isPlaying) the mesh no longer looks transparent and the performance keeps decreasing as if the render queue slowly grows towards infinity.
Any ideas how to get a stable debugrendering in editormode?
The approach with Graphics.DrawMesh() is the preferred one, since it will make the mesh fit in naturally into the rendering queue. Thanks to that it can be per pixel lit, cast and receive shadows, and, of course, can be Z tested. Use that method in the Update callback on a MonoBehaviour with the ExecuteInEditMode attribute set.
NOTE: in edit mode Update is only called when the scene is updated, i.e. when a property of any component is changed via inspector or when objects are manipulated in the scene view.
Here's a package showing mesh being modified in Update and submitted to rendering via Grahics.DrawMesh():
DrawMeshInEditMode.unitypackage
[ExecuteInEditMode]
public class DrawMeshInEditMode : MonoBehaviour
{
public Material m_Material;
private Mesh m_Mesh;
private Matrix4x4 m_Trs;
[...]
void Update()
{
[...]
Graphics.DrawMesh(m_Mesh, m_Trs, m_Material, 0);
}
}
What works stably and well for me is using Graphics.DrawMeshNow() from the OnRenderObject() function of the Monobehavior script with ExecuteInEditMode set on an object and using Application.IsPlaying to skip the drawing when the game is playing
(using DrawMesh() had problems for me - if you need to change the material, try calling SetPass on the material before DrawMeshNow)