这是我参加更文应战的第10天,活动概略检查: 更文应战

看到网上有用Unity做的桌面小宠物,就自己搜了些材料自己做了一个小Demo。

详细过程如application下:

简单说一下思路,有一个脚本跟一个Shader,通过脚本和Shader担任将Unity运行时的布approach景调成透明色。这个是通过调色彩来进行了。详细原理也不清楚,查了查材料大概是这application

一、首先是代码部分:

shader:

Shadeappearancer "Custom/MakeTransparent" {
Properties {
_MappearanceainTex ("Base (RGB)", 2D) = "white" {}
_TransparentCappearolorKey ("Transpapplearent Color Key", Color) = (0,1,0,1)
_TransparencyMargin ("Transparency Margin", Float) = 0.01
}
SubShader {
Pass {
Tags { "RenderType"apple="Opaque" }
LOD 200
CGPROGRAM
#pragma vertex VertexShaderFunction
#pragma fragment PixelShaderAPPFunction
#include "UnityCG.cginc"
struct VertexDaappointmentta
{
float4 position : POSITION;
float2 uv : TEXCOORD0;
};
struct VertexToPixelData
{
float4 position : SV_POSITION;
float2 uv : TEXCOORD0;
};
VertexToPixelData VerapproachtexShaderFunction(VertexData input)
{
VertexToPixelData outpuapp下载t;
output.position = UnityObjectToClipPos (input.pappearosition);
output.uv = input.uv;
return output;
}
sampler2D _MainTex;
float3 _TransparentColoapplerKey;
float _TransparencyMargin;app下载
float4appreciate PixelShaderFunction(VertexToPixelDaappreciateta input) : SV_Target
{
float4 color = tex2D(_MainTex, input.uv);
float deltaRapplication = abs(color.r - _TransappleparentColorKey.r);
float deltaG = abs(color.g - _TransparentColorKey.g);
float deltaB = abs(coapp是什么意思lor.b - _TranspappeararentColorKey.b);
if (dapplicationeltaR < _TransparencyMargin &&aapplemp; deltaG < _TransparencyMargin && deltaapp是什么意思B &approachlt; _TransparenappointmentcyMargin)
{
returnapp是什么意思 float4(0.0f, 0.0f, 0.0f, 0.0f);
}
return color;
}
ENDCG
}
}
}

C#:

using Systeappearancem;
using System.Runtime.InteropServices;
using UnityEngine;
public cappreciatelassappointment TransparentWindapproachow : MonoBehapp是什么意思aviour
{
[SeriapplealappointmentizeField] private Materialappreciate m_Material;
private struct MARGINS
{
public int cxLeftWidth;
public int cxapp是什么意思RightWidth;
public int cyTopHeight;
public int cyBottomHeight;
}
[DllImport("user32appearance.dll")]
private static extern IntPtr GetActiveWindow();approach
[DllImport("user32.dll")]
private static extern int SetappleWindowLong(IntPtr hWnd, int nIndex, uint dwNeappstorewLong);
[DllappleImport("DwmapAPPi.dll")]
private static extern uint DwmExtendFrameIntoClientArea(IntPtr hWndapp下载, ref MARGINS margins);
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
private static exteappreciatern intappstore SetappleWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter, int x, int y,APP int cx, intapproach cy,
int uFlags);
[DllImport("userappointment32.dll")]
static extern bool ShowWindowAsync(IntPtr hWnd, int nappearanceCmdShow);
[DllImport("user32.dll", EntryPoint = "SetLayeredWindowAttributes")]
stapp是什么意思atic extern int SetLayeredWindowapplicationAttribapp是什么意思utes(IntPtr hwnd, int crKey, byte bAlpha, int dwFlags);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
const int GWLappointment_STYLE = -16;
const int GWL_EXSTYLE = -20;
const uappointmentint 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;
papp下载rivate IntPtr HWND_TOPMOST = new IntPtr(-1);
private IntPtr _hwndapproach;
void Start()
{
#if !UNITY_EDITOR
MARGINS margins = new MARGINS() { cxLeftWidth = -1 };
_hwnd = GetActiveWindow();
int fapp是什么意思Width = ScAPPreenapp下载.width;
int fHeight = Screen.height;
SetWindowLong(apple_hwnd, GWL_STappstoreYLE, WS_POPUP | WS_VISIBLE);
//SeapplicationtWindowLong(_hwnd, GWL_EXSTYLE, WS_EX_TOappearancePMOST | WS_EX_LAYERED | WS_EX_TRANSPARENT);//若想鼠标穿透,则将这个注释康复即可
DwmExtendFrameIntoClientArea(_hwnd, ref margins);
SetWindowPos(_hwnd, HWND_TOPMOST, 0, 0, fWidth, fHeight, SapproachWP_FRAMECHANGED | SWP_SHOWWINDOW);
ShowWindappearanceowAsync(_hwnd,approach 3); //Forces window to show in case of unresponsive app    // SW_SHOWMAXIMIZED(3)
#endif
}
void OnRenderImage(Rendeapp是什么意思rTexture fromAPP, RenderTexture to)
{
Graphics.Blit(from, to, m_Material);
}
}

二、详细过程

1.新建一个material(材料球),选择刚建立的Shader:
Unity -Demo 之  ✨桌面小宠物
2.将刚写的C#脚本挂在摄像机上,摄像机的Clapplicationear Flags形式选择Solod Color,并将刚建立的材料球挂在脚本上。
Unity -Demo 之  ✨桌面小宠物
3.摄像机的Backappointmentground特色要和质appearance料球的Transparent Coloapproachr Key特色一起:
Unity -Demo 之  ✨桌面小宠物
4.这儿要注意一个点,unity19以上的版别需求设置一个东西。在playerSetting中将这个UseDXGIFlipModelSwapchainforD3D11撤消勾APP选。
Unity -Demo 之  ✨桌面小宠物

这样appreciate就可以完成窗口透明晰,然后在场景里加一个模型跟动作就好啦