Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Unity Window Handle

Discussion in 'Extensions & OnGUI' 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,600
    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,600
    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,600
    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:
    3,417
    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
    vexe likes this.
  9. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,646
    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:
    3,417
    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.
     
    ZJP likes this.
  11. ZJP

    ZJP

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

    ruudvangaal

    Joined:
    May 15, 2017
    Posts:
    6
    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.