Unity PC 自定义窗口‌标题栏

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的分享

  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity中的自定义弹框可以使用Unity3D Custom Popup插件来实现。该插件可以推迟构造弹出窗口的显示,对于构造弹出窗口相对昂贵的情况非常有用。你可以通过执行`yarn add rotorz/unity3d-custom-popup`命令来安装该软件包,并将其与Unity工具兼容。然后按照插件的说明进行操作,将自定义弹出控件添加到Unity编辑器界面中。 具体而言,你可以在代码中调用`MyPermissionDialog.Builder`来创建一个自定义弹框。可以使用`setPositiveButton`方法设置弹框的确认按钮的点击事件,并在点击事件中执行相应的操作。通过`setTitleAndContext`方法可以设置弹框的标题和内容。最后,通过调用`create`方法创建弹框,并使用`show`方法显示出来。 请注意,这只是一种实现自定义弹框的方式,根据你的具体需求,可能还有其他的方法可供选择。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Android自定义弹框以及弹框资源加载](https://blog.csdn.net/u014146238/article/details/104824842)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [unity3d-custom-popup:Unity编辑器界面的自定义弹出控件,将弹出菜单的构造推迟到显示为止。 这对于弹出...](https://download.csdn.net/download/weixin_42131443/18312293)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值