日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Unity制作自适应透明背景(PC端)

發布時間:2023/12/10 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Unity制作自适应透明背景(PC端) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

實現步驟:

1.設置MainCamera

2.添加C#腳本

一、設置MainCamera里的Camera組件,以下圖為例子

關鍵點在于 :

MainCamera->Camera->Clear Flags:Solid Color

MainCamera->Camera->Background:black

二、新建C#腳本,編寫代碼如下(HyalineBackground.cs)

using UnityEngine; using System; using System.Runtime.InteropServices; //MainCamera->Camera->Clear Flags:Solid Color //MainCamera->Camera->Background:black public class HyalineBackground : MonoBehaviour {public string strProduct;//項目名稱private int currentX;private int currentY;#region Win函數常量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")]static extern IntPtr FindWindow(string lpClassName, string lpWindowName);[DllImport("user32.dll")]static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);[DllImport("user32.dll")]static extern int GetWindowLong(IntPtr hWnd, int nIndex);[DllImport("user32.dll")]static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);[DllImport("user32.dll")]static extern int SetLayeredWindowAttributes(IntPtr hwnd, int crKey, int bAlpha, int dwFlags);[DllImport("Dwmapi.dll")]static extern uint DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margins);[DllImport("user32.dll")]private static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);//private const int WS_POPUP = 0x800000;private const int GWL_EXSTYLE = -20;private const int GWL_STYLE = -16;private const int WS_EX_LAYERED = 0x00080000;private const int WS_BORDER = 0x00800000;private const int WS_CAPTION = 0x00C00000;private const int SWP_SHOWWINDOW = 0x0040;private const int LWA_COLORKEY = 0x00000001;private const int LWA_ALPHA = 0x00000002;private const int WS_EX_TRANSPARENT = 0x20;#endregionIntPtr hwnd;void Awake(){#if UNITY_EDITORprint("unity內運行程序"); #elsehwnd = FindWindow(null, strProduct);int intExTemp = GetWindowLong(hwnd, GWL_EXSTYLE);SetWindowLong(hwnd, GWL_EXSTYLE, intExTemp | WS_EX_TRANSPARENT | WS_EX_LAYERED);SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_BORDER & ~WS_CAPTION);currentX = 0;currentY = 0;SetWindowPos(hwnd, -1, currentX, currentY, Screen.currentResolution.width, Screen.currentResolution.height, SWP_SHOWWINDOW);var margins = new MARGINS() { cxLeftWidth = -1 };DwmExtendFrameIntoClientArea(hwnd, ref margins); #endif} }

在Hierarchy視圖新建空物體,添加上面建立的腳本組件即可

三、注意代碼設置效果無法體現在編譯器模式,將代碼打包查看效果

注意Player Setting->Resolution and Presentation->Standalone Player Options->Use DXGI Filp Model Swapchain for D3D1為false

此文參考過多篇博客,可以說取其精華,實驗過許多次

最后,如若項目有任何問題,歡迎批評指正或討論。

總結

以上是生活随笔為你收集整理的Unity制作自适应透明背景(PC端)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。