unity 复制Resources文件夹中一个名叫“a”的文件夹,并建立一个名叫“b”的新文件夹
时间: 2023-08-11 07:05:53 浏览: 93
从a文件夹到b文件夹文件复制
您可以使用以下代码来复制名为“a”的文件夹并创建一个名为“b”的新文件夹:
```csharp
using System.IO;
using UnityEngine;
public class CopyResourcesFolder : MonoBehaviour
{
void Start()
{
string sourcePath = Application.dataPath + "/Resources/a";
string destPath = Application.dataPath + "/Resources/b";
if (!Directory.Exists(destPath))
{
Directory.CreateDirectory(destPath);
}
foreach (string file in Directory.GetFiles(sourcePath))
{
string fileName = Path.GetFileName(file);
string dest = Path.Combine(destPath, fileName);
File.Copy(file, dest, true);
}
foreach (string folder in Directory.GetDirectories(sourcePath))
{
string folderName = new DirectoryInfo(folder).Name;
string dest = Path.Combine(destPath, folderName);
Directory.CreateDirectory(dest);
CopyFolder(folder, dest);
}
}
private void CopyFolder(string sourceFolder, string destFolder)
{
foreach (string file in Directory.GetFiles(sourceFolder))
{
string fileName = Path.GetFileName(file);
string dest = Path.Combine(destFolder, fileName);
File.Copy(file, dest, true);
}
foreach (string folder in Directory.GetDirectories(sourceFolder))
{
string folderName = new DirectoryInfo(folder).Name;
string dest = Path.Combine(destFolder, folderName);
Directory.CreateDirectory(dest);
CopyFolder(folder, dest);
}
}
}
```
这段代码会在 Resources 文件夹下创建一个名为“b”的文件夹,并将名为“a”的文件夹及其所有内容复制到“b”文件夹中。请注意,如果“b”文件夹已经存在,则会被覆盖。
阅读全文