Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
6
Question by user-1132 (google) · Sep 16, 2010 at 11:54 AM · windowmaximizeminimize

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

Comment
Add comment · Show 1
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 cregox · Dec 08, 2011 at 07:16 PM 0
Share

also unanswered (no good ones at least): http://answers.unity3d.com/questions/25136/can-i-do-this-in-unity-make-maximise-button.html

3 Replies

· Add your reply
  • Sort: 
avatar image
8

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);
 }


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 dreamriver · Sep 21, 2017 at 05:27 PM 2
Share

Perfect! Also don't forget to include using System.Runtime.InteropServices; using UnityEngine;

avatar image masoudarvishian · Jan 03, 2019 at 05:31 PM 0
Share

Worked for me. Thank you

avatar image
4

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);


Comment
Add comment · Show 6 · 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 Sabretoothy · Jun 05, 2013 at 10:58 AM 1
Share

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.

avatar image green_core · Jun 05, 2013 at 12:42 PM 0
Share

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.

avatar image Sabretoothy · Jun 05, 2013 at 03:05 PM 0
Share

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.

avatar image thilina098 · Jul 04, 2013 at 03:41 PM 0
Share

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.

avatar image Sabretoothy · Jul 05, 2013 at 08:31 AM 0
Share

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.

Show more comments
avatar image
1

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.

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 cj_coimbra · Dec 08, 2011 at 07:52 PM 0
Share

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

avatar image Fragmental · Oct 02, 2018 at 11:03 PM 0
Share

script reference moved here https://docs.unity3d.com/ScriptReference/Screen.SetResolution.html

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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