반응형

배경이 투명한 애플리케이션을 만들어 보자.

 

우선 Built-in Render Pipeline의 경우다.

 

Clear Flags - Solid Color, Background - Black

 

Use DXGI flip model swapchain for D3D11 체크 해제

 

Auto Graphics API for Windows 체크 해제 하고 Direct3D11을 위로 올린다.

 

간단한 입력을 위해 Input Manager를 사용했다.

 

 

using System;
using UnityEngine;
using System.Runtime.InteropServices;

public class NewBehaviourScript : MonoBehaviour
{
    public struct MARGINS
    {
        public int leftWidth;
        public int rightWidth;
        public int topHeight;
        public int bottomHeight;
    }

    [DllImport("user32.dll")]
    public static extern IntPtr GetActiveWindow();

    [DllImport("user32.dll")]
    public static extern IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong);

    [DllImport("user32.dll")]
    public static extern int BringWindowToTop(IntPtr hwnd);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);

    [DllImport("Dwmapi.dll")]
    public static extern uint DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margins);

    static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
    static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);

    IntPtr hWnd;
    const UInt32 SWP_NOSIZE = 0x0001;
    const UInt32 SWP_NOMOVE = 0x0002;

    const int GWL_EXSTYLE = -20;
    const long WS_EX_LAYERED = 0x00080000L;
    const long WS_EX_TRANSTPARENT = 0x00000020L;

    void Start()
    {
        Application.runInBackground = true;

        hWnd = GetActiveWindow();

        MARGINS margins = new MARGINS { leftWidth = -1 };
        DwmExtendFrameIntoClientArea(hWnd, ref margins);
        // Negative margins have special meaning to DwmExtendFrameIntoClientArea.
        // Negative margins create the "sheet of glass" effect, where the client area
        // is rendered as a solid surface with no window border.
        SetWindowLongPtr(hWnd, GWL_EXSTYLE, (IntPtr)WS_EX_LAYERED);

        BringWindowToTop(hWnd);
        SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE);
    }

    bool toggle = true;
    void Update()
    {
        if (Input.GetKeyUp(KeyCode.Space))
        {
            toggle = !toggle;

            BringWindowToTop(hWnd);
            SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE);

            if (toggle)
            {
                SetWindowLongPtr(hWnd, GWL_EXSTYLE, (IntPtr)WS_EX_LAYERED);
            }
            else
            {
                SetWindowLongPtr(hWnd, GWL_EXSTYLE, (IntPtr)(WS_EX_LAYERED | WS_EX_TRANSTPARENT));
            }
        }
    }
}

 

스크립트를 작성하고 적당한 오브젝트에 추가한다. 스페이스 키를 누르면 배경의 존재 유무가 변경된다.

 

빌드하고 실행하면 투명한 배경에서 애플리케이션이 표시된다.

 

Univeral Reder Pipeline의 경우,

1. 프로젝트 창에서 Assets/settings/PC_Renderer(예: Renderer2D)/Post-Processing/*Enabled 체크 해제*
2. 프로젝트 창에서 Assets/settings/PC_RP(예: UniversalRP)/Quality/HDR/*체크 해제*

작업을 추가로 진행해야 한다.

 

※ 참고

https://youtu.be/RqgsGaMPZTw?si=ypzYCdXufnlmBRSd

 

반응형
Posted by J-sean
: