- Home /
Sprite created from texture not displaying
Ok,
So I am basically trying to load sprite sheets textures from the resources folder (as texture 2d) at run time and convert them into sprites.
I have got the textures loading from the resource folder and the sprites being created from the sheet, but when I look in game the sprites are not displaying, even though all the data looks correct. IE the correct sprite is being loaded onto the right object and in the debugger the sprite is linked to the correct texture and the rect dimensions and origin(pivot) are also correct. I also have confirmed that the sorting layers are set fine (I have dynamic sprite layering in my code so have double checked it), but still nothing...
Here are the code sample so hopefully someone can help :)
Here is the code to import the textures from the resource folder and store them in a dictionary:
resourceLoadStep = "Load textures...";
foreach (Texture2D texture in Resources.LoadAll<Texture2D>("Sprites"))
{
resourceLoadStep = string.Concat("Load texture ", texture.name, "...");
spriteSheets.Add(texture.name, texture);
DataHandler.control.WriteSystemLog(string.Concat("Spritesheet ", texture.name, "loaded successfully"));
yield return null;
}
yield return null;
Here is the code that splits the textures into sprites, it does this by referencing a set of sprite indexes read in earlier from XML:
resourceLoadStep = "Generate sprites...";
foreach(DataTypes.SpriteData data in spriteDataObjects)
{
//we need to create a new sprite from the data object and add it to the sprites
if (spriteSheets.ContainsKey(data.SheetName))
{
newSprite = Sprite.Create(spriteSheets[data.SheetName],
data.SpriteRect,
data.Pivot);
newSprite.name = data.Name;
switch(data.Type)
{
case DataTypes.SpriteData.SpriteType.Avatar:
if (!avatarSprites.ContainsKey(data.Name))
{
avatarSprites.Add(data.Name, newSprite);
}
else
{
DataHandler.control.WriteSystemLog(string.Concat("Unable to load sprite '", data.Name, "' as a sprite with this name already exists in 'Avatar Sprites'"));
}
break;
case DataTypes.SpriteData.SpriteType.Item:
if (!itemSprites.ContainsKey(data.Name))
{
itemSprites.Add(data.Name, newSprite);
}
else
{
DataHandler.control.WriteSystemLog(string.Concat("Unable to load sprite '", data.Name, "' as a sprite with this name already exists in 'Item Sprites'"));
}
break;
case DataTypes.SpriteData.SpriteType.Planet:
if (!planetSprites.ContainsKey(data.Name))
{
planetSprites.Add(data.Name, newSprite);
}
else
{
DataHandler.control.WriteSystemLog(string.Concat("Unable to load sprite '", data.Name, "' as a sprite with this name already exists in 'Planet Sprites'"));
}
break;
case DataTypes.SpriteData.SpriteType.Ship:
if (!shipSprites.ContainsKey(data.Name))
{
shipSprites.Add(data.Name, newSprite);
}
else
{
DataHandler.control.WriteSystemLog(string.Concat("Unable to load sprite '", data.Name, "' as a sprite with this name already exists in 'Ship Sprites'"));
}
break;
case DataTypes.SpriteData.SpriteType.Star:
if (!starSprites.ContainsKey(data.Name))
{
starSprites.Add(data.Name, newSprite);
}
else
{
DataHandler.control.WriteSystemLog(string.Concat("Unable to load sprite '", data.Name, "' as a sprite with this name already exists in 'Star Sprites'"));
}
break;
case DataTypes.SpriteData.SpriteType.Station:
if (!stationSprites.ContainsKey(data.Name))
{
stationSprites.Add(data.Name, newSprite);
}
else
{
DataHandler.control.WriteSystemLog(string.Concat("Unable to load sprite '", data.Name, "' as a sprite with this name already exists in 'Station Sprites'"));
}
break;
case DataTypes.SpriteData.SpriteType.UI:
break;
case DataTypes.SpriteData.SpriteType.None:
DataHandler.control.WriteSystemLog(string.Concat("Unabel to load sprite '", data.Name, "' as the type is set to 'None'"));
break;
}
}
else
{
DataHandler.control.WriteSystemLog(string.Concat("Unable to load sprite '", data.Name, "' as the Sprite Sheet '", data.SheetName, "' does not exist"));
}
}
yield return null;
And finally the code in one of the game objects that references the sprite by name and assigns it to the objects renderer:
//need to set sprite based on ship id
//test using the new sprite handler
thisRenderer.sprite = DataHandler.control.ShipSprites[DataHandler.control.ShipTypes[shipID].SpriteName];
I have looked every where for an answer to this, hopefully someone else has had this issue and can help!