- Home /
Is there a way to set the position of a standalone unity window?
I run a dual monitor setup (Win7), and normally when I open an application it will open in the correct place. With unity games that I create, they open directly in the middle of the two monitors, and I immediately have to drag the window to the left. Is there a way that I can set the window position of unity windows?
Answer by cr4y · Oct 01, 2013 at 07:30 AM
Yes it's possible (on standalone Windows only). It require using external dll. You just write:
#if UNITY_STANDALONE_WIN || UNITY_EDITOR
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
private static extern bool SetWindowPos(IntPtr hwnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(System.String className, System.String windowName);
public static void SetPosition(int x, int y, int resX = 0, int resY = 0) {
SetWindowPos(FindWindow(null, "My Unity Window Title"), 0, x, y, resX, resY, resX * resY == 0 ? 1 : 0);
}
#endif
Getting window handle by window name is definitely not best way, but the easiest one.
Full working example :
using UnityEngine;
using System;
using System.Collections;
using System.Runtime.InteropServices;
public class PlaceWindow : $$anonymous$$onoBehaviour {
#if UNITY_STANDALONE_WIN || UNITY_EDITOR
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
private static extern bool SetWindowPos(IntPtr hwnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(System.String className, System.String windowName);
public static void SetPosition(int x, int y, int resX = 0, int resY = 0) {
SetWindowPos(FindWindow(null, "$$anonymous$$y window title"), 0, x, y, resX, resY, resX * resY == 0 ? 1 : 0);
}
#endif
// Use this for initialization
void Awake () {
SetPosition(0,0);
}
}
Create an empty game object, add the PlaceWindow component to it, and that is all ! Do not forget to rename the $$anonymous$$y Window Title string in the one you have.
Thanks a lot !
Thanks gabrielstu, that worked great with window but it doesn't work with full screen. How to fix it?
This is great - especially when combined with the popwindow command line argument to create a chromeless window: http://docs.unity3d.com/$$anonymous$$anual/CommandLineArguments.html
Note that you can sometimes detect the name of your window title:
System.Diagnostics.Process proc = System.Diagnostics.Process.GetCurrentProcess();
string procName = proc.ProcessName;
hWnd = FindWindow(null, procName);
Answer by andeeee · Mar 24, 2010 at 03:03 PM
There is no way to set the position of the window currently.
Well, it's an answer. Thanks -- kind of stinks, but so it goes I guess.
Your answer
Follow this Question
Related Questions
Maximizing Unity Freeze 0 Answers
Why does my GUI.Window dragging behaviour act erratically on a rotated window? 1 Answer
Replicate Timeline in Animation window 1 Answer
HOW TO: Sliding GUI Window? 1 Answer
Pop up window 0 Answers