Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • 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
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

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
3

Answer by tteneder · 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 2 · 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!

avatar image Magasenakwa · Sep 01, 2020 at 11:49 PM 0
Share

I was using SetForegroundWindow and SetActiveWindow as this seems to be what everyone is recommending lately. This didn't work, though. The best I got was the icon flashing in the taskbar but the window never actually came to the foreground.

.

Using SwitchToThisWindow worked perfectly in 2019.4! Thank you... 5 years late, but thank you! :D

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.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Is there a way to make a pc build that will not lose focus when selecting another app ? 1 Answer

stop unity pausing when it loses focus 4 Answers

Fullscreen does not pause music - Windows 7 1 Answer

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

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges