You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity编辑器窗口:透明背景与鼠标位置启动实现方案咨询

实现Unity编辑器窗口:鼠标位置启动+透明背景

你的需求完全可以实现,以下是具体方案,分两部分拆解解决:

一、窗口打开时定位到最后鼠标位置

用EditorWindow即可实现,核心是通过EditorPrefs持久化保存鼠标位置:

using UnityEditor;
using UnityEngine;

public class TransparentEditorWindow : EditorWindow
{
    private static Vector2 lastMousePos;

    [MenuItem("Window/Transparent Window")]
    public static void ShowWindow()
    {
        // 读取上次保存的鼠标位置,无记录则用当前鼠标位置
        lastMousePos = EditorPrefs.HasKey("LastMousePos") 
            ? JsonUtility.FromJson<Vector2>(EditorPrefs.GetString("LastMousePos")) 
            : Event.current.mousePosition;

        var window = GetWindow<TransparentEditorWindow>();
        // 将窗口左上角对齐鼠标位置,可自行调整偏移量
        window.position = new Rect(lastMousePos.x, lastMousePos.y, 300, 200);
        window.Show();
    }

    private void OnDestroy()
    {
        // 关闭窗口时保存当前鼠标位置
        EditorPrefs.SetString("LastMousePos", JsonUtility.ToJson(Event.current.mousePosition));
    }
}

二、实现窗口背景透明

EditorWindow确实没有直接的透明开关,但有两种可行方案:

方案1:GUI自定义透明背景(简单兼容)

在OnGUI中覆盖默认背景,用半透明纹理绘制:

private void OnGUI()
{
    // 清空默认窗口背景色
    GUI.backgroundColor = new Color(0, 0, 0, 0);
    // 绘制自定义半透明背景(这里是30%透明的深灰,可调整alpha值)
    GUI.DrawTexture(
        new Rect(0, 0, position.width, position.height), 
        Texture2D.whiteTexture, 
        ScaleMode.StretchToFill, 
        true, 
        0, 
        new Color(0.1f, 0.1f, 0.1f, 0.3f), 
        0, 
        0
    );

    // 此处添加你的窗口UI内容
    GUILayout.Label("半透明背景编辑器窗口");
    if (GUILayout.Button("测试按钮"))
    {
        Debug.Log("按钮触发");
    }
}

注意:此方案下窗口标题栏仍为默认样式,若需标题栏也透明,用方案2。

方案2:NativeWindow修改原生样式(进阶全透明)

通过Unity内部未公开的NativeWindow API修改窗口原生属性,实现完全透明(包括标题栏),但需注意版本兼容性:

using System.Reflection;
using UnityEditor;
using UnityEngine;

public class FullyTransparentWindow : EditorWindow
{
    private object nativeWindow;
    private MethodInfo setTransparentMethod;

    [MenuItem("Window/Fully Transparent Window")]
    public static void ShowWindow()
    {
        var window = GetWindow<FullyTransparentWindow>();
        window.Show();
    }

    private void OnEnable()
    {
        // 反射获取NativeWindow实例和透明设置方法
        var windowType = typeof(EditorWindow).Assembly.GetType("UnityEditor.NativeWindow");
        nativeWindow = typeof(EditorWindow).GetProperty("nativeWindow", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(this);
        setTransparentMethod = windowType.GetMethod("SetTransparent", BindingFlags.Instance | BindingFlags.Public);

        // 设置窗口完全透明
        setTransparentMethod.Invoke(nativeWindow, new object[] { true });
    }

    private void OnGUI()
    {
        // 直接绘制UI,背景完全透明
        GUILayout.Label("完全透明的编辑器窗口");
        if (GUILayout.Button("关闭"))
        {
            Close();
        }
    }
}

注意:该方案依赖Unity内部API,版本更新后可能失效,需在对应Unity版本中测试验证。

三、结合需求的完整示例

将位置记忆和半透明背景整合:

using UnityEditor;
using UnityEngine;

public class CustomTransparentWindow : EditorWindow
{
    private static Vector2 lastMousePos;

    [MenuItem("Window/Custom Transparent Window")]
    public static void ShowWindow()
    {
        lastMousePos = EditorPrefs.HasKey("LastMousePos") 
            ? JsonUtility.FromJson<Vector2>(EditorPrefs.GetString("LastMousePos")) 
            : Event.current.mousePosition;

        var window = GetWindow<CustomTransparentWindow>();
        window.position = new Rect(lastMousePos.x, lastMousePos.y, 300, 200);
        window.Show();
    }

    private void OnDestroy()
    {
        EditorPrefs.SetString("LastMousePos", JsonUtility.ToJson(Event.current.mousePosition));
    }

    private void OnGUI()
    {
        GUI.backgroundColor = new Color(0, 0, 0, 0);
        GUI.DrawTexture(
            new Rect(0, 0, position.width, position.height), 
            Texture2D.whiteTexture, 
            ScaleMode.StretchToFill, 
            true, 
            0, 
            new Color(0.1f, 0.1f, 0.1f, 0.3f), 
            0, 
            0
        );

        GUILayout.Space(20);
        GUILayout.Label("从上次鼠标位置启动的透明窗口");
        if (GUILayout.Button("保存位置并关闭"))
        {
            Close();
        }
    }
}

内容的提问来源于stack exchange,提问作者Akuu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 23:31:26