Unity中的文件拷贝
在项目中经常遇到需要将streamingAssetsPath中的文件拷贝至persistentDataPath中,记录一下自己的实现方式:
public static void CopyFile(string fileName, int imageType)
{
if (Application.platform == RuntimePlatform.Android)
{
using (UnityWebRequest request = UnityWebRequest.Get(Application.streamingAssetsPath + "/" + fileName))
{
request.timeout = 3;
request.downloadHandler = new DownloadHandlerFile(Application.persistentDataPath + "/" + fileName);//直接将文件下载到外存
request.SendWebRequest();
float time = Time.time;
//下载完成后执行的回调
while (!request.isDone)
{
}
request.Abort();
//默认值是true,调用该方法不需要设置Dispose(),Unity就会自动在完成后调用Dispose()释放资源。
request.disposeDownloadHandlerOnDispose = true;
request.Dispose();
}
}
else
{
File.Copy(Application.streamingAssetsPath + "/" + fileName, Application.persistentDataPath + "/" + fileName, true);
}
}