原地址:http://www.imxqy.com/cg/unity/unity-icon.html
unity是没有提供windows标题修改和窗口图标更换的接口,所以要自己实现。
另外没有Icon这个类型,所以直接使用了API来进行对图标的操作。
这个类要挂在一个物体上,Awake进行初始化后才能使用。
先枚举寻找窗口返回句柄,然后通过SetWindowText设置窗口标题,
ExtractIcon来创建图标获取图标句柄,然后使用SendMessage来向窗口发送WM_SETICON (0x80)消息。
using System;
using System.Runtime.InteropServices;
using UnityEngine;
public class ProgramWindow : MonoBehaviour
{
public static ProgramWindow Instance;
#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);