I wrote a helper class Thread, which you can use to execute a piece of code in the main Unity theme.
This is the blue print function:
public static void executeInUpdate(System.Action action)
The full script is really long and will make this post unnecessarily long. You can see the rest of the helper class script here .
Then I can use the join API from another Threadas follows:
UnityThread.executeInUpdate(() =>
{
transform.Rotate(new Vector3(0f, 90f, 0f));
});
The problem is that when I use a variable declared outside this delegate, it allocates memory. The code above allocates 104 bytes in each frame. This is because of the variable transformthat is used inside this closure.
, 60 , 6 , . .
, Unity. 60 . receiveVideoFrame() . , Unity main Thread, Unity Texture2D. Texture2D RawImage. , - UnityThread.executeInUpdate.
bool doneUploading = false;
byte[] videoBytes = new byte[25000];
public Texture2D videoDisplay;
void receiveVideoFrame()
{
while (true)
{
downloadVideoFrameFromNetwork(videoBytes);
UnityThread.executeInUpdate(() =>
{
videoDisplay.LoadImage(videoBytes);
doneUploading = true;
});
while (!doneUploading)
{
Thread.Sleep(1);
}
doneUploading = false;
}
}
?
, ?
, , script, .