schnappischnap
schnappischnap

Reputation: 123

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.

Upvotes: 0

Views: 3488

Answers (2)

schnappischnap
schnappischnap

Reputation: 123

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();
}

Upvotes: 4

Bamdad
Bamdad

Reputation: 856

At first, you should create an EditorWindow

Here see it: http://docs.unity3d.com/ScriptReference/EditorWindow-autoRepaintOnSceneChange.html

All EditorWindows have the following property: autoRepaintOnSceneChange

You can set it true, and it repaints the window when something has changed. You can also call Repaint() manually in the Update() of the editorWindow

Upvotes: 1

Related Questions