图像文件夹:创建和填充SKShapeNodes,每个都有1个 [英] Folder of images: create and fill SKShapeNodes, 1 of each

查看:148
本文介绍了图像文件夹:创建和填充SKShapeNodes,每个都有1个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以创建一个文件夹并让SpriteKit浏览该文件夹,查找每个图像(无论名称如何),并为该文件夹中的每个文件创建一个SKShapeNode(无论有多少)?

Is it possible to create a folder and have SpriteKit go through that folder, find each image (regardless of name), and create a SKShapeNode for each in that folder (regardless of how many there are)?

假设所有图像大小相同,并且所有.png文件都可以作为填充SKShapeNodes的纹理用作唯一图像,并且所有SKShapeNodes的大小都相同。

Assume all images are the same size, and all .png files ready to be used as unique images as textures for filling SKShapeNodes, and that all SKShapeNodes will be the same size.

我想知道,一旦完成,创建了多少个形状,并根据所用纹理的图像名称对它们进行唯一命名。

I want to know, once done, how many shapes have been created, and have them uniquely named based on the name of the image of the texture used.

但我正在阅读的所有内容似乎都是关于知道有多少图像,以及它们的名字是什么。

But all I'm reading seems to be about knowing how many images are there, and what their names are, before hand.

无论从哪里开始搜索如何做到这一点。

Clueless as to where to start searching about how to do this.

我把他们非常富有想象力的名字[一,二,三,四,五等]的图片转储成了文件夹为atlas,如下所示:

I've dumped the images, with their very imaginative names [one, two, three, four, five etc. ], into a folder as "atlas", like this:

但是根据Allessandro的精彩回答,我不知道如何处理这个文件夹并了解其中的内容。

but as per Allessandro's wonderful answer, I have no idea how to address this "folder" and get at what's in it.

我认为这就是我要获得的这一行完全错误:

I think it's this line I'm getting completely wrong:

let folderPath = Bundle.main.path(forResource: "Images", ofType: nil)

我不知道要替换什么Images with 。

I don't know what to replace "Images" with.

找到一种更简单的方法:

Found a much easier way to do this:

let myTextureAtlas = SKTextureAtlas(named: "demoArt")

        print(myTextureAtlas)
        print(myTextureAtlas.textureNames)


推荐答案

请求太多,所以我尝试用一​​些代码来帮助你:

Too many requests , so I try to help you with some code:

创建文件夹

func createDir(folderName:String) {
        let fileManager = FileManager.default
        if let directory = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "APP_GROUP_IDENTIFIER") {
            let newDirectory = directory.appendingPathComponent(folderName)
            try! fileManager.createDirectory(at: newDirectory, withIntermediateDirectories: false, attributes: nil)
        }
}






现在您有了文件夹,因此您可以决定保存文件或将一个文件从文档复制到此新目录。我为最后一个做了一个例子。


Now you have your folder so you can decide to save a file or to copy one file from documents to this new directory. I make an example for the last one.

复制文件

let fileManager = FileManager.default
do {
   try fileManager.copyItem(atPath: "hero.png", toPath: "subfolder/hero.swift")
}
catch let error as NSError {
   print("Something went wrong: \(error)")
}






现在你想知道如何从目录中提取所有文件。


Now you want to know how to extract all files from a directory.

从目录中提取所有文件

func extractAllFile(atPath path: String, withExtension fileExtension:String) -> [String] {
        let pathURL = NSURL(fileURLWithPath: path, isDirectory: true)
        var allFiles: [String] = []
        let fileManager = FileManager.default
        if let enumerator = fileManager.enumerator(atPath: path) {
            for file in enumerator {
                if #available(iOS 9.0, *) {
                    if let path = NSURL(fileURLWithPath: file as! String, relativeTo: pathURL as URL).path
                        , path.hasSuffix(".\(fileExtension)"){
                        allFiles.append(path)
                    }
                } else {
                    // Fallback on earlier versions
                }
            }
        }
        return allFiles
    }

用法

let folderPath = Bundle.main.path(forResource: "Images", ofType: nil)
let allPngFiles = extractAllFile(atPath: folderPath!, withExtension: "png") // returns file path of all the png files inside the folder






之后,您可以使用 allPngFiles 创建一个 SKShapeNode 数组 fillTexture 使用从每个图像创建的SKTexture,并为每个节点提供所使用文件的名称(所以你知道在最后还有你从 array.count 创建了多少个节点。


After that, you can create an array of SKShapeNode based from allPngFiles using fillTexture with a SKTexture created from each image and give to each node the name of the file used (so you know at the end also how many nodes you have created from array.count).

这篇关于图像文件夹:创建和填充SKShapeNodes,每个都有1个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