Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Setting player window position?

Discussion in 'Scripting' started by CoolJosh3k, Jun 5, 2018.

  1. CoolJosh3k

    CoolJosh3k

    Joined:
    Dec 3, 2016
    Posts:
    145
    I cannot seem to find a way to position the player window where I want it.

    I am able to get the resolution of the users primary monitor, but when I switch to windowed mode by
    Screen.SetResolution(width, height, false);

    it just plops it down in the top-left of the primary monitor.

    How does one control the window placement? Even if I cannot give it an specific location, I want to be able to place it in the center.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    How much have you messed with setting UI anchors? Generally you place anchors where you want your UI elements to appear in relation to those anchors. For example, if you placed all the anchors in the middle of the screen and then centered the window relative to those anchors, the window will appear in the middle of the screen no matter what resolution you set.

    https://docs.unity3d.com/Manual/UIBasicLayout.html
     
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I thought the OP was asking about the actual operating system window.

    I think you'd have to do that with an operating system API, if so?
     
    Bunny83 likes this.
  4. CoolJosh3k

    CoolJosh3k

    Joined:
    Dec 3, 2016
    Posts:
    145
    Yes, I do mean the actual window the game is displayed in, not what is rendered.

    I am presuming this is a feature lacking in Unity (2017.4.3f1), but I figure there must be some way to do it.
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Pretty sure you have to use some native code for that.
     
  6. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
  7. CoolJosh3k

    CoolJosh3k

    Joined:
    Dec 3, 2016
    Posts:
    145
    Thanks for the help. Based on that code snippet in the link, I was able to modify it to my needs and get it to work.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. #if UNITY_STANDALONE_WIN
    7.  
    8. using System;
    9. using System.Runtime.InteropServices;
    10.  
    11. #endif
    12.  
    13. public class SettingsManager : MonoBehaviour {
    14.  
    15.     public bool fullScreen = false;
    16.     private static string fullScreenKey = "Full Screen";
    17.     public int minDisplaySize = 384;
    18.     private static int monitorWidth;
    19.     private static int monitorHeight;
    20.    
    21.     #if UNITY_STANDALONE_WIN
    22.    
    23.     [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
    24.     private static extern bool SetWindowPos(IntPtr hwnd, int hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
    25.    
    26.     [DllImport("user32.dll", EntryPoint = "FindWindow")]
    27.     public static extern IntPtr FindWindow(string className, string windowName);
    28.    
    29.     public static IEnumerator SetWindowPosition(int x, int y) {
    30.         yield return new WaitForEndOfFrame();
    31.         yield return new WaitForEndOfFrame();
    32.         SetWindowPos(FindWindow(null, Application.productName), 0, x, y, 0, 0, 5);
    33.     }
    34.    
    35.     #endif
    36.    
    37.     void Start() {
    38.         monitorWidth = Screen.resolutions[Screen.resolutions.Length - 1].width;
    39.         monitorHeight = Screen.resolutions[Screen.resolutions.Length - 1].height;
    40.         CheckAndSet();
    41.     }
    42.    
    43.     private void CheckAndSet() {
    44.         if (PlayerPrefs.GetInt(fullScreenKey, 0) >= 1) {
    45.             SetFullScreen();
    46.         } else {
    47.             SetWindowed();
    48.         }
    49.     }
    50.    
    51.     public void SetFullScreen() {
    52.         Screen.SetResolution(monitorWidth, monitorHeight, true);
    53.         fullScreen = true;
    54.         PlayerPrefs.SetInt(fullScreenKey, 1);
    55.         PlayerPrefs.Save();
    56.     }
    57.    
    58.     public void SetWindowed() {
    59.         SetWindowResolution();
    60.         fullScreen = false;
    61.         PlayerPrefs.SetInt(fullScreenKey, 0);
    62.         PlayerPrefs.Save();
    63.     }
    64.    
    65.     private void SetWindowResolution() {
    66.         int multiplier = 1;
    67.         if (monitorWidth >= monitorHeight) {
    68.             multiplier = monitorHeight / minDisplaySize;
    69.             if ((monitorHeight % minDisplaySize) == 0) {
    70.                 multiplier--;
    71.             }
    72.         } else {
    73.             multiplier = monitorWidth / minDisplaySize;
    74.             if ((monitorWidth % minDisplaySize) == 0) {
    75.                 multiplier--;
    76.             }
    77.         }
    78.         int size = minDisplaySize * multiplier;
    79.         if (size < minDisplaySize) {
    80.             size = minDisplaySize;
    81.         }
    82.         Screen.SetResolution(size, size, false);
    83.        
    84.         #if UNITY_STANDALONE_WIN
    85.        
    86.         int x = monitorWidth / 2;
    87.         x -= size / 2;
    88.         int y = monitorHeight / 2;
    89.         y -= size / 2;
    90.         StartCoroutine(SetWindowPosition(x, y));
    91.        
    92.         #endif
    93.     }
    94. }
    95.  
    My contribution here should allow easy swapping to a pixel perfect windowed mode. If you don't want a 1:1 ratio, that can easily be changed.
     
    ysnrik, peterparnes, wlwl2 and 2 others like this.
  8. ksam2

    ksam2

    Joined:
    Apr 28, 2012
    Posts:
    1,069
  9. Baene

    Baene

    Joined:
    Jun 1, 2014
    Posts:
    6
    Is there any way to do this yet across the desktop platforms?
     
  10. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,051