我使用了 Application.OpenURL
将我的用户发送到浏览器中,现在我想以编程方式将 Unity 程序切换回前台。
有没有无需插件就能实现的方法?
谢谢。
我使用了 Application.OpenURL
将我的用户发送到浏览器中,现在我想以编程方式将 Unity 程序切换回前台。
有没有无需插件就能实现的方法?
谢谢。
在将用户发送到其他窗口之前,使用 GetActiveWindow
获取窗口句柄,然后使用该句柄调用 SetForegroundWindow
。在调用 SetForegroundWindow
之前,您可以尝试模拟按下 Alt 键来打开菜单,以遵守 SetForegroundWindow 的某些限制。
private IntPtr unityWindow;
[DllImport("user32.dll")]
static extern IntPtr GetActiveWindow();
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
const int ALT = 0xA4;
const int EXTENDEDKEY = 0x1;
const int KEYUP = 0x2;
private void SendUser()
{
unityWindow = GetActiveWindow();
Application.OpenURL("http://example.com");
StartCoroutine(RefocusWindow(30f));
}
private IEnumerator RefocusWindow(float waitSeconds) {
// wait for new window to appear
yield return new WaitWhile(() => unityWindow == GetActiveWindow());
yield return new WaitForSeconds(waitSeconds);
// Simulate alt press
keybd_event((byte)ALT, 0x45, EXTENDEDKEY | 0, 0);
// Simulate alt release
keybd_event((byte)ALT, 0x45, EXTENDEDKEY | KEYUP, 0);
SetForegroundWindow(unityWindow);
}
SetForegroundWindow
函数将窗口置于前台或仅在任务栏中闪烁其条的操作存在一些限制。似乎您可以尝试发送 alt 键以打开菜单,这将允许另一个窗口(在此情况下为 Unity)控制前景。我编辑了我的答案以提供更多信息。 - Ruzihm这对我在Unity 5.0.1 / Windows 8.1上有效:
using UnityEngine;
using System;
using System.Collections;
using System.Runtime.InteropServices;
public class ForeGrounder : MonoBehaviour {
private const uint LOCK = 1;
private const uint UNLOCK = 2;
private IntPtr window;
void Start() {
LockSetForegroundWindow(LOCK);
window = GetActiveWindow();
StartCoroutine(Checker());
}
IEnumerator Checker() {
while (true) {
yield return new WaitForSeconds(1);
IntPtr newWindow = GetActiveWindow();
if (window != newWindow) {
Debug.Log("Set to foreground");
SwitchToThisWindow(window, true);
}
}
}
[DllImport("user32.dll")]
static extern IntPtr GetActiveWindow();
[DllImport("user32.dll")]
static extern bool LockSetForegroundWindow(uint uLockCode);
[DllImport("user32.dll")]
static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
}
window = GetActiveWindow();
当用户在此代码运行之前切换到另一个应用程序,即在应用程序初始化之前。那么结果就是 0
。 - xmedeko对于 Mac 独立应用程序,我们可以尝试
public void OpenURLInDefaultBrowser(string URL)
{
#如果是Mac操作系统
Screen.fullScreenMode = FullScreenMode.Windowed;
#endif Application.OpenURL(URL);
StartCoroutine(ReFocusUnity());
}
private IEnumerator ReFocusUnity()
{
//You can wait for some seconds or wait for any callback you are expecting
yield return new WaitForSeconds(5f);
#if UNITY_STANDALONE_OSX
Screen.fullScreenMode = FullScreenMode.FullScreenWindow;
#endif }
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
var prc = Process.GetProcessesByName("..."); //Get Unity's process, or
var prc = Process.GetCurrentProcess();
if (prc.Length > 0)
SetForegroundWindow(prc[0].MainWindowHandle);