Unity 3D Developer Question: Download Unity 3D Developer PDF

Tell me can Threads Be Used To Modify A Texture On Runtime?
Can Threads Be Used To Move A Gameobject On The Scene?
Consider The Snippet Below:
Class Randomgenerator : Monobehaviour
{
Public Float[] Randomlist;

Void Start()
{
Randomlist = New Float[1000000];

}

Void Generate()
{
System.random Rnd = New System.random();
For(int I=0;i }
}
Improve This Code Using Threads, So The 1000000 Random Number Generation Runs Without Spoiling Performance.?

Tweet Share WhatsApp

Answer:

No. Texture and Meshes are examples of elements stored in GPU memory and Unity doesn't allow other threads, besides the main one, to make modifications on these kinds of data.

No. Fetching the Transform reference isn't thread safe in Unity.

When using threads, we must avoid using native Unity structures like the Mathf and Random classes:

class RandomGenerator : MonoBehaviour
{
public float[] randomList;
void Start()
{
randomList = new float[1000000];
Thread t = new Thread(delegate()
{
while(true)
{
Generate();
Thread.Sleep(16); // trigger the loop to run roughly every 60th of a second
}
});
t.Start();
}
void Generate()
{
System.Random rnd = new System.Random();
for(int i=0;i<randomList.Length;i++) randomList[i] = (float)rnd.NextDouble();
}
}

Download Unity 3D Developer PDF Read All 35 Unity 3D Developer Questions
Previous QuestionNext Question
Do you know what Is Fixed Timestep In Unity3d? Why Does Fixed Timestep Setting Affect Game Speed?What is DAU (Daily Active Users)?