Last active
August 29, 2015 14:15
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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