【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)来设置标题就好啦,要是不想写代码,也可以在面板里设置,默认会帮你设置一次标题窗口的~

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

相关推荐
zyh______20 小时前
C#语法糖(按照实用性排序)
unity·c#
avi91111 天前
[AI教做人]CinemachineCamera 各项调整配置参数入门 + Profiler
unity·游戏开发·camera·cinemachine·visual camera·镜头配置
郝学胜-神的一滴2 天前
[简化版 GAMES 101] 计算机图形学 16:纹理走样、Mipmap、三线性插值、各向异性过滤原理全解
unity·游戏引擎·godot·图形渲染·three.js·opengl·unreal
Rauser Mack2 天前
Vibe coding游戏实战:零代码编程五子棋小游戏
人工智能·python·游戏·html·prompt
想做后端的前端2 天前
游戏里的水面是怎么做的
游戏
leoZ2313 天前
Claude 全面解析:从基础原理到实战应用指南
人工智能·游戏
2501_943782353 天前
【共创季稿事节】猜数字游戏:二分法思维与交互式反馈
前端·游戏·microsoft·harmonyos·鸿蒙·鸿蒙系统
2301_767113983 天前
Superpowers 游戏引擎从零开发实战指南
游戏引擎
吴梓穆3 天前
untiy TextMeshPro (TMP) text 操作 中文字符集 字体材质操作(添加纹理 添加描边 添加阴影)
unity
星空露珠3 天前
迷你世界UGc3.0脚本Wiki[剧情动画模块管理接口 Timeline]
开发语言·数据结构·算法·游戏·lua