Doris232 发表于 2022-4-19 16:18

[Unity 3d] 使用 Unity 开发无边框、支持拖拽、缩放、最小化的应用


老哥要我开发一个 ERP ,七弯八转最后终究是用 excel 给他整了一个。哥哥贼拉开心,还给了我 500 零花钱,美滋滋。所以徒留这个还没开工就宣告毕业的项目给大家参考。
前言:

在 Unity 开发中,如果想要在 Windows 平台开发无边框还要支持拖拽、缩放、最小化还是挺难的。为啥,因为虽然用到的接口不多,但网络上资料零零碎碎,往往需要查阅很多 win32 api、常量值并整理 PInvoke,所以本项目创建之初就公开并同步在 GitHub 发布,帮助大家一站式实现标题中列举的功能。
功能:

已实现
无边框支持缩放支持拖拽窗体

计划实现
整合 System Tray For Unity加入 置顶 功能
动图:


实现:

1. 无边框
        static void InitAppWindow()    {      if (Application.isEditor) return;      var dwStyles = GetWindowLongPtr(UnityHWnd, GWL_STYLE);      var sty = ((ulong)dwStyles);      sty &= ~(WS_CAPTION| WS_DLGFRAME)&WS_POPUP;      SetWindowLongPtr(UnityHWnd, GWL_STYLE, (IntPtr)sty);    }
注意:设置无边框后,任务栏点击无法实现 App 最小化,如果有解决方案欢迎提 PR

2. 最小化
    //最小化窗口    //具体窗口参数看这   https://msdn.microsoft.com/en-us/library/windows/desktop/ms633548(v=vs.85).aspx    public static void SetMinWindows()    {      if (!Application.isEditor)      {            ShowWindow(UnityHWnd, SW_SHOWMINIMIZED);         }    }    //设置当前窗口的显示状态        public static extern bool ShowWindow(System.IntPtr hwnd, int nCmdShow);
Tips: 代码中用到的 win32 api 以及常量请参阅本文配套 GitHub 仓库:点我。

3. 缩放
using UnityEngine;using UnityEngine.EventSystems;using static PInvoke;public class WindowResizeHandler : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IBeginDragHandler, IEndDragHandler, IDragHandler{    bool isDragging = false;    bool isInsideOfHandler = false;    public Vector2 hotspot = Vector2.zero;    // Minimum and maximum values for window width/height in pixel.        private int minWidthPixel = 768;        private int maxWidthPixel = 2048;    public Texture2D wnes;    private float aspect = 16 / 9f;    void IBeginDragHandler.OnBeginDrag(PointerEventData eventData) => isDragging = eventData.pointerId==-1;    void IDragHandler.OnDrag(PointerEventData eventData) => WindowProcess(eventData);    void IEndDragHandler.OnEndDrag(PointerEventData eventData)    {      isDragging = false;      if (!isInsideOfHandler)      {            Cursor.SetCursor(default, default, CursorMode.Auto);      }    }    void IPointerEnterHandler.OnPointerEnter(PointerEventData eventData)    {      isInsideOfHandler = true;      Cursor.SetCursor(wnes, hotspot, CursorMode.Auto);    }    void IPointerExitHandler.OnPointerExit(PointerEventData eventData)    {      isInsideOfHandler = false;      if (!isDragging)      {            Cursor.SetCursor(default, default, CursorMode.Auto);      }    }    private void WindowProcess(PointerEventData eventData)    {      if (Application.isEditor || eventData.pointerId != -1) return;      RECT rc = default;      GetWindowRect(UnityHWnd, ref rc);      int newWidth = Mathf.Clamp(rc.Right - rc.Left + Mathf.RoundToInt(eventData.delta.x), minWidthPixel, maxWidthPixel);      int newHeight = Mathf.RoundToInt(newWidth / aspect);      SetWindowPos(UnityHWnd, 0, rc.Left, rc.Top, newWidth, newHeight, SWP_SHOWWINDOW);    }}
Tips: 代码中用到的 win32 api 以及常量请参阅本文配套 GitHub 仓库:点我。

4. 拖拽窗体
using UnityEngine;using UnityEngine.EventSystems;using UnityEngine.UI;using static PInvoke;public class WindowMoveHandler : MonoBehaviour,IPointerDownHandler,IPointerUpHandler,IPointerExitHandler{    static bool isDrag = false;    void IPointerDownHandler.OnPointerDown(PointerEventData eventData) => isDrag = eventData.pointerId==-1;    void IPointerExitHandler.OnPointerExit(PointerEventData eventData) => isDrag = false;    void IPointerUpHandler.OnPointerUp(PointerEventData eventData) => isDrag = !(eventData.pointerId==-1);    private void Update()    {      if (!Application.isEditor&&isDrag)      {            DragWindow();      }    }}    //拖动窗口    public static void DragWindow()    {      ReleaseCapture();      SendMessage(UnityHWnd, 0xA1, 0x02, 0);      SendMessage(UnityHWnd, 0x0202, 0, 0);    }
Tips: 代码中用到的 win32 api 以及常量请参阅本文配套 GitHub 仓库:点我。
扩展阅读:

System-Tray-Icon-For-Unity - 为 Unity 开发的 App 提供 System Tray Icon,后面计划使用 Unity MenuItem 的方式,现在使用起来感觉不怎么便利。UnitySkipSplash - 几句话跳过 Unity Logo 闪屏界面,别问我为何这么 big 胆,我觉得 Unity 官方似乎默许了。Simple-Customize-ERP-System - 本文配套的 GitHub 仓库。
写到最后:

版权所有,转载请注明出处!
页: [1]
查看完整版本: [Unity 3d] 使用 Unity 开发无边框、支持拖拽、缩放、最小化的应用