overlay | 要添加的 Overlay。 |
show | 如果为 True,则立即显示 Overlay,如果为 False,则添加而不显示。 |
将一个 Overlay 添加到此画布。添加的 Overlay 将在关联的 EditorWindow 中显示,直到它们被移除。
在大多数情况下,Overlay 使用 OverlayAttribute 自动实例化。但是,也可以直接从 OverlayCanvas 添加和移除 Overlay。这对于需要某些上下文的短暂 Overlay 很有用。例如,作为编辑器的扩展。
使用此方法添加的 Overlay 必须实现 ITransientOverlay。一个 Overlay 只能属于一个 OverlayCanvas。要在多个窗口中显示一个 Overlay,您必须为每个窗口实例化一个 Overlay。
using UnityEditor; using UnityEngine; using UnityEditor.Overlays; using UnityEngine.UIElements;
// Attach this MonoBehaviour to a GameObject to view the example Overlay in the last active Scene View class OverlayCanvasExample : MonoBehaviour {}
// An Editor for our OverlayCanvasExample MonoBehaviour. This will show and hide the example Overlay when a GameObject // with the OverlayCanvasExample component is selected and deselected. [CustomEditor(typeof(OverlayCanvasExample))] class OverlayCanvasExampleEditor : UnityEditor.Editor { ExampleEditorOverlay m_Overlay;
void OnEnable() { // Create a new Overlay with a label indicating the selected GameObject name. m_Overlay = new ExampleEditorOverlay(name); SceneView.lastActiveSceneView.overlayCanvas.Add(m_Overlay); }
void OnDisable() { // If the Overlay has not already been closed, close it when this editor is disabled. Added Overlays will // persist until they are closed. m_Overlay?.Close(); } }
class ExampleEditorOverlay : Overlay, ITransientOverlay { // Overlays added directly to the canvas must implement ITransientOverlay, meaning they control their own lifecycle. public bool visible => true;
string m_Message;
public ExampleEditorOverlay(string message) { m_Message = message; }
public override VisualElement CreatePanelContent() { var root = new VisualElement(); root.Add(new Label(m_Message)); root.Add(new Button(Close) { text = "Close" }); return root; } }