@@ -8,27 +8,15 @@ public class HttpDownloader : InitOnceSingleton<HttpDownloader>
88{
99
1010 // 下载方法,接受 URL 和回调函数
11- public void Download ( string url , Action < DownloadResult > callback )
11+ public void Download ( string url , string absolutePath , Action < DownloadResult > callback = null )
1212 {
13- StartCoroutine ( DownloadCoroutine ( url , callback : callback ) ) ;
14- }
15-
16- public void Download ( string url , string fileName , Action < DownloadResult > callback )
17- {
18- StartCoroutine ( DownloadCoroutine ( url , fileName , callback ) ) ;
13+ StartCoroutine ( DownloadCoroutine ( url , absolutePath , callback ) ) ;
1914 }
2015
2116 // 下载协程,处理异步下载逻辑
22- private IEnumerator DownloadCoroutine ( string url , string fileName = null , Action < DownloadResult > callback = null )
17+ private IEnumerator DownloadCoroutine ( string url , string absolutePath , Action < DownloadResult > callback = null )
2318 {
24- // 从 URL 中提取文件名,并组合保存路径
25- if ( fileName == null ) {
26- fileName = Path . GetFileName ( url ) ;
27- }
28- // 下载结果存在临时文件夹
29- string filePath = Path . Combine ( Application . temporaryCachePath , "tmp" , fileName ) ;
30- // 确保目录存在
31- string directory = Path . GetDirectoryName ( filePath ) ;
19+ string directory = Path . GetDirectoryName ( absolutePath ) ;
3220 if ( ! Directory . Exists ( directory ) )
3321 {
3422 Directory . CreateDirectory ( directory ) ;
@@ -37,13 +25,13 @@ private IEnumerator DownloadCoroutine(string url, string fileName = null, Action
3725 // 创建下载结果对象
3826 DownloadResult result = new DownloadResult ( ) ;
3927
40- // 如果文件存在,先(删除) 跳过, unity读取图片时会占用,删不掉
41- if ( File . Exists ( filePath ) )
28+ // 如果文件存在,跳过, unity读取图片时会占用,删不掉
29+ if ( File . Exists ( absolutePath ) )
4230 {
4331 result . Success = true ;
44- result . FilePath = filePath ;
45- result . FileSize = new FileInfo ( filePath ) . Length ; // 获取文件大小
46- // 调用回调函数,返回结果
32+ result . FilePath = absolutePath ;
33+ result . FileSize = new FileInfo ( absolutePath ) . Length ; // 获取文件大小
34+ result . CompletionTime = DateTime . Now ; // 记录完成时间
4735 callback ? . Invoke ( result ) ;
4836 yield break ;
4937 }
@@ -52,15 +40,15 @@ private IEnumerator DownloadCoroutine(string url, string fileName = null, Action
5240 using ( UnityWebRequest www = UnityWebRequest . Get ( url ) )
5341 {
5442 // 设置下载处理器,直接保存到文件
55- www . downloadHandler = new DownloadHandlerFile ( filePath ) ;
43+ www . downloadHandler = new DownloadHandlerFile ( absolutePath ) ;
5644 yield return www . SendWebRequest ( ) ;
5745
5846 // 检查下载是否成功
5947 if ( www . result == UnityWebRequest . Result . Success )
6048 {
6149 result . Success = true ;
62- result . FilePath = filePath ;
63- result . FileSize = new FileInfo ( filePath ) . Length ; // 获取文件大小
50+ result . FilePath = absolutePath ;
51+ result . FileSize = new FileInfo ( absolutePath ) . Length ; // 获取文件大小
6452 result . CompletionTime = DateTime . Now ; // 记录完成时间
6553 }
6654 else
0 commit comments