schnappischnap schnappischnap - 3 months ago 15
C# Question

Gizmos only update when I hover over the scene or game view

I have a script:

//Assets/TestScript.cs
using UnityEngine;

public class TestScript : MonoBehaviour
{
public Vector2 a;
public Vector2 b;
public Vector2 c;
}


and it's editor script:

//Assets/Editor/TestEditor.cs
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(TestScript))]
public class TestEditor : Editor
{
public override void OnInspectorGUI()
{
TestScript script = (TestScript)target;

script.a = EditorGUILayout.Vector2Field("a", script.a);
script.b = EditorGUILayout.Vector2Field("b", script.b);
script.c = EditorGUILayout.Vector2Field("c", script.c);
}

[DrawGizmo(GizmoType.Active | GizmoType.Selected)]
static void DrawGizmos(TestScript script, GizmoType gizmoType)
{
Gizmos.DrawWireSphere(script.a, 1.0f);
Gizmos.DrawWireSphere(script.b, 1.0f);
Gizmos.DrawWireSphere(script.c, 1.0f);
}
}


It correctly shows the wire sphere gizmos, but when I edit the variables
a
,
b
or
c
the gizmos don't redraw unless I move my mouse over either the scene view or the game view. And even then it takes around a second.

The gizmos update properly when I remove the overridden
OnInspectorGui()
method.

I've tried adding
SceneView.RepaintAll()
,
HandleUtility.Repaint
and
Repaint()
to the beginning and end of the
OnInspectorGui()
method, but nothing changed.

Thank you.

Answer

Someone on reddit showed me the answer.

The SceneView.RepaintAll() method will update only the scene view, whereas UnityEditorInternal.InternalEditorUtility.RepaintAllViews() will update all the views.

The update OnInspectorGUI() overridden method now looks like this:

public override void OnInspectorGUI()
{
    TestScript script = (TestScript)target;

    script.a = EditorGUILayout.Vector2Field("a", script.a);
    script.b = EditorGUILayout.Vector2Field("b", script.b);
    script.c = EditorGUILayout.Vector2Field("c", script.c);

    if(Gui.changed)
        UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
}