Is this efficient? Help me optimize if not please.

    private void generateFloor()
    {
        for (int i = 0; i < floorHeight; i++)
        {
            for (int j = 0; j < floorWidth; j++)
            {
                Vector3 offset = new Vector3(j, i, 0);
                var newTile = Instantiate(getRandomTile(), getStartCords() + offset, Quaternion.identity, transform);
                newTile.name = "Tile_" + count;
                count++;
            }
        }
    }

/Title

I use this to generated a floor with random tiles (Which are just different shades of the same thing)
It works perfectly, but i wanna know if this is an efficient way of handling the situation.

That looks fine to me. What do your getRandomTile() and getStartCords() methods look like?

Also, not to nit-pick but getStartCords() should probably be spelled getStartCoords()

1 Like

Does this happen only once per level? Because if it’s the case then I’d like to draw your attention to the fact that, since it will only happen once, its’ inefficiency bears little relevance.

I don’t see anything that would stand out as inefficient here, this excerpt of code is too small to be massively faulty. There may possibly be perverse ways of making it more efficient, but this should be good enough.

2 Likes

Having every tile be a separate object is not efficient, no.

–Eric

2 Likes

The best partner you can ask, whether something is efficient, is a Profiler. Once you’re familiar using a Profiler, there is no need to guess or ask these type of questions anymore, because you can use such Profiler to measure performance.

Here are a few videos that profile help to get started with the Unity Profiler.

Introduction to the Profiler - Unity Official Tutorials

Unite Europe 2017 - Practical guide to profiling tools in Unity

Unite Europe 2017 - Performance optimization for beginners

1 Like

unfortunately I am just learning, so I don’t have the pro version. But thanks for commenting and sharing knowledge regardless.

 //Gets the coordinates of Bottom left corner
    private Vector3 getStartCoords() {
        return new Vector3(-(floorWidth / 2f - 0.5f),-(floorHeight / 2f - 0.5f),0);
    }
    //Gets a random tile sprite for a lively floor
    private GameObject getRandomTile()
    {
        int index = Random.Range(0, tileSet.Length);
        return tileSet[index];
    }

i fixed coords thanks for the heads up :slight_smile:

I honestly can’t think of any other way to generate custom height x width floor without having every tile be aa separate object…is there a way to combine all the tiles into one gameobject after the code is done instantiating?

Don’t instantiate objects, use the Mesh class.

–Eric

1 Like

I am kinda new, would you mind giving more details, you don’t have to do a step by step guide, maybe link me to a tutorial if there is one. Thanks for helping.

You don’t need the Pro version to use the Profiler. It’s under Window → Profiler.

3 Likes

You sir just don’t know how much this is gonna help me. Thank You from the bottom of my heart.

1 Like

Profiler has been part of personal for a couple of years now. The only features you don’t get are cosmetic, namely dark skin and removing the splash screen.

Do you actually need it?

@Eric5h5 is certainly right, using a single GameObject for the floor is much more efficient then using a thousand tiles. But it will take you more work to understand, develop, and code. If an inefficient system performs well enough there is often merit to simply moving on.

If you are actually having performance problems, I would start with the Mesh class. The basic idea is to build a single mesh for your floor, and have the uvs point to different spots on a single tile texture.

1 Like

You Dont get cloud services with out a plus or pro license (Cloud Build, Exception Reporting, Ads, Analytics etc)

But as far as the player and editor goes yeah, the only difference is splash screen and editor themes

1 Like

You get some of them. Cloud Build, Ads, Analytics, Collaborate and MultiPlayer all work on personal. Performance reporting doesn’t.

The personal services are slightly limited, but you really don’t really need the plus/pro level services until your game is deployed and making money.

1 Like

Thanks for all the help guys, I have done some research with the profiler and it seems that the game is running very smooth, just a bit of fps drops during generation (but since all of this is gonna be done at the beginning of each level) i have decided to just go with it hopefully I won’t run into problems later.

Again Thank you everyone for the great help.