• Unity
  • Services
  • Made with Unity
  • Learn
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Forums
  • Answers
  • Feedback
  • Issue Tracker
  • Blog
  • Evangelists
  • User Groups

Navigation

  • Home
  • Unity
  • Industries
  • Made with Unity
  • Learn
  • Community
    • Forums
    • Answers
    • Feedback
    • Issue Tracker
    • Blog
    • Evangelists
    • User Groups
  • Get Unity
  • Asset Store

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Jaime · Dec 09, 2010 at 12:27 PM · windowsfocus

How can I bring to front a window called by an script in unity?

Hello,

I'm trying to bring to front the unity game when it is not the active window.

I've tried implenting a dll and later importing de "user32.dll" but is not working.

The code is something like this:

using System; using System.Collections.Generic; using System.Threading; using System.Runtime.InteropServices; using System.Diagnostics; using UnityEngine; using System.Collections;

public class AccesoDLL : MonoBehaviour {

 [DllImport("SimpleDLL")] 
 private static extern byte cSum(byte b1, byte b2);
 [DllImport("SimpleDLL")] 
 private static extern int MinimizarVentana(int u);
 [DllImport("SimpleDLL")]
 private static extern int MaximizarVentana(int u);
 [DllImport("SimpleDLL")]
 private static extern int GetWindowHandler();

 [DllImport("user32.dll")]
 [return: MarshalAs (UnmanagedType.Bool)]
 private static extern bool SetForegroundWindow(int hWnd);


 public int u;

 void Awake() {

     u = GetWindowHandler();
 }

 public void Minimizar () {

     MinimizarVentana(u);
 }

 public void Maximizar () {

     MaximizarVentana(u);
 }


 public void SetForegroundWindows(){

     SetForegroundWindow(u);

 }

}

The functions Maximizar and Minimizar are working on my own dll, but when I try yo use de API functions like SetForegroundWindow not work.

Does anybody know how to do this?

Thanks!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by atti · May 05, 2015 at 08:14 AM

Here's my solution that works with Unity 5.0.1 and 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);
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image niltoid · Jun 26, 2018 at 06:09 PM 0
Share

still works on Unity 2017.4 - thanks!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Application reduced in task bar. force focus on app, always on top 0 Answers

Can't Move Around Run in Background is ON 0 Answers

stop unity pausing when it loses focus 4 Answers

Fullscreen does not pause music - Windows 7 1 Answer

Loss of texture data when Unity 2.6 looses of focus on a windows App. 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges