Unity3d去掉Windows版本的边框

[csharp] view plaincopy

 

  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Runtime.InteropServices;  
  4. using System;  
  5.   
  6. public class Test : MonoBehaviour  
  7. {   
  8.     /// <summary>  
  9.     /// 窗口宽度  
  10.     /// </summary>  
  11.     public int winWidth;  
  12.     /// <summary>  
  13.     /// 窗口高度  
  14.     /// </summary>  
  15.     public int winHeight;  
  16.     /// <summary>  
  17.     /// 窗口左上角x  
  18.     /// </summary>  
  19.     public int winPosX;  
  20.     /// <summary>  
  21.     /// 窗口左上角y  
  22.     /// </summary>  
  23.     public int winPosY;  
  24.   
  25.     [DllImport("user32.dll")]  
  26.     static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);  
  27.     [DllImport("user32.dll")]  
  28.     static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);  
  29.     [DllImport("user32.dll")]  
  30.     static extern IntPtr GetForegroundWindow();  
  31.   
  32.     const uint SWP_SHOWWINDOW = 0x0040;  
  33.     const int GWL_STYLE = -16;  
  34.     const int WS_BORDER = 1;  
  35.     const int WS_POPUP = 0x800000;  
  36.   
  37.     // Use this for initialization  
  38.     void Start()  
  39.     {  
  40.         winWidth = 610;  
  41.         winHeight = 350;  
  42.         //显示器支持的所有分辨率  
  43.         int i = Screen.resolutions.Length;  
  44.   
  45.         int resWidth = Screen.resolutions[i - 1].width;  
  46.         int resHeight = Screen.resolutions[i - 1].height;  
  47.   
  48.         winPosX = resWidth / 2 - winWidth / 2;  
  49.         winPosY = resHeight / 2 - winHeight / 2;  
  50.   
  51.         SetWindowLong(GetForegroundWindow(), GWL_STYLE, WS_POPUP);  
  52.         bool result = SetWindowPos(GetForegroundWindow(), 0, winPosX, winPosY, winWidth, winHeight, SWP_SHOWWINDOW);  
  53.   
  54.     }  
  55. }  

原文地址点击这里

 

测试发现GUI坐标偏差,修改如下

 

[csharp] view plaincopy

 

  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Runtime.InteropServices;  
  4. using System;  
  5.   
  6. public class Test : MonoBehaviour  
  7. {  
  8.     /// <summary>  
  9.     /// 窗口宽度  
  10.     /// </summary>  
  11.     public int winWidth;  
  12.     /// <summary>  
  13.     /// 窗口高度  
  14.     /// </summary>  
  15.     public int winHeight;  
  16.     /// <summary>  
  17.     /// 窗口左上角x  
  18.     /// </summary>  
  19.     public int winPosX;  
  20.     /// <summary>  
  21.     /// 窗口左上角y  
  22.     /// </summary>  
  23.     public int winPosY;  
  24.   
  25.     [DllImport("user32.dll")]  
  26.     static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);  
  27.     [DllImport("user32.dll")]  
  28.     static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);  
  29.     [DllImport("user32.dll")]  
  30.     static extern IntPtr GetForegroundWindow();  
  31.   
  32.     [DllImport("User32.dll", EntryPoint = "GetSystemMetrics")]  
  33.     public static extern IntPtr GetSystemMetrics(int nIndex);  
  34.   
  35.     const int SM_CXSCREEN = 0x00000000;  
  36.     const int SM_CYSCREEN = 0x00000001;  
  37.   
  38.     const uint SWP_SHOWWINDOW = 0x0040;  
  39.     const int GWL_STYLE = -16;  
  40.     const int WS_BORDER = 1;  
  41.     const int WS_POPUP = 0x800000;  
  42.   
  43.     public GUIText t;  
  44.   
  45.     // Use this for initialization  
  46.     void Start()  
  47.     {  
  48.         winWidth = 610;  
  49.         winHeight = 350;  
  50.         //当前屏幕分辨率  
  51.         int resWidth = (int)GetSystemMetrics(SM_CXSCREEN);  
  52.         int resHeight = (int)GetSystemMetrics(SM_CYSCREEN);  
  53.   
  54.         winPosX = resWidth / 2 - winWidth / 2;  
  55.         winPosY = resHeight / 2 - winHeight / 2 -1;  
  56.   
  57.         SetWindowLong(GetForegroundWindow(), GWL_STYLE, WS_POPUP);  
  58.         //测试发现左下角坐标为(0,1),修改如下  
  59.         bool result = SetWindowPos(GetForegroundWindow(), 0, winPosX, winPosY, winWidth, winHeight, SWP_SHOWWINDOW);  
  60.         //bool result = SetWindowPos(GetForegroundWindow(), 0, winPosX, winPosY, winWidth, winHeight, SWP_SHOWWINDOW);  
  61.           
  62.     }  
  63.   
  64.     void OnGUI()  
  65.     {  
  66.         if (Input.GetMouseButtonDown(0))  
  67.         {  
  68.             t.text = "X:"+Input.mousePosition.x + " Y:" + Input.mousePosition.y;  
  69.         }  
  70.     }  
  71. }  

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值