如何将SKTextureFilteringNearest
设置为我所有SKTextures的默认过滤模式?如果我没有将过滤设置为最近,则表明我的所有精灵的边缘都是模糊的。
答案 0 :(得分:2)
SpriteKit使用SKTextureFilteringLinear
为所有纹理设置默认的filteringMode,从而产生模糊图像(特别是在缩放时)。要解决此问题,您可以为SKTexture
和SKTextureAtlas
创建一个类别,并调用正确的方法。或者您可以使用方法调整textureWithImageNamed:
<强> SKTexture + DefaultSwizzle.h 强>
#import <SpriteKit/SpriteKit.h>
@interface SKTexture (DefaultSwizzle)
@end
<强> SKTexture + DefaultSwizzle.m 强>
#import "SKTexture+DefaultSwizzle.h"
#import <objc/runtime.h> // Include objc runtime for method swizzling methods
@implementation SKTexture (DefaultSwizzle)
+ (SKTexture *)swizzled_textureWithImageNamed:(NSString*)filename
{
// This is the original. At this point the methods have already been switched
// which means that `swizzled_texture*` is the original.
SKTexture *texture = [SKTexture swizzled_textureWithImageNamed:filename];
// Set 'nearest' as default mode
texture.filteringMode = SKTextureFilteringNearest;
return texture;
}
+ (void)load
{
Method original, swizzled;
original = class_getClassMethod(self, @selector(textureWithImageNamed:));
swizzled = class_getClassMethod(self, @selector(swizzled_textureWithImageNamed:));
// Swizzle methods
method_exchangeImplementations(original, swizzled);
}
@end
答案 1 :(得分:0)
我提出了一个不同的解决方案,无需调整即可使原始方法完好无损:
@interface SKTexture (FilteringModeCategory)
+(SKTexture*) textureWithImageNamed:(NSString*)file
filteringMode:(SKTextureFilteringMode)mode;
+(SKTexture*) textureNearestFilteredWithImageNamed:(NSString*)file;
@end
实施:
@implementation SKTexture (FilteringModeCategory)
+(SKTexture*) textureWithImageNamed:(NSString*)file
filteringMode:(SKTextureFilteringMode)mode
{
SKTexture* tex = [SKTexture textureWithImageNamed:file];
tex.filteringMode = mode;
return tex;
}
+(SKTexture*) textureNearestFilteredWithImageNamed:(NSString*)file
{
SKTexture* tex = [SKTexture textureWithImageNamed:file];
tex.filteringMode = SKTextureFilteringNearest;
return tex;
}
@end
随意重命名第二种方法。怎么样:crispyTextureWithImageNamed? :)
PS:SKTextureAtlas
也可能需要相应的textureNamed:filteringMode:
和textureNearestFilteredNamed:
类别方法。