简体   繁体   中英

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM