我嘗試在Unity3D(以後與它進行交互)中將計算機遊戲中的桌面流式傳輸到平面上。我開始了屏幕截圖,但是一分鐘後,統一遊戲就已經滿足了我個人電腦的內存(〜30GB)。在Unity3D平面的桌面屏幕(RAM溢出)
我已經嘗試通過手動調用垃圾收集器來清除它,但調用它似乎沒有改變任何東西。
我還在Visual Studio中用Windows窗體編寫了本地C#中的測試應用程序。在C#測試應用程序中,代碼行爲良好,不會氾濫RAM。
Unity有沒有可能知道如何釋放RAM?爲了讓這段代碼正常工作,我必須將System.Drawing.dll
添加到我的Unity遊戲中。
如果這種方法無法正常工作,還有其他選項可用於流式傳輸桌面並在Unity平面上顯示它嗎?
這裏是我當前的代碼:
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading;
using UnityEngine;
public class ScreenCap : MonoBehaviour {
public Renderer renderer;
public MemoryStream stream;
// Use this for initialization
void Start() {
renderer = GetComponent<Renderer>();
startScreenCaptureThread();
}
// Update is called once per frame
void Update() {
Texture2D tex = new Texture2D(200, 300, TextureFormat.RGB24, false);
if (stream != null)
{
tex.LoadImage(stream.ToArray());
renderer.material.mainTexture = tex;
stream.Dispose();
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
}
}
public void startScreenCaptureThread()
{
Thread t = new Thread(ScreenCapture);
t.Start();
}
public void ScreenCapture()
{
Rectangle screenSize;
Bitmap target;
MemoryStream tempStream;
while (true)
{
System.Diagnostics.Process proc = System.Diagnostics.Process.GetCurrentProcess();
Debug.Log(proc.PrivateMemorySize64);
screenSize = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
target = new Bitmap(screenSize.Width, screenSize.Height);
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(target))
{
g.CopyFromScreen(0, 0, 0, 0, new Size(screenSize.Width, screenSize.Height));
}
tempStream = new MemoryStream();
target.Save(tempStream, ImageFormat.Png);
tempStream.Seek(0, SeekOrigin.Begin);
stream = tempStream;
target.Dispose();
tempStream.Dispose();
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
}
}
}
您可以嘗試將Texture2D,Rectangle,Bitmap和MemoryStream變量的初始化移出方法。 我也不確定Unity中的Threading。我認爲使用協程會更好。 – Woltus
我正在嘗試在我的VR項目中添加一個虛擬桌面,並發現使用CopyFromSreen在性能方面遜色,並且在30fps的條件下完全利用1個CPU內核。看起來更好的方法是使用像SharpDX這樣的DirectX庫,或者像https://github.com/Phylliida/UnityWindowsCapture(不知道它是如何工作的)不要運行 –