Maximize Unity Game in Windows

From Unify Community Wiki
Jump to: navigation, search

The following solution embeds a Unity web player in a window that looks like a standard Microsoft Windows window (this is a solution for Microsoft Windows). You can maximize, minimize, and dynamically scale the window using the window's borders. In addition the last position, size and state (maximized/not-maximized) is saved and restored. This code also will ensure the window is not lost on a secondary monitor that is disconnected, and will only restore the last windows size/position/state if it is visible in the current monitor set up.

Since this is using Unity web player .unity3d files, this means that access to the hard drive is limited, plugins will not work, and the user will need to have the web player installed.

The following .zip file contains a complete example including a Unity example .unity3d / .html file, the C# source (Visual Studio 2005), which you will need in order to set the window title (and icon) of your game, as well as to change the registry key where the window settings are stored.

media:UnityFrame.zip

The C# project loads an .html file that is set to maximize the .unity3d file to the web browser, which is maximized to the C# main window, which results in the maximizable and resizable Unity game window. If you want to change the name of the .unity3d file you will need to edit the .html file. If you want to change the name of the .html file you will need to change the file loaded in the C# project.

Make sure that in the final project the executable from the C# project, the .html file, and the .unity3d file are all in the same directory. You could also specify a remote file in the C# project if you wanted to make a remote http:// file look and act more like local content.

To test this just download the UnityFrame.zip file linked above, unzip it, and run the UnityFrame/UnityFrame.exe file.

If you have any questions you can post to the associated thread: http://forum.unity3d.com/viewtopic.php?t=20513

You will notice that when you scale your game, any gui elements will stay in their current location unless you specify otherwise. Since I store my gui rects elsewhere, I need to check for resolution changes and dynamically update my gui, you may need to do the same. I do this like this:

Unity Code:

void OnGUI () 
{ 
  CheckIfResolutionChanged(); 
  if (!guiSetup) 
  { 
    guiSetup = true; 
    SetupGui(); 
  } 
  ... 
} 
 
private void CheckIfResolutionChanged() { 
  if (screenWidth != Screen.width  || screenHeight != Screen.height) { 
    screenWidth = Screen.width; 
    screenHeight = Screen.height; 
    guiSetup = false; 
  } 
} 
 
private void SetupGui() { 
  // Calculate any gui stuff that is not calculated every frame, that is based on the window size
}</csharp >
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Tools