- Home /
Tile loading issue
HI, I am trying to load tiles into a 2D environment (orthographic), I am grabbing PNGs from the WWW and loading them into the 30 or so GUITexture objects, I am wondering if this is the most optimal way to do this (seemingly not)? As the user scrolls around, my idea is to have those textures change (so still have only 30 or so GUITexture objects just load up new textures from server). I can get them to draw, but as soon as I move around too quickly I get a "fatal error in GC: too many threads" error. Any help would be greatly appreciated!
Code for applying the texture to a tile:
function setTexture(obj){
url = "http://blah blah blah";
var Download : WWW = new WWW (url);
yield Download; // Wait for download to complete
obj.guiTexture.texture=Download.texture;
Download.Dispose();
}
Jake
Answer by DaveA · Feb 22, 2012 at 12:08 AM
Can you try using less than 30? How did you arrive at that number?
Answer by jtcrash · Feb 22, 2012 at 10:00 PM
Dave,
Really appreciate your speedy reply, let me clarify a bit further here. These tiles (and the PNGs) are 256x256, I generate the tiles based on the Screen.width and Screen.height to determine the number of tiles that are created and subsequently pulled from WWW. The generation of the prefabs only happens once (in the start()).
var tileSize : int = 256;
function setupGrid(){
gridSizeX = (Screen.width / tileSize) + ((gridSizeX % 2) == 0 ? 3 : 2); // get the number of tiles required horizontally for the resolution of the game
gridSizeY = (Screen.height / tileSize) + ((gridSizeY % 2) == 0 ? 3 : 2);
numTiles = gridSizeX * gridSizeY; // Set the number of tiles total
for(c = 0; c < numTiles; c++){ // Generate the appropriate number of tile prefabs
var thisTile = Instantiate(tileObj, transform.position, transform.rotation);
thisTile.transform.parent = tileParent;
}
}
Note that I am adding 2 or 3 for padding and to make sure that the grid is odd numbered so I have an exact center tile (long story). This all seems to work just fine, the clones get created in the appropriate amount, and the initial tile pull works well enough.
I did notice once I compiled that memory jumps out the roof (gets to about 300MB then I get the GC error) when I try to redraw the tiles. Basically, I figured that changing the textures again would simply overwrite the texture in memory? Is this not correct?
Here is my actual drawing function (which calls that WWW pull function setTexture from before):
function drawTiles(x : float, y : float){
var col = Mathf.Floor(gridSizeX / 2);
var gu = col;
var xcoord = -(gu * zoomLevels[zoomLevel]) + x;
var ycoord = (gu * zoomLevels[zoomLevel]) + y;
var tilex = -(gu * tileSize);
var tiley = (gu * tileSize);
for(var child : Transform in transform){
getTileTexture(child, xcoord, ycoord, (xcoord + zoomLevels[zoomLevel]), (ycoord + zoomLevels[zoomLevel]));
child.guiTexture.pixelInset = Rect (tilex, tiley, tileSize, tileSize);
if(col == -gu){
xcoord = -(gu * zoomLevels[zoomLevel]) + x;
ycoord = ycoord - zoomLevels[zoomLevel];
tilex = -(gu * tileSize);
tiley = tiley - tileSize;
col = gu;
}else{
col--;
xcoord = -(col * zoomLevels[zoomLevel]) + x;
tilex = -(col * tileSize);
}
}
}
This function is also called from start() to begin with but then when you use the scroll wheel (to zoom, just like in google maps for example) I recall this function again in the hopes that it would re-pull the textures from the server.
if(Input.GetAxis('Mouse ScrollWheel')){
if(Input.GetAxis('Mouse ScrollWheel') > 0 && zoomLevel < zoomLevels.Length) zoomLevel++;
else if(Input.GetAxis('Mouse ScrollWheel') < 0) zoomLevel--;
drawTiles(0,0);
}
Also, I am new to this whole unity thing and game engine developing (though I have been programming for a bit) so be easy on me :)
Thanks a lot for taking a look at this mess for me,
Jake