# Add(Overlay)

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

## Definition

* **Type:** Method
* **Namespace:** [UnityEditor.Overlays](/engine/6000.3/script-reference/unityeditor/overlays.md)
* **Assembly:** UnityEditor

```csharp
public void Add(Overlay overlay)
```

### Parameters

**** (\[Overlay]\(/engine/6000.3/script-reference/unityeditor/overlays/overlay)): The Overlay to add.

### Remarks

In most cases, Overlays are instantiated automatically using [OverlayAttribute](/engine/6000.3/script-reference/unityeditor/overlays/overlayattribute.md). However, it is also possible to add and remove Overlays from an [OverlayCanvas](/engine/6000.3/script-reference/unityeditor/overlays/overlaycanvas.md) 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](/engine/6000.3/script-reference/unityeditor/overlays/itransientoverlay.md). An [Overlay](/engine/6000.3/script-reference/unityeditor/overlays/overlay.md) may only belong to a single [OverlayCanvas](/engine/6000.3/script-reference/unityeditor/overlays/overlaycanvas.md). To display an [Overlay](/engine/6000.3/script-reference/unityeditor/overlays/overlay.md) in multiple windows, you must instantiate an [Overlay](/engine/6000.3/script-reference/unityeditor/overlays/overlay.md) for each window.

### Examples

```csharp
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;
    }
}
```
