- Home /
Performance While Loading Map
Hey everyone!
I'm trying to figure out why my map takes such an amazingly long time to load. I have Unity Basic, so I can't access any of the diagnostic tools I've heard about. But I just added a simple timer to the load, and it takes about 23 seconds to load. My computer isn't a wimp; what am I doing wrong, and how could I optimize this?
function InitialGridLoadup (){
tilePrefab = Resources.LoadAssetAtPath("Assets/Prefabs/Tile/Tile.prefab", Object);
wallPrefab = Resources.LoadAssetAtPath("Assets/Prefabs/Tile/Wall.prefab", Object);
ceilingPrefab = Resources.LoadAssetAtPath("Assets/Prefabs/Tile/Ceiling.prefab", Object);
var timeStamp = Time.time;
Debug.Log(timeStamp);
for ( var x : int = 0; x < mapX; x++ ) {
for ( var z : int = 0; z < mapZ; z++ ) {
// Creates the tile
TileArray[x, z] = new Tile(1, false, 10, x, z);
Debug.Log(x + ", " + z);
}
Debug.Log(Time.time - timeStamp);
yield;
}
Debug.Log(Time.time - timeStamp);
}
class Tile{
var type : int;
var visible : boolean = false;
var health : int;
var x : float;
var z : float;
function Tile(newType, isVisible, newHealth, newX, newZ){//constructor
this.type = newType;
this.visible = isVisible;
this.health = newHealth;
this.x = newX;
this.z = newZ;
}
function Name() : String {return ("Tile" + x + "x" + z);}
}
Normally, I'd also be adding the following code under the "Creates the tile" comment, which makes it go even slower.
var tile : Transform = Instantiate(tilePrefab.transform, new Vector3(x,0,z),Quaternion.identity);
To give you an idea of what I'm doing: I'm building a tile-based map from scratch, and I'm trying to build it with good performance in mind.
Answer by Eric5h5 · Jun 23, 2011 at 03:20 AM
Mostly it's the Debug.Logs that's slowing it down. Take all Debug.Logs out of loops if you want anything like reasonable performance.
Oh wow... It literally went from 23-35 sec to LESS THAN one second loadup. And that's after rendering. I'm suddenly a lot less concerned about loadup performance! :D
Any idea why it might take two or three seconds after all the grid has been loaded before I can actually move the screen? It freezes for a moment, and all the code has been resolved.
Instantiating thousands of objects is pretty intensive and will take a few moments.
Short freezes out of the nowhere are normally a consequence of seeing a new texture you never saw before. if that happens it has to be uploaded from RA$$anonymous$$ to VRA$$anonymous$$ for the graphic chip to render it and if you are on desktop this takes from an instant to several to several dozen milliseconds and more depending on texture size, compression and gpu (the worse the gpu the longer, the less its compressed the longer, the larger it is, the longer ;))
Answer by Dreamora · Jun 23, 2011 at 03:17 AM
how large is mapZ and mapX? depending on that, your code is not even remotely computer and loading friendly, if you exceed what you need to fill the screen there (
Also if the map size is really big the yield after each row will slow it down a bit more. If you want to provide feedback on the progress it's ok
The objects are just data store to hold the health, position, and type of each tile. That's persistent information that I need from the start. I was thinking of rendering only the visible tiles, sure, but I need to load all the map data and it's taking way too long.
The map is currently 100 by 100. It's size changes at the start of the game depending on what map is loaded.