• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - SKTexture 预加载

[复制链接]
菜鸟教程小白 发表于 2022-12-12 19:02:49 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

当您使用 spritekit preloadTextures 函数预加载纹理时,它会将提供的数组中的所有纹理一次加载到内存中。

如果您无法在游戏中使用“加载屏幕”来拆分关卡,但确实有单独的关卡,其中的图像文件彼此不同,那么您如何避免一次将所有图像存储在内存中spritekit 在需要时加载图像而不牺牲帧速率?



Best Answer-推荐答案


您可以创建一个单例类,其中包含特定于您当前正在玩的关卡的加载和卸载资源的方法。例如,如果你有纹理 a、b 和 c 需要为级别 1 加载,纹理 x、y 和 z 需要在级别 2 中加载,你可以有一个方法 -(void)loadLevelOneTextures;-(void)loadLevelTwoTextures; 以及 -(void)unloadLevelOneTextures;-(void)unloadLevelTwoTextures;

这样,您可以在需要纹理之前告诉单例加载纹理,完成后告诉它释放它们。

//GameTextureLoader.h
//
@import SpriteKit;
@import Foundation;
@interface GameTextureLoader : NSObject
@property (strong, nonatomic)NSMutableArray *levelOneTextures;
@property (strong, nonatomic)NSMutableArray *levelTwoTextures;
+ (GameTextureLoader *)sharedTextures;
- (void)loadLevelOneTextures;
- (void)unloadLevelOneTextures;
- (void)loadLevelTwoTextures;
- (void)unloadLevelTwoTextures;

以及实现:

//GameTextureLoader.m
//
#import "GameTextureLoader.h"
@implementation GameTextureLoader
+ (GameTextureLoader *)sharedTextures{
    static dispatch_once_t onceToken;
    static id sharedInstance;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}
- (instancetype)init{
    self = [super init];
    if (self)
    {
            self.levelOneTextures = [[NSMutableArray alloc] init];
            self.levelTwoTextures = [[NSMutableArray alloc] init];
        return self;
    }
    else{
        exit(1);
    }
}
- (void)loadLevelOneTextures{
        //Order of images will determin order of textures
    NSArray *levelOneImageNames = @[@"imageA", @"imageB", @"imageC"];
    for (NSString *image in levelOneImageNames){
        SKTexture *texture = [SKTexture textureWithImageNamed:image];
        [self.levelOneTextures addObject:texture];
    }
}
- (void)loadLevelTwoTextures{
        //Order of images will determin order of textures
    NSArray *levelTwoImageNames = @[@"imageX", @"imageY", @"imageZ"];
    for (NSString *image in levelTwoImageNames){
        SKTexture *texture = [SKTexture textureWithImageNamed:image];
        [self.levelTwoTextures addObject:texture];
    }
}
- (void)unloadLevelOneTextures{
    [self.levelOneTextures removeAllObjects];
}
- (void)unloadLevelTwoTextures{
    [self.levelTwoTextures removeAllObjects];
}

您将为您拥有的每个级别执行此操作,然后要访问纹理,您将执行类似的操作。 (一定要先导入GameTextureLoader.h)

GameTextureLoader *loader = [GameTextureLoader sharedTextures];
[loader loadLevelOneTextures];
SKSpriteNode *node = [SKSpriteNode spriteNodeWithTexture:loader.levelOneTextures[0]];
[self addChild:node];

关于ios - SKTexture 预加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21868609/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap