【Unity】改变游戏运行时Window的窗口标题

【Unity】改变游戏运行时Window的窗口标题

零、需求

Unity打包好的Windows程序,启动后如何更改窗口标题?因为看着英文的感觉不太好,故有此想法。什么?你说为啥不改项目产品名?产品名会被写到文件夹名中,感觉后面可能会有问题......

壹、解决方案

代码如下:

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;

public class ChangeWindowTitle : MonoBehaviour
{
    /// <summary>
    /// 记录当前的窗口标题
    /// </summary>
    public static string TitleText = "";
    /// <summary>
    /// 窗口句柄,只能获取一次,后面获取的无效
    /// </summary>
    private static System.IntPtr windowHandle;
    /// <summary>
    /// 是否已经获得窗口句柄
    /// </summary>
    private static bool isGotWindowHandle = false;

    [Header("Windows窗口标题设置")]
    [Tooltip("窗口标题文字")]
    public string title = "窗口";
    [Tooltip("是否自动设置窗口标题文字")]
    public bool autoSet = true;


    [SerializeField]
    [Header("标题设置状态")]
    [Tooltip("标题是否已经设置过至少一次")]
    private bool isSet = false;

#if UNITY_STANDALONE_WIN
    [DllImport("user32.dll")]
    private static extern System.IntPtr FindWindow(string className, string windowName);
    [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern bool SetWindowText(System.IntPtr hwnd, string title);
#endif


    void Update()
    {
        if (autoSet && !isSet)
        {
            setTitle(title);
        }
    }

    public void setTitle(string title)
    {
        isSet = true;
#if UNITY_STANDALONE_WIN
        TitleText = title;
        if(!isGotWindowHandle)
        {
            // 获取窗口句柄
            windowHandle = FindWindow(null, Application.productName);
            isGotWindowHandle = true;
        }
        // 设置新标题
        SetWindowText(windowHandle, title);
#endif
    }
}

要注意的都写在注释里了(应该)。把它挂到对象上,直接调用public void setTitle(string title)来设置标题就好啦,要是不想写代码,也可以在面板里设置,默认会帮你设置一次标题窗口的~

好,以上就是全部内容,点个赞再走呗?不会的评论区见~ (◍•ᴗ•◍)❤

相关推荐
xiezhr1 天前
米哈游36岁程序员被曝复工当晚猝死出租屋内
游戏·程序员·游戏开发
爱搞虚幻的阿恺5 天前
Niagara粒子系统-超炫酷的闪电特效(加餐 纸牌螺旋上升效果)
游戏·游戏引擎
_Li.5 天前
Simulink - 6DOF (Euler Angles)
人工智能·算法·机器学习·游戏引擎·cocos2d
智算菩萨5 天前
儿童游乐空间的双维建构:室内淘气堡与室外亲子乐园的发展学理、功能分野与协同育人机制研究
游戏·游戏策划
weixin_424294675 天前
Unity 调用Steamworks API 的 SteamUserStats.RequestCurrentStats()报错
unity·游戏引擎·steamwork
HoFunGames5 天前
Unity小地图,Easy Minimap System MT-GPS插件
unity·游戏引擎
marteker5 天前
房地产市场平台Zillow与《魔兽世界》合作展示游戏内房屋
游戏
wy3258643645 天前
Unity 新输入系统InputSystem(基本操作)
unity·c#·游戏引擎
WarPigs5 天前
着色器multi_compile笔记
unity·着色器
wanhengidc5 天前
云手机 打造云端算力
运维·服务器·网络·游戏·智能手机