Unityのゲーム(Windowsスタンドアロン)で、ウィンドウのタイトルを変更する方法を書いていきます。
変更方法
ウィンドウのタイトルを変更する方法は2つあります。
以下に詳細を記載していきます。
1. プロダクト名を変更する方法
Windowsスタンドアロンの場合、ウィンドウのタイトルは「プロダクト名(EXE名)」になります。プロダクト名を変更する方法は、下のリンク先に記載しています。
UnityでEXEの名前を変更する方法(プロダクト名の設定方法)
2. C#スクリプトで変更する方法
以下のコードを作成して、GameObject にアタッチします。
using UnityEngine; using System.Runtime.InteropServices; public class Title : MonoBehaviour { public string txt; // 条件付きコンパイル(Windowsスタンドアロンのみ有効) #if UNITY_STANDALONE_WIN [DllImport("user32.dll", EntryPoint="FindWindow", CharSet=CharSet.Unicode)] static extern System.IntPtr FindWindow(string className, string windowName); [DllImport("user32.dll", EntryPoint="SetWindowText", CharSet=CharSet.Unicode)] static extern bool SetWindowText(System.IntPtr hwnd, string txt); void Start() { System.IntPtr hwnd = FindWindow(null, Application.productName); SetWindowText(hwnd, txt); } #endif }
それから、インスペクターでウィンドウのタイトル名(プロパティtxt
)を設定します。
2.1. C#スクリプトの注意点
コード内の FindWindow関数は、Application.productName(ゲームのプロダクト名)と同じ名前のウィンドウを取得します。
System.IntPtr hwnd = FindWindow(null, Application.productName); SetWindowText(hwnd, txt);
プロダクト名(EXE名)と同じ名前のウィンドウが存在する場合は、そちらを取得してタイトルを変更する可能性があります。
2.2. 日本語のタイトル名について
日本語のタイトル名を設定する場合、上のコードのように SetWindowText関数の DllImportで、CharSet=CharSet.Unicode
を指定する必要があります。
変更後イメージ
下の画像のように、アイコン横のタイトルが変更されます。