当前位置:   UNITY > 正文

2023最新unity 发布exe程序背景透明穿透_unity 透明窗口

unity 透明窗口

由于网上的一些帖子比较老了,好多人评论不能用黑屏之类的。这边重新整理个最新的,亲测可用。

先大体概括下,一共就几处设置,一个脚本。

一、设置

普通渲染的项目摄像机的Clear Flags选择Solid Color,颜色为黑色(0,0,0,0)。

使用HDRP的项目,关闭掉摄像机上的脚本:HDAdditionalCameraData,摄像机默认的backgroundType,Sky改为Color,颜色为黑色(0,0,0,0)见下图。

↓↓↓特别注意下图,所使用的unity版本如果没有下面的选项则不用管。

下面放上脚本:

  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Runtime.InteropServices;
  5. using System.IO;
  6. /// <summary>
  7. /// 一共可选择三种样式
  8. /// </summary>
  9. public enum enumWinStyle
  10. {
  11. /// <summary>
  12. /// 置顶
  13. /// </summary>
  14. WinTop,
  15. /// <summary>
  16. /// 置顶并且透明
  17. /// </summary>
  18. WinTopApha,
  19. /// <summary>
  20. /// 置顶透明并且可以穿透
  21. /// </summary>
  22. WinTopAphaPenetrate
  23. }
  24. public class WinSetting : MonoBehaviour
  25. {
  26. #region Win函数常量
  27. private struct MARGINS
  28. {
  29. public int cxLeftWidth;
  30. public int cxRightWidth;
  31. public int cyTopHeight;
  32. public int cyBottomHeight;
  33. }
  34. [DllImport("user32.dll")]
  35. static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  36. [DllImport("user32.dll")]
  37. static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
  38. [DllImport("user32.dll")]
  39. static extern int GetWindowLong(IntPtr hWnd, int nIndex);
  40. [DllImport("user32.dll")]
  41. static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
  42. [DllImport("user32.dll")]
  43. static extern int SetLayeredWindowAttributes(IntPtr hwnd, int crKey, int bAlpha, int dwFlags);
  44. [DllImport("Dwmapi.dll")]
  45. static extern uint DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margins);
  46. [DllImport("user32.dll")]
  47. private static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);
  48. private const int WS_POPUP = 0x800000;
  49. //设定一个新的扩展风格
  50. private const int GWL_EXSTYLE = -20;
  51. // 定义窗体样式,-16表示设定一个新的窗口风格
  52. private const int GWL_STYLE = -16;
  53. private const int WS_EX_LAYERED = 0x00080000;
  54. private const int WS_BORDER = 0x00800000;
  55. private const int WS_CAPTION = 0x00C00000;
  56. private const int SWP_SHOWWINDOW = 0x0040;
  57. private const int LWA_COLORKEY = 0x00000001;
  58. private const int LWA_ALPHA = 0x00000002;
  59. private const int WS_EX_TRANSPARENT = 0x20;
  60. //
  61. private const int ULW_COLORKEY = 0x00000001;
  62. private const int ULW_ALPHA = 0x00000002;
  63. private const int ULW_OPAQUE = 0x00000004;
  64. private const int ULW_EX_NORESIZE = 0x00000008;
  65. #endregion
  66. //
  67. public string strProduct;//项目名称
  68. public enumWinStyle WinStyle = enumWinStyle.WinTop;//窗体样式
  69. //
  70. public int ResWidth;//窗口宽度
  71. public int ResHeight;//窗口高度
  72. //
  73. public int currentX;//窗口左上角坐标x
  74. public int currentY;//窗口左上角坐标y
  75. //
  76. private bool isApha;//是否透明
  77. private bool isAphaPenetrate;//是否要穿透窗体
  78. // Use this for initialization
  79. void Awake()
  80. {
  81. Screen.fullScreen = false;
  82. #if UNITY_EDITOR
  83. print("编辑模式不更改窗体");
  84. #else
  85. switch (WinStyle)
  86. {
  87. case enumWinStyle.WinTop:
  88. isApha = false;
  89. isAphaPenetrate = false;
  90. break;
  91. case enumWinStyle.WinTopApha:
  92. isApha = true;
  93. isAphaPenetrate = false;
  94. break;
  95. case enumWinStyle.WinTopAphaPenetrate:
  96. isApha = true;
  97. isAphaPenetrate = true;
  98. break;
  99. }
  100. //获得窗口句柄
  101. IntPtr hwnd = FindWindow(null, strProduct);
  102. //
  103. if (isApha)
  104. {
  105. //去边框并且透明
  106. SetWindowLong(hwnd, GWL_EXSTYLE, WS_EX_LAYERED);
  107. // 获得当前样式
  108. int intExTemp = GetWindowLong(hwnd, GWL_EXSTYLE);
  109. if (isAphaPenetrate)//是否透明穿透窗体
  110. {
  111. SetWindowLong(hwnd, GWL_EXSTYLE, intExTemp | WS_EX_TRANSPARENT | WS_EX_LAYERED);
  112. }
  113. //
  114. SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_BORDER & ~WS_CAPTION); //无边框、无标题栏
  115. SetWindowPos(hwnd, -1, currentX, currentY, ResWidth, ResHeight, SWP_SHOWWINDOW);
  116. var margins = new MARGINS() { cxLeftWidth = -1 };// 边距内嵌值确定在窗口四侧扩展框架的距离 -1为没有窗口边框
  117. //
  118. DwmExtendFrameIntoClientArea(hwnd, ref margins);
  119. SetLayeredWindowAttributes(hwnd, 0, 255, 1);
  120. }
  121. else
  122. {
  123. //单纯去边框
  124. SetWindowLong(hwnd, GWL_STYLE, WS_POPUP);
  125. SetWindowPos(hwnd, -1, currentX, currentY, ResWidth, ResHeight, SWP_SHOWWINDOW);
  126. }
  127. #endif
  128. }
  129. }

 随便扔到哪,我是扔到摄像机上了,说下参数填写,见下图1图2

挂上脚本后,strproduct要和图2处的ProductName填写的一样,老版本的unity导出的exe名字也要一样,,新版的unity只要选择存放的文件夹,名字不用管。

 下面是效果

普通的

unity 发布exe透明穿透(普通)

 HDRP的

unity 发布exe透明穿透(HDRP)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/UNITY/detail/34369
推荐阅读
相关标签
  

闽ICP备14008679号