반응형

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

 

우선 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
:
반응형

투명한 배경의 윈도우를 만들어 보자.

 

스프라이트를 추가하고 스크립트를 연결한다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using Godot;
 
public partial class Control : Sprite2D
{
    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
    {
        GetViewport().TransparentBg = true;
    }
 
    public override void _Process(double delta)
    {
        RotationDegrees += 180.0f * (float)delta;
    }
}
 

 

위와 같은 코드를 작성한다.

 

Project Settings - Display - Window - Borderless / Transparent 옵션을 모두 체크한다.

 

실행하면 배경은 물론 타이틀바도 없는 윈도우에서 게임이 플레이된다.

 

반응형
Posted by J-sean
:
반응형

ParallaxBackground와 ParallaxLayer를 이용해 배경 화면을 자연스럽게 스크롤 해 보자.

 

Project Settings - Rendering - Textures - Canvas Textures - Default Texture Filter - Nearest 를 선택한다.

 

위와 같이 씬을 구성한다.

● ParallaxBackground - 두 개의 ParallaxLayer를 자식 노드로 생성하고 스크립트를 추가한다.

● ParallaxLayer - 각각 Sprite2D를 자식 노드로 생성한다.

● Sprite2D - 배경 화면을 Texture로 지정한다.

 

background_layer_1.png
0.01MB
background_layer_2.png
0.01MB

 

두 개의 ParallaxLayer 모두 Motion - Mirroring - X 속성을 스프라이트 크기에 맞게 지정한다.

수평으로 스크롤 하는 경우 X 속성을 지정하지만 수직이라면 Y 속성을 지정한다.

 

1
2
3
4
5
6
7
8
9
10
11
using Godot;
 
public partial class BackgroundScript : ParallaxBackground
{
    public int ScrollingSpeed = 100;
 
    public override void _Process(double delta)
    {
        ScrollOffset -= new Vector2(ScrollingSpeed * (float)delta, 0);
    }
}
 

 

ParallaxBackground 스크립트를 작성한다.

 

 

두 배경이 같은 속도로 이동한다.

 

멀리 있는 배경(ParallaxLayer)의 Motion - Scale 속성을 0.5로 지정한다.

 

멀리 있는 배경이 느리게 이동한다.

 

※ 참고

ParallaxBackground

ParallaxLayer

 

반응형
Posted by J-sean
: