Transparency support for Borderless Windows

I am looking to add transparency support to a Borderless Windows script we are working on, this uses the User32 DLL so this is Windows only for the moment.
I am not looking to affect the whole window, but areas designated transparent in the Unity UI, I have set the camera to Depth Only so that there is nothing visible behind it, but I believe extra code is required to get the window to recognise the alpha channel from Unity.
Here is the bulk of what I have so far.
(I have experience with programming, but from a web development standpoint, I am quite new to the Windows API so any help that can be given is greatly appreciated.)

using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Diagnostics;
using UnityEngine;
public class WindowDriver : MonoBehaviour
{
    public Rect ScreenPosition;
    public bool IsFullscreen = false;
#if UNITY_STANDALONE_WIN && !UNITY_EDITOR
    [DllImport("user32.dll")]
    static extern IntPtr SetWindowLong (IntPtr hwnd, int  _nIndex, int  dwNewLong);
    [DllImport("user32.dll")]
    static extern bool SetWindowPos (IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
    [DllImport("user32.dll")]
    static extern IntPtr GetForegroundWindow ();
    const uint SWP_SHOWWINDOW = 0x0040;
    const int GWL_STYLE = -16;
    const int WS_BORDER = 1;
    void Start ()
    {
        if(IsFullscreen)
            ScreenPosition = GetFullscreenResolution();
        SetWindowLong (GetForegroundWindow (), GWL_STYLE, WS_BORDER);
        bool result = SetWindowPos (GetForegroundWindow (), 0, (int)ScreenPosition.x, (int)ScreenPosition.y, (int)ScreenPosition.width, (int)ScreenPosition.height, SWP_SHOWWINDOW);
    }
#endif
#if UNITY_EDITOR
    void Update()
    {
        if (IsFullscreen)
            ScreenPosition = GetFullscreenResolution();
    }
#endif
    Rect GetFullscreenResolution()
    {
        Resolution resolution = Screen.currentResolution;
        return new Rect(0f, 0f, (float)resolution.width, (float)resolution.height);
    }
}

Just to clarify, I am looking for the ability to see through the window in the areas defined as transparent, and preferably to be able to see through the partially transparent areas as you would in Unity, but to be able to see through the entire window.

I’ve not found much. I was pointed to this:

I’ll keep trying to get some traction.

Do Unity Standalone builds run on OpenGL?

Not by default on Windows. You can use the -force-opengl command line argument to force it.

Ironically it looks like you can also use -popupwindow to create a window without a frame on it and skip that mess you currently have.

Before I statrted this I knew it could be achieved with -popupwindow but I wanted something more versatile and controllable in Unity.
My biggest problem now is getting it to recognise transparency if it is set in the UI.

Just wondering if anyone has gotten any closer with this? Having it not transparent in the places I need it is really beginning to bug me from a design perspective.
I am under the impression that it has something to do with he camera settings, I know I need some more code (not sure what) to get this working, but I am also sure that the camera needs something extra as even with the Clear Flags as Depth Only or Don’t Clear, the UI appears to be completely solid in the build, not even lighter.

I’m still unclear about exactly what you are trying to do. Can you post a screenshot of what you’re after?

It’s not camera settings - the camera doesn’t know about the window it resides in. It can’t know to clear the entire background down to the desktop.

Since you mentioned this is for a launcher and you’ve already gone through the trouble of making your solution Windows-only by not using the command line arg then why don’t you just go “all the way” and write a Winform application instead?

I already have the window borderless (as shown in provided code), now I am trying to make the areas that are set to transparent, well…transparent.

I have nothing behind it, so this is just UI driven, like a menu.
I am trying to get areas of the window to be transparent depending on the alpha of the UI pieces, so you can see whatever is behind it if there is nothing in a specific area of the UI, instead of it being black.

In the image below, purple represents the areas that are transparent (don’t have anything in the UI) and white is areas I want solid (or partially solid). On the right is what appears if I leave the purple areas transparent when I build it.
What I am looking for is for the black areas in the image on the right to be transparent, and anywhere I set the ui to partial transparency (such as alpha at 100 for example) to be partially transparent.

The reason I didn’t want to do this as a Windows Form Application is due to the clear superiority the Unity UI has over all other design programs, it’s easy to use and I am already used to it.

If it would be easier, I can use command line arguements, I didn’t realise these could be used in Unity like this, although in the documentation for command line arguenements I cannot find anything about borderless windows or transparency.

There’s no command line argument that will do what you’re asking. My only point was that you’ve already locked yourself into Windows with your custom solution and nothing you’re doing relies on Unity-based functionality so instead of fighting Unity trying to do something it doesn’t support you could just make a Winform application which isn’t as locked in.

1 Like

I did not want to use command line arguments because as far as I can see, they cannot be put into a script within the Unity development environment, but must be used like flags outside.
I am mainly developing for Windows anyway, if I do decide at a later date to go over to Mac for example, I will design another launcher around Macintosh, it isn’t that much different with Mac anyway.

I wanted to stay away from WinForms as I wanted to keep it all within Unity, as previously mentioned.

For what reason specifically? You’ve already discarded the cross-platform compatibility that Unity affords you. You’re okay with pulling in user32.dll but not using Winforms? Why?

This is mostly for the flexible and easy UI elements now in the new versions of Unity.
A windows form application does not give the kind of UI that Unity gives (easily).

EDIT: It appears, using WPF, that this is possible in XAML using AllowTransparency=“True” as a launch tag, I know this is also doable in Wind Forms (just need to work out how in Unity) but I was wondering if it is possible to build a Unity build to the source of the Windows Form instead of the exe.