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.
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.
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
//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 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?
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.
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.
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.
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.