基本思路
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