Version: Unity 6 (6000.0)
LanguageEnglish
  • C#

EditorWindow.OnFocus()

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

Description

Called when the window gets keyboard focus.

Additional resources: OnLostFocus.


Preview your camera in orthographic mode when you select the window.

// Simple script that lets you preview your main camera in
// Orthographic view when selected.

using UnityEngine;
using UnityEditor;
using UnityEngine.UIElements;

public class OrthographicPreview : EditorWindow
{
    RenderTexture renderTexture;
    Camera camera;
    Image image;

    [MenuItem("Examples/Orthographic Previewer")]
    static void Init()
    {
        OrthographicPreview window =
            (OrthographicPreview)EditorWindow.GetWindow(typeof(OrthographicPreview), true, "My Empty Window");
        window.Show();
    }

    void OnEnable()
    {
        int w = (int)this.position.width;
        int h = (int)this.position.height;

        image = new Image();
        renderTexture = new RenderTexture(w, h, 32, RenderTextureFormat.ARGB32);
        camera = Camera.main;
    }
    
    void OnDisable()
    {
        Object.DestroyImmediate(renderTexture);
    }

    void CreateGUI()
    {
        var buttonClose = new Button();
        buttonClose.text = "Close";
        buttonClose.clicked += () =>
        {
            camera.orthographic = false;
            this.Close();
        };
        rootVisualElement.Add(buttonClose);

        if (renderTexture != null)
        {
            image.image = renderTexture;
            rootVisualElement.Add(image);          
        }
    }

    void OnFocus()
    {
        Selection.activeTransform = camera.transform;
        camera.orthographic = true;
    }

    void Update()
    {
        int w = (int)this.position.width;
        int h = (int)this.position.height;
        if (renderTexture.width != w || renderTexture.height != h)
        {
            Object.DestroyImmediate(renderTexture);
            renderTexture = new RenderTexture(w, h, 32, RenderTextureFormat.ARGB32);
            image.image = renderTexture;
        }

        if (camera != null)
        {
            camera.targetTexture = renderTexture;
            camera.Render();
            camera.targetTexture = null;
        }
    }

    void OnLostFocus()
    {
        camera.orthographic = false;
    }
}