A small example

Imagine you want to have thousands of similar items in your scene. This is not a strange request; there may be many valid reasons for that: you may wish to render thousands of ships in a gigantic galactic battle, or you may want to animate thousands of units for a Real-Time Strategy (RTS) game, or you may want to handle a massive number of particles.

For simplicity, in our demo, we want a scene with 10,000 spinning cubes. So let's begin:

  1. Each cube will have a single MonoBehaviour instance that executes a very simple rotation cube:
using UnityEngine;

namespace Classic
{
public class Rotator : MonoBehaviour
{

public float rotationSpeed;

void Update()
{
transform.Rotate(0f, rotationSpeed * Time.deltaTime, 0f);
}
}
}

The script is self-explanatory: we have a public variable, rotationSpeed, storing the rotation speed of the cube. Then, in Update, we simply rotate the cube.

  1. Now, we do not want to insert 10,000 cubes into the scene manually. So, we will create a game manager that will do the following:
    1. Spawn 10,000 cubes in the scene
    2. Set a random rotation speed for each one of them
  1. So, we create an empty GameObject, and we attach to it a game manager script, as follows:
using UnityEngine;
using System;

namespace Classic { 

    public class ClassicCubeManager : MonoBehaviour
    {

        #region COMMON_GAME_MANAGER_DATA
        public float cubeSpacing = 0.1f;
        public int width = 10;
        public int height = 10;

        public GameObject cubePrefab;
        #endregion

        void Start()
        {
            SpawnCubes();

        }

        private void SpawnCubes()
        {
            Debug.Log(String.Format("Spawning {0} cubes", (width / cubeSpacing) * (height / cubeSpacing)));
            Vector3 position = new Vector3();
            while (position.x < width)
            {
                while (position.y < height) {
                    var newCube = GameObject.Instantiate(cubePrefab);
                    newCube.transform.position = position;
                    newCube.GetComponent<Rotator>().rotationSpeed = UnityEngine.Random.Range(25.0f, 50.0f);
                    position = new Vector3(position.x, position.y + cubeSpacing, 0f);
                }
                position = new Vector3(position.x + cubeSpacing, 0f, 0f);
            }
        }

    }
} 

The script simply takes a cubePrefab and spawns a certain number of them into a width x height rectangle of space. The exciting part is the SpawnCubes function. The function starts with position in the origin and starts spawning cubes until we reach the opposite corner. This is a pretty standard script.

  1. Now we can run it, and we should see something like this:

As you can see, the frame rate is not optimal. Looking at the stats in the top-right corner, you can see that the game is running at ~22 FPS.

Note that these values are taken from my non-optimal machine. You may find different values. If your computer is so fast that the demo is running perfectly, try to increase the number of cubes to 20,000 or more.
  1. This FPS value is not optimal. However, we can open the Profiler window (Window | Analysis | Profiler) and try to understand how the application behaves:

The image is clear: we are allocating almost 1 GB of RAM and spending 45 ms per frame, 10 ms of which are used just for the scripts. That's wrong. The update script is straightforward: we are just rotating a cube by a few degrees each frame.

We should do better. And we will.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.6.77