Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity spawning lots of objects at runtime running slow

Tags:

unity3d

I've created a simple project where approx. 7000 cubes are created in the scene (with VR camera) but the problem is that when I move camera to see all cubes the FPS becomes very bad, something like 5-6 frames. My PC is i7 with GTX 1070, and I thought that it have to draw hundred thousands of cubes without any problems. Really, I saw minecraft, it looks like there no problems to draw cubes ))

So question is is it possible to optimize the scene so that all cubes are painted it one call or something to provide a good performance?

I'm actually made all cubes static and there are no textures only standard material...

Here is how it looks now: enter image description here

I'm using default Directional Light and It would be good to not change the standard shader because of it great work with light.

Here is how I'm generating the cubes:

private void AddCube(Vector3 coords)
{
    var particle = (Transform)MonoBehaviour.Instantiate(prototype.transform, holder.transform);

    SetScale(particle);
    SetPosition(particle, coords);

    cubes.Add(particle.gameObject);
    particle.gameObject.isStatic = true;

}

private void SetScale(Transform particle)
{
    particle.localScale = new Vector3(Scale, Scale, Scale);
}

private void SetPosition(Transform particle, Vector3 coords)
{
    particle.position = coords;
}

This is the screenshot of Stats: Stats screenshot I have here 41 fps because I moved camera away from cubes to have clean background for the stats panel. Actually after I'm making the cubes 'static' the FPS is depends on are the cubes visible on the screen or not.

like image 554
Vetal Avatar asked Feb 15 '17 10:02

Vetal


People also ask

Is instantiate slow unity?

One thing we can see in the profiler is that Instantiate and Destroy methods are slow. So when they are called often after some time, the garbage collector has to clear resources which causes a lot of performance drop and profiler line show spike on this time.

How does unity instantiate work?

Instantiate can be used to create new objects at runtime. Examples include objects used for projectiles, or particle systems for explosion effects. Instantiate can also clone script instances directly. The entire game object hierarchy will be cloned and the cloned script instance will be returned.

What is spawn manager in unity?

In the network HLAPI the word “spawn” is used to mean something more specific. In the server authoritative model of the network HLAPI, to “spawn” an object on the server means that the object should be created on clients connected to the server, and the object will be managed by the spawning system.

How do you spawn objects in unity?

To spawn a prefab, simply drag the object that you want to create from your project's assets to a public game object reference and then pass that into the Instantiate function. To create a specific object, drag the prefab you want to instantiate into the game object reference on the script.


1 Answers

The problem is most likely caused by number of individual objects you are instantiating. If cubes doesnt change their transform after generating you should be able to use StaticBatchingUtility to combine them into one batch.

Call this after generating cubes.

StaticBatchingUtility.Combine(GameObject cubesRoot);
like image 113
Greg Lukosek Avatar answered Sep 21 '22 18:09

Greg Lukosek