2015-02-06 2 views
3

SpriteKit을 사용하여 SKSpriteNode 너비를 채우기 위해 수평으로 반복되는 SKTexture를 어떻게 타일링합니까? 이것은 내가 지금까지 가지고있는 것입니다 - 단지 텍스처를 늘립니다.스프라이트 노드의 텍스처를 타일링하는 방법은 무엇입니까?

var header = SKSpriteNode() 
    let headerTexture = SKTexture(imageNamed: "inGameHeader-1.png") 
    header = SKSpriteNode(texture: headerTexture) 
    header.position = CGPoint(x: 0, y: CGRectGetMaxY(self.frame)-39) 
    header.size.width = CGRectGetWidth(self.frame) 
    header.size.height = 150 
    header.anchorPoint = CGPoint(x: 0,y: 1) 
    addChild(header) 
+0

현재 텍스처를 바둑판 식으로 배열하는 간단한 방법은 없지만 단일 드로잉 패스에서 많은 스프라이트를 생성 할 수 있습니다. 어느 비슷한 결과를 제공합니까? – Whirlwind

+0

흠, 어떻게 그 일을할까요? 내가해야 할 일은 헤더 배경을 채우기 위해 1px 폭의 그라디언트를 반복하는 것입니다. 나는 방금 그게 1px 반복되는 600px 너비 그라디언트를 갖는 것보다 더 효율적이라고 생각했습니다. – Chris

+0

내가 말했듯이 현재는 그렇게 할 방법이 없지만 걱정할 필요가 없습니다. Spritekit은 최적화 : https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/DesigningGameswithSpriteKit/DesigningGameswithSpriteKit.html#//apple_ref/doc/uid/TP40013043-CH7-SW7 – Whirlwind

답변

0

SpriteKit 셰이더를 사용하여 작동하는 구현은 this answer에서 찾을 수 있습니다. Swift에서 사용하려면 헤더를 가져와야합니다.

0

내 솔루션은 다음과 같습니다. 스프라이트의 크기와 타일 크기가 같은 이미지를이 함수에 보냅니다. 타일링 된 SKTexture가 반환됩니다. 여기

-(SKTexture *)textureWithImage:(UIImage *)image tiledForSize:(CGSize)size withCapInsets:(UIEdgeInsets)capInsets{ 
//get resizable image 
image = [image resizableImageWithCapInsets:capInsets]; 
//redraw image to target size 
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f); 
[image drawInRect:CGRectMake(0, 0, size.width-1, size.height-1)]; 
[[UIColor clearColor] setFill]; 
//get target image 
UIImage *ResultImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
// get texture 
SKTexture * texture = [SKTexture textureWithImage:ResultImage]; 
return texture; 
} 
0

당신은 여기 내 솔루션, 당신은 resizableImageWithCapInsets가있는 UIButton 또는 UIImageView에이 작업을 수행 할 수 있습니다 알고, 그래서 SKTexture 클래스에는 실제로 지정된 이니셜 라이저가 없습니다.

참고 :이 기능은별로 유용하지 않습니다. 사용하기 전에 텍스처를 미리로드하거나 커스텀 셰이더 세계로 들어가는 것이 가장 좋습니다.

static func generateTiledTexture(size: CGSize, imageNamed imageName: String) -> SKTexture? { 
    var texture: SKTexture? 

    UIGraphicsBeginImageContext(size) 
    let context = UIGraphicsGetCurrentContext() 

    if let image = UIImage(named: imageName), let cgImage = image.cgImage { 
     context?.draw(cgImage, in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height), byTiling: true) 

     if let tiledImage = UIGraphicsGetImageFromCurrentImageContext() { 
      texture = SKTexture(image: tiledImage) 
     } 
    } 

    UIGraphicsEndImageContext() 

    return texture 
} 
0

는 AS 확장을 사용하지 스위프트 3과 4에 대한 정적 함수의 :

-(SKTexture *)tiledTextureImage:(UIImage *)image ForSize:(CGSize)size tileSize:(CGSize)tileSize{ 

// First we create a new size based on the longest side of your rect 
int targetDimension = (int)fmax(size.width,size.height); 
CGSize targetSize = CGSizeMake(targetDimension, targetDimension); 

// Create a CGImage from the input image and start a new image context. 
struct CGImage *targetRef = image.CGImage; 
UIGraphicsBeginImageContext(targetSize); 
CGContextRef contextRef = UIGraphicsGetCurrentContext(); 

// Now we simply draw a tiled image 
CGContextDrawTiledImage(contextRef, CGRectMake(0,0, tileSize.width,tileSize.height), targetRef); 
UIImage *tiledTexture = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

// Finally create a texture and return it. 
return [SKTexture textureWithImage:tiledTexture]; 
} 
관련 문제