【问题标题】:load all files from a specific atlas Sprite-kit从特定的地图集 Sprite-kit 加载所有文件
【发布时间】:2014-05-07 20:28:56
【问题描述】:

考虑到此图集中的所有文件名都不同,有没有办法从特定图集中加载所有文件?。

我尝试了在这篇文章中找到的方法 Getting a list of files in the Resources folder - iOS 它可以完美运行,但仅适用于基本文件夹,不适用于 .atlas 扩展名

  NSString * resourcePath = [[NSBundle mainBundle] resourcePath];
  NSString * documentsPath = [resourcePath stringByAppendingPathComponent:@"MYATLAS.atlas"];
  NSError * error;
  NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:&error];

【问题讨论】:

  • 您要预加载纹理,还是立即加载并使用它们?
  • 我想加载它们并创建 SKTextures 并将它们全部添加到 NSDictionnary 中并使用文件名作为键
  • 我会调整我的答案以使用字典
  • 顺便说一句。我认为预加载图集对您来说会很好,您将能够从预加载的图集中快速按名称获取每个纹理:)
  • 好的谢谢我现在明白了!

标签: ios objective-c sprite-kit atlas


【解决方案1】:

首先像这样初始化你的图集:

SKTextureAtlas* myAtlas = [SKTextureAtlas atlasNamed:@"MYATLAS.atlas"];

然后您可以将所有纹理加载到字典中

NSMutableDictionary* texturesDictionary = [NSMutableDictionary dictionary];
for(NSString* textureName in myAtlas.textureNames){
    SKTexture* texture = [myAtlas textureNamed:textureName];
    [texturesDictionary setObject:texture key:textureName];
}

或者只是预加载图集以备后用

[myAtlas preloadWithCompletionHandler:completionHandler];

【讨论】:

  • 非常感谢!我以前从未见过 SKTextureAtlas 有纹理名称。
猜你喜欢
  • 1970-01-01
  • 2014-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多