Version: 2022.3
LanguageEnglish
  • C#

OverlayCanvas.Add

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Obsolete OverlayCanvas.Add(Overlay, bool) is obsolete, use Add(Overlay) overload.

Declaration

public void Add(Overlays.Overlay overlay, bool show);

Parameters

overlay The Overlay to add.
show True to display the Overlay immediately, false to add without displaying.

Description

Add an Overlay to this canvas. Added Overlays will be displayed in the associated EditorWindow until they are removed.

In most cases, Overlays are instantiated automatically using OverlayAttribute. However, it is also possible to add and remove Overlays from an OverlayCanvas directly. This is useful for short-lived Overlays that require some context. E.g., as an extension of an Editor.

Overlays added using this method must implement ITransientOverlay. An Overlay may only belong to a single OverlayCanvas. To display an Overlay in multiple windows, you must instantiate an Overlay for each window.

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; } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961