版本: Unity 6 (6000.0)
语言英语
  • C#

EditorWindow.OnLostFocus()

建议更改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交内容,但我们会阅读用户提出的每项建议,并在适当情况下进行更新。

关闭

提交失败

由于某种原因,您的建议更改无法提交。请<a>稍后再试</a>。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

描述

当窗口失去键盘焦点时调用。

其他资源: OnFocus


当您失去对该窗口的焦点时,恢复正常视图。

// 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;
    }
}