我之前曾问过这个问题,但没有提供代码,因为我没有简单的方法来做到这一点。然而现在我在 Unity 中启动了一个新项目,并尝试复制该行为,而不附加所有不必要的包袱。
这是我当前的设置:
public class Main : MonoBehaviour
{
public GameObject calculatorPrefab;
void Start ()
{
for (int i = 0; i < 10000; i++)
{
Instantiate(calculatorPrefab);
}
}
}
public class Calculator : MonoBehaviour
{
void Start ()
{
ThreadPool.QueueUserWorkItem(DoCalculations);
}
void DoCalculations(object o)
{
// Just doing some pointless calculations so the thread actually has something to do.
float result = 0;
for (int i = 0; i < 1000; i++)
{
// Note that the loop …Run Code Online (Sandbox Code Playgroud)