Unity exe去掉边框

有时用unity发布exe,但是不希望有系统自带的那个最小化,关闭的的边框,下面就来了:

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

public class WindowRemoveBorder : MonoBehaviour {

    public Rect screenPosition;

    [DllImport("user32.dll")]
    static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);

    [DllImport("user32.dll")]
    static extern bool SetWindowPos(IntPtr hWnd, int hWndInserAfter, int X, int Y, int cx, int cy, uint uFlags);

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

    [DllImport("User32.dll", EntryPoint = "GetSystemMetrics")]
    public static extern IntPtr GetSystemMetrics(int nIndex);

    const int SM_CXSCREEN = 0x00000000;
    const int SM_CYSCREEN = 0x00000001;  

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

    void Start()
    {
        //当前屏幕分辨率  
        int resWidth = (int)GetSystemMetrics(SM_CXSCREEN);  
        int resHeight = (int)GetSystemMetrics(SM_CYSCREEN);

        //窗体的宽高
        screenPosition.width = Screen.width;
        screenPosition.height = Screen.height;

        //设置窗体的位置
        screenPosition.x = resWidth / 2 - screenPosition.width / 2;
        screenPosition.y = resHeight / 2 - screenPosition.height / 2;

        //将网上的WS_BORDER替换成WS_POPUP
        SetWindowLong(GetForegroundWindow(), GWL_STYLE, WS_POPUP);

        bool result = SetWindowPos(GetForegroundWindow(), 0, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW);
    }
}

效果图



参考文章:

1、http://www.360doc.com/content/16/0620/14/30388632_569261323.shtml

2、http://blog.youkuaiyun.com/awnuxcvbn/article/details/38545419

要在Unity PC应用程序中去掉边框和设置透明背景,可以按照以下步骤进行操作: 1. 首先,确保你的Unity版本高于5.0。边框和透明背景选项在较旧的版本中可能不可用。 2. 选择你的主摄像机对象,然后在检视面板中找到“Clear Flags(清除标记)”设置。将其更改为“Solid Color(纯色)”。 3. 在同一检视面板中,找到“Background(背景)”设置,并将其颜色设置为透明。你可以使用颜色拾取器或手动输入RGBA值来设置透明颜色。 4. 接下来,需要修改你的应用程序的窗口样式。在你的脚本中,使用以下代码: ```csharp using System; using System.Runtime.InteropServices; public class WindowStyle : MonoBehaviour { [DllImport("user32.dll")] public static extern IntPtr GetActiveWindow(); [DllImport("user32.dll")] public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); [DllImport("user32.dll")] public static extern bool SetLayeredWindowAttributes(IntPtr hWnd, uint crKey, byte alpha, int dwFlags); private const int GWL_STYLE = -16; private const int WS_BORDER = 0x00800000; private const int WS_EX_LAYERED = 0x00080000; private const int LWA_COLORKEY = 0x00000001; private void Start() { IntPtr hWnd = GetActiveWindow(); SetWindowLong(hWnd, GWL_STYLE, WS_BORDER); SetWindowLong(hWnd, WS_EX_LAYERED, WS_EX_LAYERED); SetLayeredWindowAttributes(hWnd, 0x00FFFFFF, 0, LWA_COLORKEY); } } ``` 执行以上脚本后,将在应用程序开始时设置窗口样式。边框将被删除,并且背景将成为透明的。 5. 最后,你可以根据需要自定义应用程序窗口的大小和位置,以适应你的项目需求。你可以在Unity的Player设置中或在代码中进行调整。 希望以上解答对你有所帮助!如果还有任何疑问,请随时追问。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值