Search Unity

Unity Window Handle

Discussion in 'Immediate Mode GUI (IMGUI)' started by akgdeen, Dec 13, 2011.

  1. akgdeen

    akgdeen

    Joined:
    Dec 8, 2011
    Posts:
    81
    Hi,

    This may not the correct forum to post this type of request. I have searched lot but no result.

    In unity, when I use "Process.GetCurrentProcess().MainWindowHandle" returns 0 only,
    which is not returning the exact Unity Window Handle. Can anyone help me on how to get the unity window handle.

    Thanks in advance.
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    you can't get the unity window handle to my knowledge. Unity manages the whole thing
     
  3. akgdeen

    akgdeen

    Joined:
    Dec 8, 2011
    Posts:
    81
    I am developing the game which will run like as gadget. So, If I have the unity window handle, it may help me to use the handly with native API atleast.
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    why do you use an engine that you know will run in an own self managed windows when you want to run something as a gadget? makes completely no sense.

    but depending on what you want to do: embedd the offline webplayer in its IE activex form into a standalone app
     
  5. akgdeen

    akgdeen

    Joined:
    Dec 8, 2011
    Posts:
    81
    Thnaks for update,

    it is kind of data driven game which shows status information with custom 3d objects [includes translation, rotation].

    No other way other than IE activex for standalone system?
     
  6. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    well you can always use the application name -> find process on Win32 level -> get handle

    but it requires that you are on pro to call out to native code
     
  7. akgdeen

    akgdeen

    Joined:
    Dec 8, 2011
    Posts:
    81
    Well, Thanks Dreamora
     
  8. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,633
    I know this post is old, but since this comes up in searches, I thought I'd post the answer. This will get Unity's Window handle (on Windows of course), and it doesn't require Pro to work. Tested on Unity 4.5, works in editor and build.

    Code (csharp):
    1. [System.Runtime.InteropServices.DllImport("user32.dll")]
    2. private static extern System.IntPtr GetActiveWindow();
    3.  
    4. public static System.IntPtr GetWindowHandle() {
    5.   return GetActiveWindow();
    6. }
    Edit: Note that this will not work if the Unity Window is not the active Window. In the editor it will return the currently focused editor window.
     
    Last edited: Sep 19, 2018
  9. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    Yes. Natives (like Windows 32/64 API) DLLs always worked on ALL Unity version (Pro or Not). The contrary informations are an Urban Legend.


    Sorry for the resurrection.
     
  10. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,633
    Only if they're in the project's root path or system path. In 3-4.x, Unity didn't allow you to access to native DLLs in the assets folder and would throw an error telling you needed pro. Most people just got around this by moving the DLLs into the project root (outside assets) including many asset store products (baffling enough they were allowed). Unity 5 did away with this restriction since there is now only one version of Unity. That's why I felt it necessary to make it known you didn't need pro.
     
    ModLunar and ZJP like this.
  11. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    Of course...
     
  12. ruudvangaal

    ruudvangaal

    Joined:
    May 15, 2017
    Posts:
    27
    Is there a more robust solution by now? (~Unity 2018.3). I intend to use the window handle to set the window position for non-fullscreen runs. I have multiple Unity builds running at the same time, and having them all appear at the same position is annoying. And to my knowledge, the '-screen-position <x> <y>' commandline argument is still not available.
     
  13. dhaminitinCbre

    dhaminitinCbre

    Joined:
    Apr 24, 2019
    Posts:
    6
    Hi has anyone still managed to find the right answer for this? I am also stuck in the same situation.
     
  14. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,633
    This is exactly what I posted above in message #8.
     
  15. ntomlight

    ntomlight

    Joined:
    Sep 25, 2017
    Posts:
    1
    Some Magic Here is !!!
     
  16. Fiard

    Fiard

    Joined:
    May 15, 2013
    Posts:
    4
    Once more I'm giving this issue a resurrection.
    We have a bug with this solution already, because there is a chance, that Active or Foreground window has changed right before you try to access it. And we move other windows via Win32 API occasionally.
     
    l_racher likes this.
  17. EirikWahl

    EirikWahl

    Joined:
    Apr 7, 2018
    Posts:
    21