unity将安卓streamingAssetsPath文件复制到persistentDataPath

private void TestCopy()
{
  string from = Application.streamingAssetsPath + "/Test/test.txt";
  string to = Application.persistentDataPath + "/Test/";
  CopyFile(from, to);
}

public static void CopyFile(string sourcePath, string destinationPath)
{
    byte[] fileData = null;
    // 从 StreamingAssets 文件夹读取文件数据
    if (Application.platform == RuntimePlatform.Android)
    {
        using (UnityWebRequest www = UnityWebRequest.Get(sourcePath))
        {
            www.SendWebRequest();
            while (!www.isDone) { }
            fileData = www.downloadHandler.data;
        }
    }
    else
    {
        fileData = File.ReadAllBytes(sourcePath);
    }
    // 创建目标文件夹(如果不存在)
    string destinationFolder = Path.GetDirectoryName(destinationPath);
    if (!Directory.Exists(destinationFolder))
    {
        Directory.CreateDirectory(destinationFolder);
    }
    // 将文件数据写入目标文件
    File.WriteAllBytes(destinationPath, fileData);
}

posted @ 2023-06-18 13:40  longxiaoming  阅读(477)  评论(0编辑  收藏  举报