RSS Feed

Game Size and the Sprite Atlas

November 21, 2017

A sprite atlas is a sheet holding more than one sprite. It is supposed to be more efficient for the graphics engine to use. For our purposes, it is also useful for reducing program size. For Goblin Defence I was able to cut size from 118Mb down to 59Mb.

 

Using a sprite was easy. Just change calls to "Resource.Load(path)" to "atlas.GetSprite(name)". Since the atlas only knows the name, I did need to rename sprites with the same name but on different paths.

 

In Goblin Defence sprites were sometimes used to hold Texture2D textures for other purposes. A sprite contains the offset into the atlas. There is no built-in function at run-time to copy the texture. I had to write one.

 

  public static Texture2D GetTexture(Sprite sprite) {
    if (!sprite.packed) {
      return sprite.texture; // not in atlas
    }
    int x = (int)System.Math.Ceiling(sprite.textureRect.x)

    int y = (int)System.Math.Ceiling(sprite.textureRect.y);


    int width = (int)System.Math.Ceiling(sprite.textureRect.width);

    int height = (int)System.Math.Ceiling(sprite.textureRect.height);


    Texture2D texture = new Texture2D (width, height);
    texture.SetPixels(sprite.texture.GetPixels(x, y, width, height));
    texture.Apply();
    return texture;
  }

 

Have a look at the results while playing Goblin Defence.

 

 

 

Share on Facebook
Share on Twitter
Please reload

Featured Posts

My limited tests show that Dictionary is faster than `SortedList`. The latter uses `Array.BinarySearch` to find entries. I have read other performance...

How are C# Dictionaries so Fast?

August 27, 2018

1/5
Please reload

Recent Posts

April 12, 2018

Please reload

Archive
Please reload

Search By Tags
Please reload

Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square