Unity制作自适应透明背景(PC端)
生活随笔
收集整理的這篇文章主要介紹了
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端)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 单片机平台的最小偏差圆弧插补算法
- 下一篇: 浅谈高斯消元的实现和简单应用