I want the background to be fully transparent and allow the UI elements to show the Window behind or things. For now this is what i have.
Unity Version: 6.1
The DXCI Swapchain is Disabled
When i build and start it there are my GameObjects and UI Elements Visible, it is Clickable and it interacts with the things behind but the Background is Black
using System;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.EventSystems;
public class TransparentWindow : MonoBehaviour
{
[DllImport("user32.dll")]
static extern IntPtr GetActiveWindow();
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll", SetLastError = true)]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
private struct MARGINS
{
public int cxLeftWidth;
public int cxRightWidth;
public int cyTopHeight;
public int cyBottomHeight;
}
[DllImport("Dwapi.dll")]
private static extern uint DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margins);
const int GWL_EXSTYLE = -20;
const int WS_EX_LAYERED = 0x00080000;
const int WS_EX_TRANSPARENT = 0x00000020;
IntPtr hWnd;
void Start()
{
#if !UNITY_EDITOR
hWnd = GetActiveWindow();
MARGINS margins = new MARGINS{ cxLeftWidth = -1 };
DwmExtendFrameIntoClientArea(hWnd, ref margins);
SetWindowLong(hWnd, GWL_EXSTYLE, WS_EX_LAYERED | WS_EX_TRANSPARENT);
SetWindowPos(hWnd, (IntPtr)(-1), 0, 0, 0, 0, 0);
#endif
}
void SetClickTrough(bool clicktrough)
{
#if !UNITY_EDITOR
if (clicktrough)
{
SetWindowLong(hWnd, GWL_EXSTYLE, WS_EX_LAYERED | WS_EX_TRANSPARENT);
}
else
{
SetWindowLong(hWnd, GWL_EXSTYLE, WS_EX_LAYERED);
}
#endif
}
void Update()
{
bool over3DObject = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out _);
bool overUI = EventSystem.current.IsPointerOverGameObject();
SetClickTrough(!(over3DObject || overUI));
}
}
