[英]How can I draw the lines also in runtime and not only in scene as gizmos?
[英]Gizmos only update when I hover over the scene or game view
我有一个脚本:
//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()
方法时,小控件将正确更新。
我尝试将SceneView.RepaintAll()
, HandleUtility.Repaint
和Repaint()
到OnInspectorGui()
方法的开头和结尾,但没有任何更改。
谢谢。
有人在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();
}
首先,您应该创建一个EditorWindow
在这里看到它: http : //docs.unity3d.com/ScriptReference/EditorWindow-autoRepaintOnSceneChange.html
所有EditorWindows都具有以下属性: autoRepaintOnSceneChange
您可以将其设置为true,并在某些更改后重新绘制窗口。 您也可以在editorWindow的Update()中手动调用Repaint()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.