I am using Graphics.DrawMesh to draw some helper meshes in the scene view.Generally it works,along with a minor bug, which is when I begin to draw it or turn it off, some times I need to click some object with meshFilter on it to make the scene view refresh(so that the content of drawMesh will appear or disappear). I tried SceneView.RepaintAll at several steps, which does not work.
When directly using it as a gameObject component script, it is much better, when using it as a EditorWindow script, this issue is more frequent. Is that a Unity bug? Maybe the Graphics.DrawMesh’s is not happening instantly right after it is executed?
Anyone has an idea?
Below is the general structure of this script:
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
using UnityEngine.Rendering;
[ExecuteInEditMode]
public class MyScript: MonoBehaviour
{
....
public void OnEnable()
{
_isRender = true;
SceneView.duringSceneGui += DrawStuff;
}
public void OnDisable()
{
_isRender = false;
SceneView.duringSceneGui -= DrawStuff;
SceneView.RepaintAll();
}
void Update()
{
_isRender = true;
}
void DrawStuff(SceneView sceneView)
{
if (_isRender)
{
UnityEngine.Graphics.DrawMesh(someMesh, someObject.transform.position, someObject.transform.rotation, someMaterial, 0);
_isRender = false;
}
}
}
When you’re in Editor mode I don’t think Unity runs your Update() methods every frame. It runs a lot less frequently, maybe once a second or less. Only when the object in question is inspected or manipulated is it immediately run.
You can also try to add your own update callback to UnityEditor.EditorApplication.update. The callbacks that you add will be executed frequently, because its behaviour differs from the actual Update run by ExecuteInEditMode.
If it is the Update’s frequency problem, it will cause the mesh not to render. But current situation is ,it will stay rendering after turned off until some scene model is selected. And this is not happening when using the code above directly as a component script, but occuring mostly only when calling it from a editor window interface.
It could possibly be either Unity’s bugs or that the game is not optimized or has too much stuff going on. This is the extent of what I know. Hope your problem gets fixed soon ^o^
Thank you. I create some hack fix for it,which can make sure that this issue doesn’t show when turning off. But when turning on, it still happens occasionally.
Hope I can find the real reason behind this issue.
Just after your DrawMesh call. SetDirty on the sceneView itself won’t dirty your project, but it seems to make sure the sceneView is marked as dirty correctly.