Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Graphics.DrawMesh's refresh issue

Discussion in 'Scripting' started by realaldrick, May 19, 2020.

  1. realaldrick

    realaldrick

    Joined:
    May 19, 2020
    Posts:
    22
    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:

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System;
    5. using UnityEngine.Rendering;
    6.  
    7. [ExecuteInEditMode]
    8. public class MyScript: MonoBehaviour
    9. {
    10.    ....
    11.  
    12.     public void OnEnable()
    13.     {
    14.             _isRender = true;
    15.             SceneView.duringSceneGui += DrawStuff;
    16.     }
    17.  
    18.     public void OnDisable()
    19.     {
    20.         _isRender = false;
    21.         SceneView.duringSceneGui -= DrawStuff;
    22.         SceneView.RepaintAll();
    23.     }
    24.  
    25.     void Update()
    26.     {
    27.         _isRender = true;
    28.     }
    29.  
    30.     void DrawStuff(SceneView sceneView)
    31.     {
    32.         if (_isRender)
    33.         {
    34.                 UnityEngine.Graphics.DrawMesh(someMesh, someObject.transform.position, someObject.transform.rotation, someMaterial, 0);
    35.                 _isRender = false;
    36.         }
    37.     }
    38.  
    39. }
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,992
    if you disable "if (_isRender)" line, doesnt that work better?
     
  3. realaldrick

    realaldrick

    Joined:
    May 19, 2020
    Posts:
    22
    I don't think so. And without that, there will be some other artifacts.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    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.
     
  5. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    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.
     
  6. realaldrick

    realaldrick

    Joined:
    May 19, 2020
    Posts:
    22
    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.
     
  7. bitlysub2anf

    bitlysub2anf

    Joined:
    May 8, 2020
    Posts:
    71
    Try replacing the Update with FixedUpdate or LateUpdate
     
  8. bitlysub2anf

    bitlysub2anf

    Joined:
    May 8, 2020
    Posts:
    71
    Try FixedUpdate first (if I did not make it clear, replace Update with the other two)
    And let me know if it works
     
  9. realaldrick

    realaldrick

    Joined:
    May 19, 2020
    Posts:
    22
    No it doesn't.Neither does editorapplication.update delegate.
     
  10. bitlysub2anf

    bitlysub2anf

    Joined:
    May 8, 2020
    Posts:
    71
    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^
     
    realaldrick likes this.
  11. realaldrick

    realaldrick

    Joined:
    May 19, 2020
    Posts:
    22
    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.
     
  12. Zylinski

    Zylinski

    Joined:
    Dec 17, 2012
    Posts:
    2
    Hi. Try doing:
    Code (CSharp):
    1. EditorUtility.SetDirty(sceneView);
    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.
     
  13. jgamboa_unity

    jgamboa_unity

    Joined:
    Oct 7, 2020
    Posts:
    4
    Hey man, I just had to log in to thank you. This does definitely work!

    Just make sure you call the `EditorUtility.SetDirty(sceneView)` before the `SceneView.RepaintAll()` function.

    Thank you very much this has been a problem of my tools from our previous project!

    Truly grateful for this.