饭太稀

This commit is contained in:
bob
2025-06-30 10:51:37 +08:00
commit 8e45469c83
753 changed files with 87652 additions and 0 deletions

View File

@@ -0,0 +1,50 @@
#if FANTASY_UNITY
using System;
using Fantasy.Async;
using UnityEngine.Networking;
namespace Fantasy.Unity.Download
{
public abstract class AUnityDownload : IDisposable
{
private long _timeId;
private ulong _totalDownloadedBytes;
private Download _download;
protected UnityWebRequest UnityWebRequest;
private FCancellationToken _cancellationToken;
private Scene Scene;
protected AUnityDownload(Scene scene,Download download)
{
Scene = scene;
_download = download;
_download.Tasks.Add(this);
}
protected UnityWebRequestAsyncOperation Start(UnityWebRequest unityWebRequest, bool monitor)
{
UnityWebRequest = unityWebRequest;
_timeId = Scene.TimerComponent.Unity.RepeatedTimer(33, Update);
return UnityWebRequest.SendWebRequest();
}
private void Update()
{
var downloadSpeed = UnityWebRequest.downloadedBytes - _totalDownloadedBytes;
_download.DownloadSpeed += downloadSpeed;
_download.TotalDownloadedBytes += downloadSpeed;
_totalDownloadedBytes = UnityWebRequest.downloadedBytes;
}
public virtual void Dispose()
{
Update();
_totalDownloadedBytes = 0;
UnityWebRequest?.Dispose();
_download.Tasks.Remove(this);
Scene.TimerComponent.Unity.Remove(ref _timeId);
_download = null;
}
}
}
#endif

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: c5743903d34d474a818b5c2bafa31459
timeCreated: 1726021902

View File

@@ -0,0 +1,72 @@
#if FANTASY_UNITY
using System.Collections.Generic;
using System.Linq;
using Fantasy.Async;
using UnityEngine;
namespace Fantasy.Unity.Download
{
public sealed class Download
{
public Scene Scene;
public ulong DownloadSpeed;
public ulong TotalDownloadedBytes;
public readonly HashSet<AUnityDownload> Tasks = new();
public static Download Create(Scene scene) => new Download(scene);
private Download(Scene scene)
{
Scene = scene;
}
public void Clear()
{
DownloadSpeed = 0;
TotalDownloadedBytes = 0;
if (Tasks.Count <= 0)
{
return;
}
foreach (var aUnityDownload in Tasks.ToArray())
{
aUnityDownload.Dispose();
}
Tasks.Clear();
}
public FTask<AssetBundle> DownloadAssetBundle(string url, bool monitor = false, FCancellationToken cancellationToken = null)
{
return new DownloadAssetBundle(Scene, this).StartDownload(url, monitor, cancellationToken);
}
public FTask<AudioClip> DownloadAudioClip(string url, AudioType audioType, bool monitor = false, FCancellationToken cancellationToken = null)
{
return new DownloadAudioClip(Scene, this).StartDownload(url, audioType, monitor, cancellationToken);
}
public FTask<Sprite> DownloadSprite(string url, bool monitor = false, FCancellationToken cancellationToken = null)
{
return new DownloadSprite(Scene, this).StartDownload(url, monitor, cancellationToken);
}
public FTask<Texture> DownloadTexture(string url, bool monitor = false, FCancellationToken cancellationToken = null)
{
return new DownloadTexture(Scene, this).StartDownload(url, monitor, cancellationToken);
}
public FTask<string> DownloadText(string url, bool monitor = false, FCancellationToken cancellationToken = null)
{
return new DownloadText(Scene, this).StartDownload(url, monitor, cancellationToken);
}
public FTask<byte[]> DownloadByte(string url, bool monitor = false, FCancellationToken cancellationToken = null)
{
return new DownloadByte(Scene, this).StartDownload(url, monitor, cancellationToken);
}
}
}
#endif

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 5715816370e84842aaab799c9776a5e4
timeCreated: 1726023436

