實作步驟:
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;
#endregion
IntPtr hwnd;
void Awake()
{
#if UNITY_EDITOR
print("unity內運行程式");
#else
hwnd = 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

此文參考過多篇博客,可以說取其精華,實驗過許多次
最后,如若專案有任何問題,歡迎批評指正或討論,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/339278.html
標籤:其他
上一篇:云原生愛好者周刊:玩 Kubernetes 游戲,贏取免費機票
下一篇:手機游戲游戲隱私政策
