首页 > 手游开发 > Unity3D > Unity3D 如何修改Windows应用程序窗口的标题
2019
10-04

Unity3D 如何修改Windows应用程序窗口的标题

Unity3D默认情况下会将产品名称作为Windows应用程序窗口的标题。如果将产品名称设置为中文时,导出的应用程序和资源目录的名称都将是中文,这并不是特别的合适。所在为了能够实现目录和文件名称为英文或拼音,而应用程序窗口的标题为中文,我们得利用另外的方法来实现。

将以下代码保存一个SetWindowText.cs的文件中,然后绑定到场景中的某个对象即可

using UnityEngine;
using System;
using System.Runtime.InteropServices;
public class SetWindowText : MonoBehaviour
{
    #region WIN32API
    delegate bool EnumWindowsCallBack(IntPtr hwnd, IntPtr lParam);
    [DllImport("user32", CharSet = CharSet.Unicode)]
    static extern bool SetWindowTextW(IntPtr hwnd, string title);
    [DllImport("user32")]
    static extern int EnumWindows(EnumWindowsCallBack lpEnumFunc, IntPtr lParam);
    [DllImport("user32")]
    static extern uint GetWindowThreadProcessId(IntPtr hWnd, ref IntPtr lpdwProcessId);
    #endregion
    IntPtr myWindowHandle;
    public void Start()
    {
        IntPtr handle = (IntPtr)System.Diagnostics.Process.GetCurrentProcess().Id;  //获取进程ID
        EnumWindows(new EnumWindowsCallBack(EnumWindCallback), handle);     //枚举查找本窗口
        SetWindowTextW(myWindowHandle, "测试代码"); //设置窗口标题
    }

    bool EnumWindCallback(IntPtr hwnd, IntPtr lParam)
    {
        IntPtr pid = IntPtr.Zero;
        GetWindowThreadProcessId(hwnd, ref pid);
        if (pid == lParam)  //判断当前窗口是否属于本进程
        {
            myWindowHandle = hwnd;
            return false;
        }
        return true;
    }
}

 

最后编辑:
作者:游戏创作者大陆

留下一个回复

你的email不会被公开。