본문 바로가기
Unity

유니티 바탕화면 보이는 투명한 앱 만들기

by 위즈밈 2021. 11. 1.
반응형

에디터 위에 둥둥 떠있는 큐브

반응형

큐브를 띄운 채로 그림판 조작도 가능하다

 

이를 이용하면 모니터 위에 서식하는 미소녀를 띄우거나 할 수 있다.

 

이를 적용하기 전에 해야될 것이 두가지 있다.

 

1) 메인카메라 - Clear Flags를 Solid Color로 설정, 그 후 Background 컬러의 RGBA값을 0,0,0,0으로 설정

 

2) PlayerSettings - Resolution and Presentation의 Standalone Player Options에서 Use DXGI Flip Model Swapchain for D3D를 체크해제 

 

 

아래는 실질적으로 앱을 투명화시켜주는 코드다.

 

스타트 함수에서 

SetWindowLong(hWnd, GWL_EXSTYLE, WS_EX_LAYERED);

로 설정해놨기 때문에 앱의 투명한 부분을 클릭해도 아무런 일이 일어나지 않지만

 

스페이스바를 누르면

 SetWindowLong(hWnd, GWL_EXSTYLE, WS_EX_LAYERED | WS_EX_TRANSTPARENT);

로 변경되어 투명한 부분이 진짜 투명하게 되어 아래에 있는 것을 클릭할 수 있게 된다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
 
public class TransparentApp : 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 int SetWindowLong(IntPtr hWnd, int nIndex, uint 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 uint WS_EX_LAYERED = 0x00080000;
    const uint WS_EX_TRANSTPARENT = 0x00000020;
 
    void Start()
    {
        Application.runInBackground = true;
 
        hWnd = GetActiveWindow();
 
        MARGINS margins = new MARGINS { leftWidth = -1 };
        DwmExtendFrameIntoClientArea(hWnd, ref margins);
        SetWindowLong(hWnd, GWL_EXSTYLE, WS_EX_LAYERED);
 
        BringWindowToTop(hWnd);
        SetWindowPos(hWnd, HWND_TOPMOST, 0000, SWP_NOSIZE);
    }
 
    bool toggle = true;
    public void Update()
    {
        if (Input.GetKeyUp(KeyCode.Space))
        {
            toggle = !toggle;
 
            BringWindowToTop(hWnd);
            SetWindowPos(hWnd, HWND_TOPMOST, 0000, SWP_NOSIZE);
 
            if (toggle)
            {
                SetWindowLong(hWnd, GWL_EXSTYLE, WS_EX_LAYERED);
            }
            else
            {
                SetWindowLong(hWnd, GWL_EXSTYLE, WS_EX_LAYERED | WS_EX_TRANSTPARENT);
            }
        }
    }
}
 
cs

 

 

반응형

댓글