LoginSignup
1
1

More than 3 years have passed since last update.

【Unity】exeのタイトルバーを非表示にする

Last updated at Posted at 2019-07-18

タイトルバー消したい。
無題.png

① WindowController.cs

シーン上の好きなオブジェクトにアタッチします。

using UnityEngine;
using System.Collections;

using System;
using System.Runtime.InteropServices;

namespace Utils {

    public class WindowController : MonoBehaviour {

        private string windowName = "windowname";
        private int x = 0;
        private int y = 0;
        private int width = 1920;
        private int height = 1080;
        private bool hideTitleBar = true;

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

        // Sets window attributes
        [DllImport("user32.dll")]
        public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

        // Gets window attributes
        [DllImport("user32.dll")]
        public static extern int GetWindowLong(IntPtr hWnd, int nIndex);

        // assorted constants needed
        public static int GWL_STYLE = -16;
        public static int WS_CHILD = 0x40000000; //child window
        public static int WS_BORDER = 0x00800000; //window with border
        public static int WS_DLGFRAME = 0x00400000; //window with double border but no title
        public static int WS_CAPTION = WS_BORDER | WS_DLGFRAME; //window with a title bar

        void Awake () {
            var window = FindWindow(null, windowName);
            if(hideTitleBar) {
                int style = GetWindowLong(window, GWL_STYLE);
                SetWindowLong(window, GWL_STYLE, (style & ~WS_CAPTION));
            }
            SetWindowPos(window, 0, x, y, width, height, width * height == 0 ? 1 : 0);
        }
    }
}

private string windowName = "windowname";・・・windowのタイトル名
private int x = 0;・・・ウィンドウの左上端座標x
private int y = 0;・・・ウィンドウの左上端座標y
private int width = 1920;・・・ウィンドウの幅
private int height = 1080;・・・ウィンドウの高さ
private bool hideTitleBar = true;・・・タイトルバーを隠すか

② user32.dll

Assets/Plugins下にそのまま置いてください。

extern "C" __declspec(dllexport) int Add(int a, int b)
{
     return(a + b);
}

※コード内容に深い意味はありませんので、もっといいコードがあればどなたか。

③ Player Settings

Player Settings > Player > Resolution and Presentation > Resolution
Fullscreen Mode を Windowedにする。
Resizable Window チェックを外す。
無題.png

コード引用元

Is there a way to set the position of a standalone unity window?
Removing the Title bar of external application using c#

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1