View File

@@ -0,0 +1,54 @@
#if FANTASY_UNITY
using System;
using Fantasy.Async;
using UnityEngine;
using UnityEngine.Networking;
namespace Fantasy.Unity.Download
{
public sealed class DownloadAssetBundle : AUnityDownload
{
public DownloadAssetBundle(Scene scene, Download download) : base(scene, download)
{
}
public FTask<AssetBundle> StartDownload(string url, bool monitor, FCancellationToken cancellationToken = null)
{
var task = FTask<AssetBundle>.Create(false);
var unityWebRequestAsyncOperation = Start(UnityWebRequestAssetBundle.GetAssetBundle(Uri.EscapeUriString(url)), monitor);
if (cancellationToken != null)
{
cancellationToken.Add(() =>
{
Dispose();
task.SetResult(null);
});
}
unityWebRequestAsyncOperation.completed += operation =>
{
try
{
if (UnityWebRequest.result == UnityWebRequest.Result.Success)
{
var assetBundle = DownloadHandlerAssetBundle.GetContent(UnityWebRequest);
task.SetResult(assetBundle);
}
else
{
Log.Error(UnityWebRequest.error);
task.SetResult(null);
}
}
finally
{
Dispose();
}
};
return task;
}
}
}
#endif

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 07cbb9a010ed4fe1b90919f81847b9ea
timeCreated: 1726023471

View File

@@ -0,0 +1,54 @@
#if FANTASY_UNITY
using System;
using Fantasy.Async;
using UnityEngine;
using UnityEngine.Networking;
namespace Fantasy.Unity.Download
{
public sealed class DownloadAudioClip : AUnityDownload
{
public DownloadAudioClip(Scene scene, Download download) : base(scene, download)
{
}
public FTask<AudioClip> StartDownload(string url, AudioType audioType, bool monitor, FCancellationToken cancellationToken = null)
{
var task = FTask<AudioClip>.Create(false);
var unityWebRequestAsyncOperation = Start(UnityWebRequestMultimedia.GetAudioClip(Uri.EscapeUriString(url), audioType), monitor);
if (cancellationToken != null)
{
cancellationToken.Add(() =>
{
Dispose();
task.SetResult(null);
});
}
unityWebRequestAsyncOperation.completed += operation =>
{
try
{
if (UnityWebRequest.result == UnityWebRequest.Result.Success)
{
var audioClip = DownloadHandlerAudioClip.GetContent(UnityWebRequest);
task.SetResult(audioClip);
}
else
{
Log.Error(UnityWebRequest.error);
task.SetResult(null);
}
}
finally
{
Dispose();
}
};
return task;
}
}
}
#endif

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 66d3739ec33845148534e6ecaf134b73
timeCreated: 1726023476

View File

@@ -0,0 +1,52 @@
#if FANTASY_UNITY
using Fantasy.Async;
using UnityEngine.Networking;
namespace Fantasy.Unity.Download
{
public sealed class DownloadByte : AUnityDownload
{
public DownloadByte(Scene scene, Download download) : base(scene, download)
{
}
public FTask<byte[]> StartDownload(string url, bool monitor, FCancellationToken cancellationToken = null)
{
var task = FTask<byte[]>.Create(false);
var unityWebRequestAsyncOperation = Start(UnityWebRequest.Get(url), monitor);
if (cancellationToken != null)
{
cancellationToken.Add(() =>
{
Dispose();
task.SetResult(null);
});
}
unityWebRequestAsyncOperation.completed += operation =>
{
try
{
if (UnityWebRequest.result == UnityWebRequest.Result.Success)
{
var bytes = UnityWebRequest.downloadHandler.data;
task.SetResult(bytes);
}
else
{
Log.Error(UnityWebRequest.error);
task.SetResult(null);
}
}
finally
{
Dispose();
}
};
return task;
}
}
}
#endif

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: ae87f3ea9f4e4c9ebabedf45b0bb83b1
timeCreated: 1726023481

