Mesh in the Scene View - how many DrawMesh calls and how to remove again?

Hi All

The Task:

I’m trying to make an editor, which shows a procedurally created mesh in the scene view, which is part of an EditorWindow module I’m writing.

What I do

To enable a OnSceneGUI function for this code, I’m using the

SceneView.onSceneGUIDelegate += OnSceneGUI;

which allows an OnSceneGUI function to be called.

In this function, I use the following call to render a mesh with a specified material:

Graphics.DrawMesh( TestMesh, Vector3.zero, Quaternion.identity, MeshMaterial, 0);

Please note the MeshMaterial is a predefined material, which is just a simple diffuse one.

The Problem

Now, it seems like this causes my mesh to be rendered anew each time the OnSceneGUI is called, and if I put in a boolean check to only call it once it stays (and does not slow down my view of the scene).

Also, I don’t know how to make it disappear again.

Can anyone point me in the direction I should have taken to make this work properly?

Thanks

Henrik

I found another post stating the same problem.

The workaround was to create a GameObject with the mesh and set it to Hide and not show in inspector.

I don’t know why this is happening but you can clear all drawn meshes calling EditorUtility.SetDirty(target); in your OnSceneGUI.

In your case you can draw the mesh each frame

void OnSceneGUI(SceneView view)
{    
    //submit meshes here
    Graphics.DrawMesh( ... );

    //delete meshes of previous frame and draw new meshes
    EditorUtility.SetDirty(target);    
}

Or choose to draw meshes only once

void OnSceneGUI(SceneView view)
{    
    if (drawMeshes)
    {      
       //submit meshes here
       Graphics.DrawMesh( ... );
       EditorUtility.SetDirty(target);    

       drawMeshes = false;
    }
}

Hi. Try doing:

EditorUtility.SetDirty(sceneView);

Just after your DrawMesh call. If you do SetDirty on target, then your project might become dirty each frame. Doing it on the sceneView itself instead, it won’t dirty the project.