1、效果
2、代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class CustomTitleBar : MonoBehaviour
{
//获取指定unity.exe窗口
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
public static IntPtr ParenthWnd = FindWindow(null, "Test"); //build时候的项目名,playerSetting中设置的
//设置窗口边框
[DllImport("user32.dll")]
public static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);
//设置窗口位置,尺寸
[DllImport("user32.dll")]
public static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
//窗口拖动
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
//设置当前窗口的显示状态
[DllImport("user32.dll")]
public static extern bool ShowWindow(System.IntPtr hwnd, int nCmdShow);
//边框参数
private const uint SWP_SHOWWINDOW = 0x0040;
private const int GWL_STYLE = -16;
//隐藏标题栏图标
private const int WS_POPUP = 0x800000;
private const int SW_SHOWMAXIMIZED = 3;//最大化窗口
private const int SW_SHOWMINIMIZED = 2;//(最小化窗口)
public Button minButton;
public Button restoreButton;
public Button clossButton;
public Transform titleBG;
// Start is called before the first frame update
void Start()
{
Window();
minButton.onClick.AddListener(MinButtonList);
restoreButton.onClick.AddListener(RestoreButtonList);
clossButton.onClick.AddListener(ClossButtonList);
AddInterFaceEvent(titleBG, EventTriggerType.Drag, TitleBarList);
}
public EventTrigger.Entry AddInterFaceEvent(Transform trans, EventTriggerType eventType, UnityAction<BaseEventData> action)
{
EventTrigger trigger = trans.GetComponent<EventTrigger>();//transform.GetChild(0)
if (trigger == null)
trigger = trans.gameObject.AddComponent<EventTrigger>();//transform.GetChild(0)
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = eventType;
entry.callback = new EventTrigger.TriggerEvent();
entry.callback.AddListener(action);
trigger.triggers.Add(entry);
return entry;
}
private void TitleBarList(BaseEventData arg0)
{
isWindow = true;
ReleaseCapture();
SendMessage(ParenthWnd, 0xA1, 0x02, 0);
SendMessage(ParenthWnd, 0x0202, 0, 0);
}
//关闭
public void ClossButtonList()
{
Application.Quit();
}
//全屏
public static void FullScreen()
{
isWindow = false;
ShowWindow(ParenthWnd, SW_SHOWMAXIMIZED);
}
static bool isWindow;
//窗口
public static void Window()
{
isWindow = true;
//去除上边栏(不可拖拽缩放)
SetWindowLong(ParenthWnd, GWL_STYLE, WS_POPUP);
SetWindowPos(ParenthWnd, 0, 320, 220, 1280, 720, SWP_SHOWWINDOW);
}
//恢复
public static void RestoreButtonList()
{
if (isWindow)
{
FullScreen();
}
else
{
Window();
}
}
//最小
public void MinButtonList()
{
ShowWindow(ParenthWnd, SW_SHOWMINIMIZED);
}
}
3、注意事项
1、这个名字是打包的名字名字
2、要选择
4、工程地址
链接:https://pan.baidu.com/s/1f5GHFqUnHc-hwO-DbeQkDA?pwd=8888
提取码:8888
--来自百度网盘超级会员V6的分享