View File

@@ -0,0 +1,55 @@
#if FANTASY_UNITY
using System;
using Fantasy.Async;
using UnityEngine;
using UnityEngine.Networking;
namespace Fantasy.Unity.Download
{
public sealed class DownloadSprite : AUnityDownload
{
public DownloadSprite(Scene scene, Download download) : base(scene, download)
{
}
public FTask<Sprite> StartDownload(string url, bool monitor, FCancellationToken cancellationToken = null)
{
var task = FTask<Sprite>.Create(false);
var unityWebRequestAsyncOperation = Start(UnityWebRequestTexture.GetTexture(Uri.EscapeUriString(url)), monitor);
if (cancellationToken != null)
{
cancellationToken.Add(() =>
{
Dispose();
task.SetResult(null);
});
}
unityWebRequestAsyncOperation.completed += operation =>
{
try
{
if (UnityWebRequest.result == UnityWebRequest.Result.Success)
{
var texture = DownloadHandlerTexture.GetContent(UnityWebRequest);
var sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.one * 5, 1f);
task.SetResult(sprite);
}
else
{
Log.Error(UnityWebRequest.error);
task.SetResult(null);
}
}
finally
{
Dispose();
}
};
return task;
}
}
}
#endif

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 0c2a0f442e974169b7d8b7a5878fe0e6
timeCreated: 1726023487

View File

@@ -0,0 +1,52 @@
#if FANTASY_UNITY
using Fantasy.Async;
using UnityEngine.Networking;
namespace Fantasy.Unity.Download
{
public sealed class DownloadText : AUnityDownload
{
public DownloadText(Scene scene, Download download) : base(scene, download)
{
}
public FTask<string> StartDownload(string url, bool monitor, FCancellationToken cancellationToken = null)
{
var task = FTask<string>.Create(false);
var unityWebRequestAsyncOperation = Start(UnityWebRequest.Get(url), monitor);
if (cancellationToken != null)
{
cancellationToken.Add(() =>
{
Dispose();
task.SetResult(null);
});
}
unityWebRequestAsyncOperation.completed += operation =>
{
try
{
if (UnityWebRequest.result == UnityWebRequest.Result.Success)
{
var text = UnityWebRequest.downloadHandler.text;
task.SetResult(text);
}
else
{
Log.Error(UnityWebRequest.error);
task.SetResult(null);
}
}
finally
{
Dispose();
}
};
return task;
}
}
}
#endif

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4284aafa8572453cb75920d2d58e9c50
timeCreated: 1726023491

View File

@@ -0,0 +1,54 @@
#if FANTASY_UNITY
using System;
using Fantasy.Async;
using UnityEngine;
using UnityEngine.Networking;
namespace Fantasy.Unity.Download
{
public sealed class DownloadTexture : AUnityDownload
{
public DownloadTexture(Scene scene, Download download) : base(scene, download)
{
}
public FTask<Texture> StartDownload(string url, bool monitor, FCancellationToken cancellationToken = null)
{
var task = FTask<Texture>.Create(false);
var unityWebRequestAsyncOperation = Start(UnityWebRequestTexture.GetTexture(Uri.EscapeUriString(url)), monitor);
if (cancellationToken != null)
{
cancellationToken.Add(() =>
{
Dispose();
task.SetResult(null);
});
}
unityWebRequestAsyncOperation.completed += operation =>
{
try
{
if (UnityWebRequest.result == UnityWebRequest.Result.Success)
{
var texture = DownloadHandlerTexture.GetContent(UnityWebRequest);
task.SetResult(texture);
}
else
{
Log.Error(UnityWebRequest.error);
task.SetResult(null);
}
}
finally
{
Dispose();
}
};
return task;
}
}
}
#endif

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 5eaa6023378844ebb51e4b80425d8a4e
timeCreated: 1726023496