Minimizing and maximizing by script

In a standalone player, is it possible to force minimizing the screen by scripting?

What I am trying to do is minimizing the screen when some event happens, and then maximising the screen whe I read a value from a text file.

Maybe i have to write a plugin, in that case how does it work?


more ▼

asked Sep 16 '10 at 11:54 AM

user-1132 (google) gravatar image

user-1132 (google)
26 2 2 3

Dec 08 '11 at 07:16 PM Cawas
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

You can do anything with window by using WinAPI. But you have to get window handle to do it. In usual .NET application you can get them by this way:

var handle Process.GetCurrentProcess().MainWindowHandle

But it doesn't work in unity. So, i use another way to get the handle. I enumerate all windows by function EnumWindowsProc and i get process id of each window by GetWindowThreadProcessId. When i find window with pid, that equals to pid of unity i can manipulate by unity window. Resize, move, minimize, etc.

To minimize window you can use ShowWindow function.

To use WinAPI functions you have to import them:

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int GetWindowThreadProcessId(HandleRef handle, out int processId);

private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool EnumWindows(EnumWindowsProc callback, IntPtr extraData);
more ▼

answered Dec 08 '11 at 08:05 PM

green_core gravatar image

106 3

Hi green_core. I have tried your method and when I find the matching process id to Process.GetCurrentProcess().Id the hWnd does not seem to be the window. Is there something I am doing wrong here.

Jun 05 '13 at 10:58 AM Sabretoothy

Your callback, which is EnumWindowsProc, is called for each window. As a parameter of function you receive hWnd, which is a handle of window. You should get process id of this window by GetWindowThreadProcessId function. To put hWnd into the function you should wrap it into HandleRef. You can do it like this: var obj = new object(); var hRef = new HandleRef(obj, hWnd); So. After this put it into GetWindowThreadProcessId and compare result with your ProcessId. If they equal - bingo! :) You've just found your window. Then return false to interrupt the enumeration of windows. If they don't equal, you should return true to continue enumeration.

I hope it would be helpful for you.

Jun 05 '13 at 12:42 PM green_core

That sorted it thank you. I was missing the step of returning false to interup the enumeration after I had found the matching Process ID.

Jun 05 '13 at 03:05 PM Sabretoothy

Hi green_core OR Sabretoothy,

Could you post a sample code how to minimize and maximize the Unity window using EnumWindowsProc and GetWindowThreadProcessId functions ?

Thanks in advance.

Jul 04 '13 at 03:41 PM thilina098

You would need to add in the functions green_core linked above and then do something like the following:

bool bUnityHandleSet = false;
HandleRef unityWindowHandle;

public bool EnumWindowsCallBack(IntPtr hWnd, IntPtr lParam)
    int procid;
    int returnVal = GetWindowThreadProcessId (new HandleRef(this, hWnd), out procid);

    int currentPID = System.Diagnostics.Process.GetCurrentProcess().Id;

    HandleRef handle = new HandleRef(this, System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle);

    if(procid == currentPID)
       unityWindowHandle = new HandleRef(this, hWnd);
       bUnityHandleSet = true;
       return false;

    return true;

That is a possible delegate you would pass in to the EnumWindows function. So EnumWindows(EnumWindowsCallBack, IntPtr.Zero); would go in something like awake and then ShowWindow(unityWindowHandle.Handle, SW_MAXIMIZE); or some other function that will maximise the window would need to be called at some point after.

Best place I found for info on all the winAPI functions was pinvoke.net

Hope this helps.

Jul 05 '13 at 08:31 AM Sabretoothy
(comments are locked)
10|3000 characters needed characters left


Switch that boolean argument to True for fullscreen and False for windowed.

more ▼

answered Dec 08 '11 at 07:46 PM

cj_coimbra gravatar image

461 14 16 17

And the width and height arguments to resize the window of course...

Dec 08 '11 at 07:52 PM cj_coimbra
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Sep 16 '10 at 11:54 AM

Seen: 4146 times

Last Updated: Jul 05 '13 at 09:53 AM