Unityフォームの透明性

5245 ワード

文章はゲームから転じた蛮牛:http://www.manew.com/forum.php?mod=viewthread&tid=103087&_dsign=ec6d9a3d
たまたま海外のウェブサイトで見たスクリプトで、純色の背景を削る方法でwindowsのフォームを透明化し、背景のない小さな顔やデスクトップの精霊などに使うことができます.
まずTransparentWindowというCSスクリプトを新規作成し、次のコードを入力します.
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;
    }
 
    // Define function signatures to import from Windows APIs
 
    [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);
 
 
    // Definitions of window styles
    const int GWL_STYLE = -16;
    const uint WS_POPUP = 0x80000000;
    const uint WS_VISIBLE = 0x10000000;
 
    void Start()
    {
#if !UNITY_EDITOR
        var margins = new MARGINS() { cxLeftWidth = -1 };
 
        // Get a handle to the window
        var hwnd = GetActiveWindow();
 
        // Set properties of the window
        // See: https://msdn.microsoft.com/en-us/library/windows/desktop/ms633591%28v=vs.85%29.aspx
        SetWindowLong(hwnd, GWL_STYLE, WS_POPUP | WS_VISIBLE);
 
        // Extend the window into the client area
        //See: https://msdn.microsoft.com/en-us/library/windows/desktop/aa969512%28v=vs.85%29.aspx 
        DwmExtendFrameIntoClientArea(hwnd, ref margins);
#endif
    }
 
    // Pass the output of the camera to the custom material
    // for chroma replacement
    void OnRenderImage(RenderTexture from, RenderTexture to)
    {
        Graphics.Blit(from, to, m_Material);
    }
}

ChromakeyTransparentというShaderを新規作成します.
Shader "Custom/ChromakeyTransparent" {
    Properties{
        _MainTex("Base (RGB)", 2D) = "white" {}
        _TransparentColourKey("Transparent Colour Key", Color) = (0,0,0,1)
        _TransparencyTolerance("Transparency Tolerance", Float) = 0.01
    }
 
    SubShader{
        Pass{
            Tags{ "RenderType" = "Opaque" }
            LOD 200
 
            CGPROGRAM
 
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
 
            struct a2v
            {
                float4 pos : POSITION;
                float2 uv : TEXCOORD0;
            };
 
            struct v2f
            {
                float4 pos : SV_POSITION;
                float2 uv : TEXCOORD0;
            };
 
            v2f vert(a2v input)
            {
                v2f output;
                output.pos = UnityObjectToClipPos(input.pos);
                output.uv = input.uv;
                return output;
            }
 
            sampler2D _MainTex;
            float3 _TransparentColourKey;
            float _TransparencyTolerance;
 
            float4 frag(v2f input) : SV_Target
            {
                // What is the colour that *would* be rendered here?
                float4 colour = tex2D(_MainTex, input.uv);
 
                // Calculate the different in each component from the chosen transparency colour
                float deltaR = abs(colour.r - _TransparentColourKey.r);
                float deltaG = abs(colour.g - _TransparentColourKey.g);
                float deltaB = abs(colour.b - _TransparentColourKey.b);
 
                // If colour is within tolerance, write a transparent pixel
                if (deltaR < _TransparencyTolerance && deltaG < _TransparencyTolerance && deltaB < _TransparencyTolerance)
                {
                    return float4(0.0f, 0.0f, 0.0f, 0.0f);
                }
 
                // Otherwise, return the regular colour
                return colour;
            }
            ENDCG
        }
    }
}

新しいマテリアルを作成し、作成したばかりのShaderを選択し、TransparentWindowをカメラにマウントします.カメラのClear FlagsはSolid Color、BackgroundはマテリアルのTransparent Color Keyと同じ色を選択します(モデルのエッジの色に近い色を選択することをお勧めします.そうしないと、明らかなエッジが表示されます).TransparentWindowのMaterial変数にマテリアルをドラッグします.



実行後、背景色が削除されていることがわかります(黒は透明です):

Buildを見ると、デスクトップの精霊のようなプログラムが見えます.