[Unity 3d] 使用 Unity 開發(fā)無邊框、可拖拽、縮放、置頂、最小化的應(yīng)用

老哥要我開發(fā)一個(gè) ERP ,七彎八轉(zhuǎn)最后終究是用 excel 給他整了一個(gè)。哥哥賊拉開心,還給了我 500 零花錢,美滋滋。所以徒留這個(gè)還沒開工就宣告畢業(yè)的項(xiàng)目給大家參考。

前言:

在 Unity 開發(fā)中,如果想要在 Windows 平臺(tái)開發(fā)無邊框還要支持拖拽、縮放、最小化還是挺難的。為啥,因?yàn)殡m然用到的接口不多,但網(wǎng)絡(luò)上資料零零碎碎,往往需要查閱很多 win32 api、常量值并整理 PInvoke,所以本項(xiàng)目創(chuàng)建之初就公開并同步在 GitHub 發(fā)布,幫助大家一站式實(shí)現(xiàn)標(biāo)題中列舉的功能。

功能:

已實(shí)現(xiàn)

  1. 無邊框
  2. 支持縮放
  3. 支持拖拽窗體
  4. 支持置頂功能

計(jì)劃實(shí)現(xiàn)

動(dòng)圖:

實(shí)現(xiàn):

1. 無邊框

    [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSplashScreen)]
    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);
    }

注意:設(shè)置無邊框后,任務(wù)欄點(diǎn)擊無法實(shí)現(xiàn) App 最小化,如果有解決方案歡迎提 PR

2. 最小化

    //最小化窗口
    //具體窗口參數(shù)看這     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); 
        }
    }
    //設(shè)置當(dāng)前窗口的顯示狀態(tài)
    [DllImport("user32.dll")]
    public static extern bool ShowWindow(System.IntPtr hwnd, int nCmdShow);

Tips: 代碼中用到的 win32 api 以及常量請(qǐng)參閱本文配套 GitHub 倉庫:點(diǎn)我。

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.
    [SerializeField]
    private int minWidthPixel = 768;
    [SerializeField]
    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 以及常量請(qǐng)參閱本文配套 GitHub 倉庫:點(diǎn)我

4. 拖拽窗體

using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using static PInvoke;
[RequireComponent(typeof(Graphic))]
public class WindowMoveHandler : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
    void IPointerDownHandler.OnPointerDown(PointerEventData eventData)
    {
        if (eventData.pointerId == -1)
        {
            DragWindow();
        }
    }
    void IPointerUpHandler.OnPointerUp(PointerEventData eventData)
    {
        if (eventData.pointerId == -1)
        {
            MouseButtonUp();
        }
    }
}
    //拖動(dòng)窗口
    public static void DragWindow()
    {
        ReleaseCapture();
        SendMessage(UnityHWnd, 0xA1, 0x02, 0);
        SendMessage(UnityHWnd, 0x0202, 0, 0);
    }

5. 置頂窗體(Topmost)

    // 應(yīng)用窗口置頂
    public static void SetTopmost(bool isTopmost)
    {
        if (!Application.isEditor)
        {
            int ptr = isTopmost ? -1 : -2;
            SetWindowPos(UnityHWnd, ptr, 0, 0, 0, 0, 1 | 2 | 64);//0x0040
        }
        else
        {
            Debug.LogWarning($"{nameof(PInvoke)}: 為避免編輯器行為異常,請(qǐng)打包 exe 后測(cè)試!");
        }
    }

Tips: 代碼中用到的 win32 api 以及常量請(qǐng)參閱本文配套 GitHub 倉庫:點(diǎn)我。

擴(kuò)展閱讀:

  1. System-Tray-Icon-For-Unity - 為 Unity 開發(fā)的 App 提供 System Tray Icon,后面計(jì)劃使用 Unity MenuItem 的方式,現(xiàn)在使用起來感覺不怎么便利。
  2. UnitySkipSplash - 幾句話跳過 Unity Logo 閃屏界面,別問我為何這么 big 膽,僅供學(xué)習(xí)用途嘛。
  3. Simple-Customize-ERP-System - 本文配套的 GitHub 倉庫。

https://www.laowangomg.com/?p=579

寫到最后:

  • 版權(quán)所有,轉(zhuǎn)載請(qǐng)注明出處!
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容