简体   繁体   English

有什么方法可以将 Unity3d 带到前台?

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

I have sent my user out to the browser with Application.OpenURL .我已使用Application.OpenURL将我的用户发送到浏览器。 And now I want to programatically bring unity back to the foreground.现在我想以编程方式将统一带回前台。

Is there any way to do it without a plugin?没有插件有什么办法吗?

Thanks.谢谢。

Use GetActiveWindow to get the window's handle before you send the user away, then use SetForegroundWindow using that handle. 在发送用户之前使用GetActiveWindow获取窗口的句柄,然后使用该句柄使用SetForegroundWindow Before you use SetForegroundWindow , you can try simulating an Alt keypress to bring up a menu to abide by certain limitations of 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);
}

if you are using Unity3D in Windows, try below code after calling Application.OpenURL(...) : 如果您在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);

This worked for me on Unity 5.0.1 / Windows 8.1: 这在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);
}

For Mac Standalone we can try对于 Mac Standalone,我们可以尝试

public void OpenURLInDefaultBrowser(string URL)
    {

#if UNITY_STANDALONE_OSX #if UNITY_STANDALONE_OSX

Screen.fullScreenMode = FullScreenMode.Windowed; Screen.fullScreenMode = FullScreenMode.Windowed;

#endif Application.OpenURL(URL); #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 #if UNITY_STANDALONE_OSX

        Screen.fullScreenMode = FullScreenMode.FullScreenWindow;

#endif } #万一 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM