- Home /
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?
Thanks
also unanswered (no good ones at least): http://answers.unity3d.com/questions/25136/can-i-do-this-in-unity-make-maximise-button.html
Answer by alkohol · Aug 14, 2017 at 08:15 PM
Similar as proposed @green_core but a little bit easier way to minimize is described here
In the end you will have something like this:
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern IntPtr GetActiveWindow();
public void OnMinimizeButtonClick()
{
ShowWindow(GetActiveWindow(), 2);
}
Perfect! Also don't forget to include using System.Runtime.InteropServices; using UnityEngine;
Answer by green_core · Dec 08, 2011 at 08:05 PM
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);
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.
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.
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.
Hi green_core OR Sabretoothy,
Could you post a sample code how to $$anonymous$$imize and maximize the Unity window using EnumWindowsProc and GetWindowThreadProcessId functions ?
Thanks in advance.
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().$$anonymous$$ainWindowHandle);
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_$$anonymous$$AXI$$anonymous$$IZE);
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.
Answer by cj_coimbra · Dec 08, 2011 at 07:46 PM
http://unity3d.com/support/documentation/ScriptReference/Screen.SetResolution.html
Switch that boolean argument to True for fullscreen and False for windowed.
And the width and height arguments to resize the window of course...
script reference moved here https://docs.unity3d.com/ScriptReference/Screen.SetResolution.html
Your answer
Follow this Question
Related Questions
Customize Game Window (For Exported Project) 1 Answer
Windows maximized 3 Answers
Android: How can I Minimize/Maximize Apps? 0 Answers
Keeping MonoDevelop from going fullscreen when opening a file 0 Answers
Minimize And Maximize An application 1 Answer