Unity窗口显示在最前面

39 篇文章 1 订阅

基本思路

1、得到Unity程序窗口的句柄

2、判断Unity程序窗口是否在最前面,如果不是,则将其显示在最前面

代码实现

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.UI;

public class Test1 : MonoBehaviour
{
    [DllImport("User32.dll")]
    private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    [DllImport("user32.dll")]
    private static extern IntPtr GetForegroundWindow();

    [DllImport("User32.dll")]
    private static extern bool SetForegroundWindow(IntPtr hWnd);

    [DllImport("User32.dll")]
    private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);

    void FixedUpdate()
    {
        // apptitle自己到查看进程得到,一般就是程序名不带.exe
        // 或者用spy++查看
        IntPtr hwnd = FindWindow(null, "apptitle");

        // 如果没有找到,则不做任何操作(找不到一般就是apptitle错了)
        if (hwnd == IntPtr.Zero)
        {
            return;
        }

        IntPtr activeWndHwnd = GetForegroundWindow();

        // 当前程序不是活动窗口,则设置为活动窗口
        if (hwnd != activeWndHwnd)
        {
            ShowWindowAsync(hwnd, 3);
            SetForegroundWindow(hwnd);
        }
    }
}

转载自:https://blog.csdn.net/jxyyl/article/details/66474649

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值