EditorApplication.update and Graphics.drawmesh problem

I have got a static class which has a function hooked up to EditorApplication.update;

During each update, I am using Graphics.DrawMesh(), which makes a cube spawn in the world.

Now, the interesting part is, because every update I call DrawMesh(), for some reason, the old meshes don’t clear themselves, unless I move or rotate or scale any object within the scene. If I disable components / gameObjects it also will clean the old meshes from the screen.

So if it’s left running, it will keep spawning cube meshes in a specified range. It will clear them up as soon as I move, rotate or scale something within the scene.
SceneVeiw.RepaintAll() at the start of my update doesn’t help to remove the old meshes :frowning:

If you don’t know the answer, at least tell me, what sort of function gets called when I move the object? Clearly it’s different to just flying around the scene.
Thank you very much!

Have a look:

using UnityEngine;
using UnityEditor;

[InitializeOnLoad]
static public class meshDrawer {

    static Mesh aMesh;
    static Material aMaterial;

    static meshDrawer(){
        EditorApplication.update -= Update;
        EditorApplication.update += Update;
        aMesh = GameObject.Find("Cube").GetComponent<MeshFilter>().sharedMesh;
        aMaterial = GameObject.Find("Cube").renderer.sharedMaterial; //You will need a cube object in your hierarchy for this to work
        }

    static void Update() {
            Graphics.DrawMesh(aMesh, new Vector3(Random.Range (0f,2f), Random.Range(0f,4f), Random.Range(0f,2f)), Quaternion.identity, aMaterial, 0);
    }
}

What da heck? :smile:
How can we make it draw one mesh per the update callback, and clean the old ones every callback?

Hello.

I’ve got the same propblem, but after trying some different approaches, I found a solution.

The problem is, that while in the editor Unity only calls the default Update() method when something happens.
The Graphics class seems to clear it’s caches only in the default Update() method.
In your case, when you move something around, the Update() is called, the caches are cleared, and after that you are drawing all meshes regularly till something moves again (which can be a long time).

To fix this, just use the default Update() method to draw all your meshes.
I hope that solves your problem.

Greetings
Chillersanim

1 Like