簡體   English   中英

有什么方法可以將 Unity3d 帶到前台?

[英]Any way to bring Unity3d to the foreground?

我已使用Application.OpenURL將我的用戶發送到瀏覽器。 現在我想以編程方式將統一帶回前台。

沒有插件有什么辦法嗎?

謝謝。

在發送用戶之前使用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);
}

如果您在Windows中使用Unity3D,請在調用Application.OpenURL(...)后嘗試以下代碼:

[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);

這在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);
}

對於 Mac Standalone,我們可以嘗試

public void OpenURLInDefaultBrowser(string URL)
    {

#if UNITY_STANDALONE_OSX

Screen.fullScreenMode = FullScreenMode.Windowed;

#endif 應用程序.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;

#萬一 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM