Closed. This question needs details or clarity。它当前不接受答案。












想改善这个问题吗?添加详细信息,并通过editing this post阐明问题。

6年前关闭。



Improve this question




如何将SKTextureFilteringNearest设置为所有SKTextures的默认过滤模式?如果我没有将滤镜设置为最接近,则我的精灵所有边缘似乎都模糊了。

最佳答案

SpriteKit使用SKTextureFilteringLinear为所有纹理设置默认的filteringMode,这会导致图像模糊(尤其是缩放时)。要解决此问题,可以为SKTextureSKTextureAtlas创建类别并调用适当的方法。或者您可以使用方法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

07-28 01:53
查看更多