ios objective-c swift sprite-kit sktextureatlas

ios - Spritekit: no carga imágenes @ 3x de SKTextureAtlas



objective-c swift (5)

Como mi proyecto de muestra fue eliminado (pensé que sería mucho más fácil de probar), publicaré algunos códigos e imágenes para ilustrar mi punto.

Aquí hay imágenes de muestra

Mi configuración de atlas:

Mi configuración de imagen de lanzamiento:

El código donde agrego estos sprites a mi escena

override func didMoveToView(view: SKView) { let texture = SKTextureAtlas(named: "scenery") let test = SKSpriteNode(texture: texture.textureNamed("test")) test.position = CGPoint(x: self.size.width/2, y: self.size.height/2) self.addChild(test) }

Estos son mis resultados:

simulador de iPhone 5:

simulador de iPhone 6 plus:

Intenté cambiar la imagen de inicio para usar el catálogo de activos. Entonces, el iPhone 6 plus parece mejorar una pantalla 2x. Todavía está cargando la imagen 2x, pero la escala.

Lo necesito para cargar mi imagen 3x y escalar con mi imagen 2x.

La respuesta de Gabuh a continuación me señaló en la dirección correcta. Trabaja en un nuevo proyecto. Sin embargo, si uso su solución para mi proyecto SpriteKit real, mis imágenes 3x no se reducen. Son 3 veces más grandes de lo que deberían ser.


Este error aún no se ha resuelto. Al usar solo imágenes @ 2x, la imagen visual de la aplicación se rompe. En su lugar, elija la imagen correcta mirando la escala de la pantalla.

textureName = [UIScreen mainScreen].scale > 2.9 ? @"awesome@3x" : @"awesome";


No estoy seguro de por qué esto nunca se hizo, pero aquí está el comienzo de una solución que es realmente correcta, pero desafortunadamente es un poco más lenta. Tal vez alguien pueda ver algunas cosas para que se procese más rápido

import Foundation import SpriteKit public class Atlas: SKTextureAtlas { var textures = [String:(texture:SKTexture,image:UIImage)](); public convenience init(named name: String) { self.init() let scale = CGFloat(UIScreen().scale); let path = "/(name).atlasc//(name)"; let atlasContent = NSDictionary(contentsOfFile: NSBundle.mainBundle().pathForResource(path, ofType: "plist")!); let content = atlasContent!["images"] as! [[String:AnyObject]]; let info = content[Int(scale) - 1] ; let imagepath = "/(name).atlasc//((info["path"] as! String!).stringByReplacingOccurrencesOfString(".png", withString: ""))"; let imgDataProvider = CGDataProviderCreateWithCFData(NSData(contentsOfFile: NSBundle.mainBundle().pathForResource(imagepath, ofType: "png")!)); let cgimage = CGImageCreateWithPNGDataProvider(imgDataProvider, nil, true, .RenderingIntentDefault); let subimages = info["subimages"] as! [[String:AnyObject]]; for subimage in subimages { let spriteSourceSize = CGSizeFromString(subimage["spriteSourceSize"] as! String); let size = CGSizeApplyAffineTransform(spriteSourceSize, CGAffineTransformMakeScale(1/scale,1/scale)); let isFullyOpaque = subimage["isFullyOpaque"] as! Bool; let spriteOffset = CGPointFromString((subimage["spriteOffset"] as! String)); let textureRect = CGRectFromString((subimage["textureRect"] as! String)); let textureRectSize = CGSizeApplyAffineTransform(textureRect.size, CGAffineTransformMakeScale(1/scale,1/scale)); let name = (subimage["name"] as! String).stringByReplacingOccurrencesOfString("@3x.png", withString: ""); let textureRotated = subimage["textureRotated"] as! Bool; let smallImage = CGImageCreateWithImageInRect(cgimage, textureRect); UIGraphicsBeginImageContextWithOptions(size, isFullyOpaque, scale); let context = UIGraphicsGetCurrentContext(); CGContextSaveGState(context); CGContextSetShouldAntialias(context,false); CGContextSetAllowsAntialiasing( context ,false); CGContextSetInterpolationQuality(context , CGInterpolationQuality.None) if(textureRotated) { CGContextTranslateCTM(context, (size.width)/2, (size.height)/2); CGContextScaleCTM(context, 1, -1); CGContextRotateCTM(context,CGFloat(M_PI_2)); CGContextTranslateCTM(context, 0, ((size.height - textureRectSize.height))); CGContextTranslateCTM(context, -((size.height)/2), -((size.width)/2)); CGContextTranslateCTM(context, spriteOffset.y/scale, -spriteOffset.x/scale); } else { //Set to center of image to flip correctly CGContextTranslateCTM(context, (size.width)/2, (size.height)/2); CGContextScaleCTM(context, 1, -1); CGContextTranslateCTM(context, -((size.width)/2), -((size.height)/2)); CGContextTranslateCTM(context, spriteOffset.x/scale, spriteOffset.y/scale); } CGContextDrawImage(context,CGRect(origin: CGPoint.zero,size: textureRectSize), smallImage); let image = UIGraphicsGetImageFromCurrentImageContext(); let texture = SKTexture(image: image); textures[name] = (texture:texture,image:image); CGContextRestoreGState(context); UIGraphicsEndImageContext(); } } override public func textureNamed(name: String) -> SKTexture { return textures[name]!.texture; } public func imageNamed(name: String) -> UIImage { return textures[name]!.image; } }


Parece estar funcionando ahora si está utilizando una nueva forma de crear atlas. Lo importante es que el Deployment target debe ser> = 9.0 ...

Seleccione Assets.xcassets y haga clic en + signo para crear un nuevo atlas de sprites:

Elija la opción "Nuevo Sprite Atlas" y agregue los activos @ 2x y @ 3x:

Luego, en un código, haga esto:

let sprite = SKSpriteNode(texture: SKTextureAtlas(named: "Sprites").textureNamed("test")) sprite.position = CGPoint(x: frame.midX, y: frame.midY) addChild(sprite)

Insinuación:

Si está probando en Simulator, restablezca el contenido y la configuración de Simulator para borrar el caché antes de intentarlo.


Parece un error cuando Xcode genera el atlas compilado. Si marca dentro del paquete de su aplicación compilada, verá que Xcode no está creando los nombres de atlas correctos para las imágenes @ 3x.

Logré obtener los activos @ 3x creando atlas con el nombre @ 3x y dejando la imagen sin el sufijo.

Y puede verificar la UIScreen.mainscreen().scale Para decidir el nombre del atlas a utilizar. Verifique el nombre del atlas en la imagen adjunta y el código dentro de getTextureAtlas


Xcode 6.2 ahora carga imágenes @ 3x y @ 2x de un atlas. Carga un tamaño 1x (y parece cambiar el tamaño de la imagen por sí solo) si no coloca @ 2x / @ 3x al final del nombre de la imagen.