unity打包exe运行无窗口、窗体放大缩小效果

一、第一种办法效果不是很好,请看第二种办法
效果展示:
在这里插入图片描述
在这里插入图片描述

在Files->Build Setting 中设置无对话窗模式
在这里插入图片描述

在这里插入图片描述

#region 窗口放大缩小
    public int winPosX;                 //窗口左上角x
    public int winPosY;                 //窗口左上角y

    public int minWidth, maxWidth;      //窗口宽度
    public int minHeight, maxHeight;    //窗口高度
    
    [DllImport("user32.dll")]
    static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);
    [DllImport("user32.dll")]
    static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
    [DllImport("user32.dll")]
    static extern IntPtr GetForegroundWindow();

    const uint SWP_SHOWWINDOW = 0x0040;
    const int GWL_STYLE = -16;
    const int WS_BORDER = 1;
    const int WS_POPUP = 0x800000;

    private void WindowSizeEvent(bool ison)
    {
        if (ison)
        {
            //显示器支持的所有分辨率
            int i = Screen.resolutions.Length;

            int resWidth = Screen.resolutions[i - 1].width;
            int resHeight = Screen.resolutions[i - 1].height;

            winPosX = resWidth / 2 - minWidth / 2;
            winPosY = resHeight / 2 - minHeight / 2;

            SetWindowLong(GetForegroundWindow(), GWL_STYLE, WS_POPUP);
            bool result = SetWindowPos(GetForegroundWindow(), 0, winPosX, winPosY, minWidth, minHeight, SWP_SHOWWINDOW);

            windowSize.GetComponent<Image>().sprite = icon_big;//修改图标
        }
        else
        {
            //显示器支持的所有分辨率
            int i = Screen.resolutions.Length;

            int resWidth = Screen.resolutions[i - 1].width;
            int resHeight = Screen.resolutions[i - 1].height;

            winPosX = resWidth / 2 - minWidth / 2;
            winPosY = resHeight / 2 - minHeight / 2;

            SetWindowLong(GetForegroundWindow(), GWL_STYLE, WS_POPUP);
            bool result = SetWindowPos(GetForegroundWindow(), 0, 0, 0, maxWidth, maxHeight, SWP_SHOWWINDOW);

            windowSize.GetComponent<Image>().sprite = icon_smaller;//修改图标
        }
    }
    #endregion

ps:我用的2017版本,在打包时设置公司名称和项目名称时效果会失效,如果在使用时,代码没有问题,屏幕大小设置没问题,但是效果还是不对的话,建议恢复Bulid Setting属性。

二、第二种办法是调用unity 窗口模式下的放大缩小功能

1.首先我们创建四个按钮 分别是 放大、缩小、还原、关闭
2.设置Project Settings的窗口属性 注意:2019版本没有窗口模式选项了
在这里插入图片描述
3.代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using UMP;
using System.Runtime.InteropServices;
using System;

public class Test : MonoBehaviour
{
    public Button bigBtn;
    public Button smallBtn;
    public Button restoreBtn;
    public Button closegBtn;
    //public InputField input;
    //public UniversalMediaPlayer UniversalMediaPlayer;
    void Start()
    {
        bigBtn.onClick.AddListener(OnClickMaximize);
        smallBtn.onClick.AddListener(OnClickMinimize);
        restoreBtn.onClick.AddListener(OnClickRestore);
        closegBtn.onClick.AddListener(CloseWindow);
    }

    #region 窗口放大缩小
    [DllImport("user32.dll")]
    public static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);

    [DllImport("user32.dll")]
    static extern IntPtr GetForegroundWindow();

    const int SW_SHOWMINIMIZED = 2; //{最小化, 激活}
    const int SW_SHOWMAXIMIZED = 3;//最大化
    const int SW_SHOWRESTORE = 1;//还原

    //最小化
    public void OnClickMinimize()
    {
        Debug.Log("最小化");
        ShowWindow(GetForegroundWindow(), SW_SHOWMINIMIZED);
    }

    //最大化
    public void OnClickMaximize()
    {
        Debug.Log("最大化");
        ShowWindow(GetForegroundWindow(), SW_SHOWMAXIMIZED);
    }

    //还原
    public void OnClickRestore()
    {
        Debug.Log("还原");

        ShowWindow(GetForegroundWindow(), SW_SHOWRESTORE);
    }


    #endregion
    void CloseWindow()
    {
        Application.Quit();
    }

}

在场景拖入对应按钮,打包就可以了。

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值