This is the 10th day of my participation in the More text Challenge. For more details, see more text Challenge

See online useful Unity to do the desktop pet, he searched some information he made a small Demo.

The specific steps are as follows:

The idea is that there is a script and a Shader that are responsible for making the Unity runtime background transparent. This is done by adjusting the colors. Specific principle is not clear, looked up information is probably like this

First, the code part:

shader:

Shader "Custom/MakeTransparent" {
  Properties {
    _MainTex ("Base (RGB)", 2D) = "white" {}
    _TransparentColorKey ("Transparent Color Key", Color) = (0.1.0.1)
    _TransparencyMargin ("Transparency Margin", Float) = 0.01 
  }
  SubShader {
    Pass {
      Tags { "RenderType"="Opaque" }
      LOD 200
    
      CGPROGRAM

      #pragma vertex VertexShaderFunction
      #pragma fragment PixelShaderFunction
    
      #include "UnityCG.cginc"

      struct VertexData
      {
        float4 position : POSITION;
        float2 uv : TEXCOORD0;
      };

      struct VertexToPixelData
      {
        float4 position : SV_POSITION;
        float2 uv : TEXCOORD0;
      };

      VertexToPixelData VertexShaderFunction(VertexData input)
      {
        VertexToPixelData output;
        output.position = UnityObjectToClipPos (input.position);
        output.uv = input.uv;
        return output;
      }
    
      sampler2D _MainTex;
      float3 _TransparentColorKey;
      float _TransparencyMargin;

      float4 PixelShaderFunction(VertexToPixelData input) : SV_Target
      {
        float4 color = tex2D(_MainTex, input.uv);
      
        float deltaR = abs(color.r - _TransparentColorKey.r);
        float deltaG = abs(color.g - _TransparentColorKey.g);
        float deltaB = abs(color.b - _TransparentColorKey.b);

        if (deltaR < _TransparencyMargin && deltaG < _TransparencyMargin && deltaB < _TransparencyMargin)
        {
          return float4(0.0f, 0.0f, 0.0f, 0.0f);
        }

        return color;
      }
      ENDCG
    }
  }
}
Copy the code

C#:

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

public class TransparentWindow : MonoBehaviour
{
    [SerializeField] private Material m_Material;

    private struct MARGINS
    {
        public int cxLeftWidth;
        public int cxRightWidth;
        public int cyTopHeight;
        public int cyBottomHeight;
    }

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

    [DllImport("user32.dll")]
    private static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);

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

    [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
    private static extern int SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter, int x, int y, int cx, int cy,
        int uFlags);

    [DllImport("user32.dll")]
    static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);

    [DllImport("user32.dll", EntryPoint = "SetLayeredWindowAttributes")]
    static extern int SetLayeredWindowAttributes(IntPtr hwnd, int crKey, byte bAlpha, int dwFlags);

    [DllImport("User32.dll")]
    private static extern bool SetForegroundWindow(IntPtr hWnd);

    const int GWL_STYLE = -16;
    const int GWL_EXSTYLE = -20;
    const uint WS_POPUP = 0x80000000;
    const uint WS_VISIBLE = 0x10000000;

    const uint WS_EX_TOPMOST = 0x00000008;
    const uint WS_EX_LAYERED = 0x00080000;
    const uint WS_EX_TRANSPARENT = 0x00000020;

    const int SWP_FRAMECHANGED = 0x0020;
    const int SWP_SHOWWINDOW = 0x0040;
    const int LWA_ALPHA = 2;

    private IntPtr HWND_TOPMOST = new IntPtr(-1);

    private IntPtr _hwnd;

    void Start(){#if! UNITY_EDITOR MARGINS margins =new MARGINS() { cxLeftWidth = -1 };
    _hwnd = GetActiveWindow();
    int fWidth = Screen.width;
    int fHeight = Screen.height;
        SetWindowLong(_hwnd, GWL_STYLE, WS_POPUP | WS_VISIBLE);
        //SetWindowLong(_hwnd, GWL_EXSTYLE, WS_EX_TOPMOST | WS_EX_LAYERED | WS_EX_TRANSPARENT); // If you want to mouse through, you can restore the comment
        DwmExtendFrameIntoClientArea(_hwnd, ref margins);
        SetWindowPos(_hwnd, HWND_TOPMOST, 0.0, fWidth, fHeight, SWP_FRAMECHANGED | SWP_SHOWWINDOW); 
        ShowWindowAsync(_hwnd, 3); //Forces window to show in case of unresponsive app // SW_SHOWMAXIMIZED(3)
#endif
    }

    void OnRenderImage(RenderTexture from, RenderTexture to)
    {
        Graphics.Blit(from, to, m_Material); }}Copy the code

Second, specific steps

1. Create a Material and select the Shader you created:2. Hang the C# script you just wrote on the camera, select Solod Color in Clear Flags mode for the camera, and hang the material ball you just created on the script.3. The Background of the camera should be consistent with the Transparent Color Key of the material ball:4. One point to note here is that the version of Unity19 or above needs to set something. Will this in playerSetting UseDXGIFlipModelSwapchainforD3D11 unchecking.

To make the window transparent, add a model and action to the scene