我有一个脚本:
//Assets/TestScript.cs
using UnityEngine;
public class TestScript : MonoBehaviour
{
public Vector2 a;
public Vector2 b;
public Vector2 c;
}
及其编辑脚本:
//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);
}
}
它正确显示了导线球体小控件,但是当我编辑变量a
,b
或c
时,小发明不会重绘,除非我将鼠标移到任何一个上场景视图或游戏视图。即使这样,它也需要大约一秒钟。
当我删除被覆盖的OnInspectorGui()
方法时,gizmos会正确更新。
我已尝试将SceneView.RepaintAll()
,HandleUtility.Repaint
和Repaint()
添加到OnInspectorGui()
方法的开头和结尾,但没有任何更改。
谢谢。
答案 0 :(得分:0)
首先,您应该创建 EditorWindow
在此处看到它:http://docs.unity3d.com/ScriptReference/EditorWindow-autoRepaintOnSceneChange.html
所有 EditorWindows 都具有以下属性: autoRepaintOnSceneChange
您可以将其设置为true,并在某些内容发生变化时重新绘制窗口。 您也可以在editorWindow的Update()中手动调用重绘()
答案 1 :(得分:0)
有人在reddit上给我看了答案。
SceneView.RepaintAll()
方法仅更新场景视图,而UnityEditorInternal.InternalEditorUtility.RepaintAllViews()
将更新所有视图。
更新OnInspectorGUI()
重写方法现在如下所示:
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();
}