Skip to content

Instantly share code, notes, and snippets.

@hjerpbakk
Last active August 29, 2015 14:15
Show Gist options
  • Save hjerpbakk/16a5cbe5a7192261ae45 to your computer and use it in GitHub Desktop.
Save hjerpbakk/16a5cbe5a7192261ae45 to your computer and use it in GitHub Desktop.
Extension method on SKSpriteNode which tiles a texture and applies it to the sprite.
/// <summary>
/// Creates a tiled texture and applies it to the <see cref="SKSpriteNode"/>.
/// </summary>
/// <param name="spriteNode">The <see cref="SKSpriteNode"/> to which the texture is applied.</param>
/// <param name="texture">The texture used as tiles.</param>
/// <param name="coverageSize">The size of the finished tiled texture.</param>
public static void TileSprite(this SKSpriteNode spriteNode, UIImage texture, CGSize coverageSize) {
var textureSize = new CGRect(CGPoint.Empty, texture.Size);
UIGraphics.BeginImageContext(coverageSize);
var context = UIGraphics.GetCurrentContext();
context.DrawTiledImage(textureSize, texture.CGImage);
var tiledBackground = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
spriteNode.Texture = SKTexture.FromImage(tiledBackground.CGImage);
spriteNode.Size = coverageSize;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment