首页 > 分享 > Unity实现桌面宠物之透明效果和直接进入程序

Unity实现桌面宠物之透明效果和直接进入程序

Unity实现桌面宠物之透明效果

注意:只适合windows平台

直接贴代码吧,其实这些代码也不是我自己写的。使用的是网上的。谢谢前辈们的
踩坑。

实现的思路是这样的,将相机给成纯色,然后进行抠图。具体是怎么回事我
也不是很清楚。如果有明白的,还希望能教我一下。

using System.Collections; using System.Collections.Generic; using System.Runtime.InteropServices; using System; using UnityEngine; using System.Diagnostics; public class WindowManager : MonoBehaviour { private static WindowManager instance; public static WindowManager Instance { get { return instance; } } // Use this for initialization [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); public delegate bool WNDENUMPROC(IntPtr hwnd, uint lParam); [DllImport("user32.dll", SetLastError = true)] public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, uint lParam); [DllImport("user32.dll", SetLastError = true)] public static extern IntPtr GetParent(IntPtr hWnd); [DllImport("user32.dll")] public static extern uint GetWindowThreadProcessId(IntPtr hWnd, ref uint lpdwProcessId); [DllImport("kernel32.dll")] public static extern void SetLastError(uint dwErrCode); // Definitions of window styles const int GWL_STYLE = -16; const uint WS_POPUP = 0x80000000; const uint WS_VISIBLE = 0x10000000; public const int width = 300; public const int height = 300; void Start() { SetWindowScreen(width, height); var margins = new MARGINS() { cxLeftWidth = -1 }; var hwnd = GetProcessWnd(); SetWindowLong(hwnd, GWL_STYLE, WS_POPUP | WS_VISIBLE); DwmExtendFrameIntoClientArea(hwnd, ref margins); } void SetWindowScreen(int width, int height) { Screen.SetResolution(width, height, false); } void OnRenderImage(RenderTexture from, RenderTexture to) { Graphics.Blit(from, to, m_Material); } public static IntPtr GetProcessWnd() { IntPtr ptrWnd = IntPtr.Zero; uint pid = (uint)Process.GetCurrentProcess().Id; // 当前进程 ID bool bResult = EnumWindows(new WNDENUMPROC(delegate (IntPtr hwnd, uint lParam) { uint id = 0; if (GetParent(hwnd) == IntPtr.Zero) { GetWindowThreadProcessId(hwnd, ref id); if (id == lParam) // 找到进程对应的主窗口句柄 { ptrWnd = hwnd; // 把句柄缓存起来 SetLastError(0); // 设置无错误 return false; // 返回 false 以终止枚举窗口 } } return true; }), pid); return (!bResult && Marshal.GetLastWin32Error() == 0) ? ptrWnd : IntPtr.Zero; } }

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104

我只是在别人的代码上修改了一下,设置了些别的东西。

将这个脚本挂在到摄像机,但是还需要给它一个材质球,使用shader将背景色该去掉。

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 } } }

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364

注意,材质球的颜色要和相机的背景色设置成一样的。(具体是因为什么我也不是很清楚)

打包成可执行程序之后,双击程序会进入选择分辨率的界面。但是我不想这样,我想双击直接就进入宠物界面。

PlayerSetting->Resolution下,将Display Resolution Dialog设置为Disabled就可以了。

相关知识

使用unity实现桌面宠物
Android全局桌面宠物 Unity方案实现
Unity宠物跟随
[Unity]怎么做个桌面宠物精灵desktop mascot
如何用unity制作一个桌面宠物?
男孩桌面小宠物快来玩吧
java开发桌面宠物
手机桌面宠物app下载
java桌面宠物swing实现
Unity 制作萌系live2d桌宠:屏幕自适应+交互

网址: Unity实现桌面宠物之透明效果和直接进入程序 https://m.mcbbbk.com/newsview725996.html

所属分类:萌宠日常
上一篇: 桌面蟑螂宠物下载
下一篇: 【桌面宠物大全】最好的桌面宠物下