TransparentWindow.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Runtime.InteropServices;
  5. using UnityEngine;
  6. public class TransparentWindow : MonoBehaviour
  7. {
  8. [SerializeField]
  9. private Material m_Material;
  10. private struct MARGINS
  11. {
  12. public int cxLeftWidth;
  13. public int cxRightWidth;
  14. public int cyTopHeight;
  15. public int cyBottomHeight;
  16. }
  17. // Define function signatures to import from Windows APIs
  18. [DllImport("user32.dll")]
  19. private static extern IntPtr GetActiveWindow();
  20. [DllImport("user32.dll")]
  21. private static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);
  22. [DllImport("Dwmapi.dll")]
  23. private static extern uint DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margins);
  24. // Definitions of window styles
  25. const int GWL_STYLE = -16;
  26. const uint WS_POPUP = 0x80000000;
  27. const uint WS_VISIBLE = 0x10000000;
  28. void Start()
  29. {
  30. #if !UNITY_EDITOR
  31. var margins = new MARGINS() { cxLeftWidth = -1 };
  32. var hwnd = GetActiveWindow();
  33. SetWindowLong(hwnd, GWL_STYLE, WS_POPUP | WS_VISIBLE);
  34. DwmExtendFrameIntoClientArea(hwnd, ref margins);
  35. #endif
  36. }
  37. void OnRenderImage(RenderTexture from, RenderTexture to)
  38. {
  39. Graphics.Blit(from, to, m_Material);
  40. }
  41. }