Hey all I’m creating a tile set engine and I’m having issues with getting the sprites for the tiles to render in the scene but everything else seems to work. The following code is where my engine will cut the source sprite into smaller sprite tiles using a grid.
Any ideas?
Thanks.
void CreateSet(){
if (newSetName == "") {
Debug.LogWarning ("Your new tile set must have a name.");
}
else if (tar.ContainsTileSet (newSetName)) {
Debug.LogWarning ("There is already a tile set with the name : \"" + newSetName + "\".");
}
else if (newSetSprite == null) {
Debug.LogWarning ("The tile set image has not been defined.");
}
else {
try{
var tex = newSetSprite.texture;
int x = 0, y = 0, w = tex.width, h = tex.height;
List<Sprite> tiles = new List<Sprite>();
while(y < h){
var pix = tex.GetPixels(x, y, tileSize, tileSize);
Texture2D tileTex = new Texture2D(tileSize, tileSize, TextureFormat.RGBA32, false);
tileTex.SetPixels(pix);
tileTex.Apply();
Sprite tile = Sprite.Create(tileTex, new Rect(0, 0, tileSize, tileSize), newSetSprite.pivot, newSetSprite.pixelsPerUnit);
tile.name = (x / tileSize).ToString() + "-" + (y / tileSize).ToString();
tiles.Add(tile);
x += tileSize;
if(x >= w){
x = 0;
y += tileSize;
}
}
var s = new TileSet(newSetName, newSetSprite, tileSize, tiles.ToArray());
tileSets.Add(s);
newSetName = "";
}
catch(System.Exception e){
Debug.LogWarning ("The tile set could not be created.");
throw e;
}
}
}