Exemple #1
0
    // Very slow function that should be executed in a separated thread.
    void loadTerrain()
    {
        Debug.Log("Start load terrain");
        // here maybe you can load from file some data
        // and operate with it ...

        // For this example I introduce a sleep in order to
        // made the function slow.
        for (int i = 0; i < 10; i++)
        {
            Thread.Sleep(1000);
            Debug.Log("load terrain :: loading ...");
        }

        Debug.Log("load terrain :: end load resources");

        // now maybe you want to modify the current scene with the data
        // you operated with before, but you can't execute Unity functions
        // directly in a child thread. You can use the ThreadPoolExecutor.
        threadPool.AddMainThreadAction(
            () => { // this add an anonymous function to the threas pool
            // for this example, this function adds a plane as a terrain
            GameObject plane         = GameObject.CreatePrimitive(PrimitiveType.Plane);
            plane.transform.position = new Vector3(0, -0.6f, 0);
            plane.transform.parent   = gameObject.transform.parent;
        }
            );
    }