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

查看:12
本文介绍了图像文件夹:创建和填充 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 文件都准备好用作唯一图像作为填充 SKShapeNode 的纹理,并且所有 SKShapeNode 大小相同.

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:

但根据亚历山德罗的精彩回答,我不知道如何处理这个文件夹"并了解其中的内容.

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".

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

<小时>

之后,您可以使用 fillTextureallPngFiles 创建一个 SKShapeNode 数组,并使用从每个图像创建的 SKTexture 并给每个node 使用的文件的 name (这样你最后也知道你从 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天全站免登陆