Need help with managing multiple instances of a prefab.

First of all. My english is not very good, so i’m sorry for the inconveniences. If you need me to explain something again, there’s no problem.

So, here we go:

I have passed the last week experimenting with a celular automata for creating a “minecraft like” grid with hexagonal columns

My problems starts when i instantiate it.

I have a empty game object and all the hexBlocks are their childrens. My first attempt to do a big composition of bloks made me realize that i have a performance isue.

Here you can see how it looks:

And then the troubles start:

4695776--443051--Cap1.png

13 FPS are not enough and more taking into account that we are in an early stage of development. No textures, no complex forms, no animations etc. There is a lot of bathes too. So i start digging into the internet.

The first thing that i tried was too enable GPU instantiating for the material of the hexPilars.

The bathes count improves a lot, but not the FPS count:

4695776--443063--Cap4.png

Next step, combine meshes:

I use the code in the unity manual example (here):

MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>();
        CombineInstance[] combine = new CombineInstance[meshFilters.Length];

        int i = 0;

        while (i < meshFilters.Length)
        {
            combine[i].mesh = meshFilters[i].sharedMesh;
            combine[i].transform = meshFilters[i].transform.localToWorldMatrix;
            meshFilters[i].gameObject.SetActive(false);

            i++;
        }
        transform.GetComponent<MeshFilter>().mesh = new Mesh();
        transform.GetComponent<MeshFilter>().mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
        transform.GetComponent<MeshFilter>().mesh.CombineMeshes(combine);
        transform.gameObject.SetActive(true);

Now the performance is great:
4695776--443066--Cap5.png

But… Now i dont have a collection of interactuable game objets. I have a single game object with the apareance of many.

I need to have multiple objects in order to interact with them, mining it, selecting it, highligting it, and probably more things that i have dont think yet.

And this is the question: ¿How can i get performance in this scenario, and preserve my capacity of work with independent gameobjects?

Thanks in advance.

This seems like a project that’s tailor-made to be run on the Data Oriented Tech Stack that just came out of preview in 2019.1. Check out videos like this one for more information. Long story short, if you’re running large numbers of simple objects, retooling your code to run on DOTS can give you huge performance gains.

1 Like

It is without a doubt a very interesting system, but it seems oriented to the execution of code in multiple threads. In my case the blocks do not have code, the performance failure seems to be caused by having too many meshes in the scene. If there is any way to fix it with DOTS I am not able to see it. I’m right? I’m missing something?

I’m sure there’s a way to deal with your issue, since Unity recently released a MegaCity demo that contains a huge number of objects in the scene, moving simultaneously:

https://unity.com/megacity

Megacity is build on DOTS, though I don’t know whether DOTS has an inherent rendering performance benefits compared to standard game objects. In any case, you can download the Megacity demo, and try to figure out how they’re achieving the framerates they’re getting.

OK, I’ve been trying to try the DOTs system, but it’s only available in preview. All the tutorials that I have followed use features that either I can not install, or they are deprecated or just do not do what they should. On the other hand the system requires unlearning the way in which you work with Unity …

I think that the system, although it is very promising, is not ready even for its implementation, and that it is necessary to let it settle. I will try to find another way to optimize multiple static identical gameobjets …

Thanks for the help.

You’re correct that DOTS is in preview, and requires a very different kind of approach. So it will depend on what your timeline is for this project. If you’re aiming to finish it in the next couple of months, DOTS probably isn’t a safe bet. If you’re planning on working on this over the course of the next year or two, then it seems reasonable to start with it now, but be prepared for it to change and evolve over time. It really depends on how essential the performance gains are to you.

But DOTS aside, maybe that MegaCity project can simply demonstrate to you how they can render so many objects on screen at once with reasonable framerates.

You could write a code that only activates the top most blocks that are seen. When the player mines/removes a block it automatically activate the blocks under and next to it. When the player adds a block the block underneath is deacivated because you can not see it anymore.
All blocks not visible and hidden behind other blocks should be set to inactive.

Some other things to improve performance:
-Also be sure to use Occlusion culling.
-Almost all blocks should share the same material. When they are the same size and same material Unity automatically reduces draw calls.

I’m having a hard time finding time for Unity this week. I downloaded megacity although I have not been able to check it yet. My intention is to first explore the Gor-Sky suggestion that seems the best approach at this time.

I’ll tell you what happened when I can prove it.

Thank you!

Finally I have used the method suggested by Gor-Sky. I have deactivated and combined all hexagons that have the maximum number of neighbors. I have left the rest without changing anything and finally I have achieved a decent FPS rate and I can work with the gameObjects of the extremes, which are actually interactible.

Thank you so much for everything.
4718216--446204--Captura.PNG