Version: 2022.3
public bool removeFileOnAbort ;

描述

如果下载被中止(手动中止或因出现错误而中止),则应删除创建的文件。默认值:false。

using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;

public class DownloadHandlerFileSample : MonoBehaviour { void Start() { StartCoroutine(Download()); }

IEnumerator Download() { var uwr = new UnityWebRequest("https://unity3d.com/"); uwr.method = UnityWebRequest.kHttpVerbGET; var resultFile = Path.Combine(Application.persistentDataPath, "result.txt"); var dh = new DownloadHandlerFile(resultFile); dh.removeFileOnAbort = true; uwr.downloadHandler = dh; yield return uwr.SendWebRequest(); if (uwr.result != UnityWebRequest.Result.Success) Debug.Log(uwr.error); else { Debug.Log("Download saved to: " + resultFile); } } }