Question by
valentin56610 · Apr 04, 2020 at 11:57 AM ·
instantiateinstantiate prefabtilesmany
Instantiating 90.000 gameObjects (very simple) takes forever and freeze unity
Hi! So I am making a WWII strategy hex game, and I am making a map of Europe (300x300) and when loading or even creating the map, unity will freeze and it is taking around 3 minutes to create the map. I don't understand how it can be SO long to create just 90.000 simple tiles. Also, it does not even really depend on the hardware because I made my brother generate a map of the same size with his high-end PC and it took the same amount of time. I want to add that the script TileGO that every tile has, doesn't have ANY Start() method or even an Update(). And I would like to add, that I have like 1FPS in game when looking at the 90 000 tiles, is that normal ? when I come closer, it is totally fine. Here is my code :
GameObject newTile;
for (int x = 0; x < GameData.GetInstance().map.SizeX; x++)
{
for (int y = 0; y < GameData.GetInstance().map.SizeY; y++)
{
// LOADING EXISTING MAP
if (MapEditorMenu.isLoadedMapEditor)
{
// Check what version of the tile needs to be instantiated
if (GameData.GetInstance().map.TilesTable[x, y].IsWinterTile)
season = "Winter/";
else
season = "Summer/";
// instantiate the prefab
newTile = Instantiate(Resources.Load("Prefabs/Tiles/" + season + GameData.GetInstance().map.TilesTable[x, y].Name) as GameObject);
// change its material, because in game i added 2D light, but i don't want to bother with it in th editor for performances reasons
newTile.GetComponent<SpriteRenderer>().material = BaseTileMaterial;
//change the GO's name to its coordinates to recover it faster
newTile.name = x + " " + y.ToString();
// add the tileEditorGO component
newTile.AddComponent<TileEditorGO>();
// add the tile in unity to my mapData used for saving
newTile.GetComponent<TileEditorGO>().tile = GameData.GetInstance().map.TilesTable[x, y];
if (GameData.GetInstance().map.TilesTable[x, y].IsVictoryPoint)
{
// Caching the victory points to reuse later
listOfAllVps.Add(newTile.GetComponent<TileEditorGO>());
// set the city name
if (GameData.GetInstance().map.TilesTable[x, y].CityName != null)
{
GameObject prefab = Resources.Load<GameObject>("CityNames/BaseCityNameHolder");
GameObject cityNameGO = Instantiate(prefab);
cityNameGO.GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("CityNames/" + newTile.GetComponent<TileEditorGO>().tile.CityName);
cityNameGO.name = GameData.GetInstance().map.TilesTable[x, y].CityName;
cityNameGO.transform.SetParent(newTile.transform);
cityNameGO.transform.position = new Vector3(newTile.transform.position.x, newTile.transform.position.y - 2.3f, newTile.transform.position.z - 1.1f);
}
// Add the owner's flag (Germany, USSR ...)
selectedPlacedTile = newTile;
foreach (Player player in GameData.GetInstance().listOfPlayers)
if (GameData.GetInstance().map.TilesTable[x, y].Owner == player.Name)
SetflagForVP(player.Country);
}
// Destroy the original component, because this is the component I use for
Destroy(newTile.GetComponent<TileGO>());
// spawning the ground unit on the air unit if there is one
if (GameData.GetInstance().map.TilesTable[x, y].AirUnit != null)
SpawnTheUnit(GameData.GetInstance().map.TilesTable[x, y].AirUnit, newTile);
// spawning the ground unit on the tile if there is one
if (GameData.GetInstance().map.TilesTable[x, y].GroundUnit != null)
SpawnTheUnit(GameData.GetInstance().map.TilesTable[x, y].GroundUnit, newTile);
}
// CREATING A NEW MAP :
else
{
newTile = Instantiate(baseEditorTileSummer) as GameObject;
newTile.name = x + " " + y.ToString();
newTile.GetComponent<TileEditorGO>().tile = new Tile();
GameData.GetInstance().map.TilesTable[x, y] = newTile.GetComponent<TileEditorGO>().tile;
GameData.GetInstance().map.TilesTable[x, y].Name = "Plain";
}
// assign the tile's parent
newTile.transform.SetParent(parent.transform);
// place the tile on the map
if (x % 2 != 0)
newTile.transform.position = new Vector2(x * 4.4f, y * 5.1f);
else
newTile.transform.position = new Vector2(x * 4.4f, y * 5.1f + 2.55f);
}
}
Comment