初始化仓库
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
//------------------------------------------------------------
|
||||
// Game Framework
|
||||
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
|
||||
// Homepage: https://gameframework.cn/
|
||||
// Feedback: mailto:ellan@gameframework.cn
|
||||
//------------------------------------------------------------
|
||||
|
||||
using GameFramework.Download;
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UnityGameFramework.Runtime
|
||||
{
|
||||
/// <summary>
|
||||
/// 下载代理辅助器基类。
|
||||
/// </summary>
|
||||
public abstract class DownloadAgentHelperBase : MonoBehaviour, IDownloadAgentHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 范围不适用错误码。
|
||||
/// </summary>
|
||||
protected const int RangeNotSatisfiableErrorCode = 416;
|
||||
|
||||
/// <summary>
|
||||
/// 下载代理辅助器更新数据流事件。
|
||||
/// </summary>
|
||||
public abstract event EventHandler<DownloadAgentHelperUpdateBytesEventArgs> DownloadAgentHelperUpdateBytes;
|
||||
|
||||
/// <summary>
|
||||
/// 下载代理辅助器更新数据大小事件。
|
||||
/// </summary>
|
||||
public abstract event EventHandler<DownloadAgentHelperUpdateLengthEventArgs> DownloadAgentHelperUpdateLength;
|
||||
|
||||
/// <summary>
|
||||
/// 下载代理辅助器完成事件。
|
||||
/// </summary>
|
||||
public abstract event EventHandler<DownloadAgentHelperCompleteEventArgs> DownloadAgentHelperComplete;
|
||||
|
||||
/// <summary>
|
||||
/// 下载代理辅助器错误事件。
|
||||
/// </summary>
|
||||
public abstract event EventHandler<DownloadAgentHelperErrorEventArgs> DownloadAgentHelperError;
|
||||
|
||||
/// <summary>
|
||||
/// 通过下载代理辅助器下载指定地址的数据。
|
||||
/// </summary>
|
||||
/// <param name="downloadUri">下载地址。</param>
|
||||
/// <param name="userData">用户自定义数据。</param>
|
||||
public abstract void Download(string downloadUri, object userData);
|
||||
|
||||
/// <summary>
|
||||
/// 通过下载代理辅助器下载指定地址的数据。
|
||||
/// </summary>
|
||||
/// <param name="downloadUri">下载地址。</param>
|
||||
/// <param name="fromPosition">下载数据起始位置。</param>
|
||||
/// <param name="userData">用户自定义数据。</param>
|
||||
public abstract void Download(string downloadUri, long fromPosition, object userData);
|
||||
|
||||
/// <summary>
|
||||
/// 通过下载代理辅助器下载指定地址的数据。
|
||||
/// </summary>
|
||||
/// <param name="downloadUri">下载地址。</param>
|
||||
/// <param name="fromPosition">下载数据起始位置。</param>
|
||||
/// <param name="toPosition">下载数据结束位置。</param>
|
||||
/// <param name="userData">用户自定义数据。</param>
|
||||
public abstract void Download(string downloadUri, long fromPosition, long toPosition, object userData);
|
||||
|
||||
/// <summary>
|
||||
/// 重置下载代理辅助器。
|
||||
/// </summary>
|
||||
public abstract void Reset();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0c6e60daac172714dba741765e1fdd97
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,409 @@
|
||||
//------------------------------------------------------------
|
||||
// Game Framework
|
||||
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
|
||||
// Homepage: https://gameframework.cn/
|
||||
// Feedback: mailto:ellan@gameframework.cn
|
||||
//------------------------------------------------------------
|
||||
|
||||
using GameFramework;
|
||||
using GameFramework.Download;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UnityGameFramework.Runtime
|
||||
{
|
||||
/// <summary>
|
||||
/// 下载组件。
|
||||
/// </summary>
|
||||
[DisallowMultipleComponent]
|
||||
[AddComponentMenu("Game Framework/Download")]
|
||||
public sealed class DownloadComponent : GameFrameworkComponent
|
||||
{
|
||||
private const int DefaultPriority = 0;
|
||||
private const int OneMegaBytes = 1024 * 1024;
|
||||
|
||||
private IDownloadManager m_DownloadManager = null;
|
||||
private EventComponent m_EventComponent = null;
|
||||
|
||||
[SerializeField]
|
||||
private Transform m_InstanceRoot = null;
|
||||
|
||||
[SerializeField]
|
||||
private string m_DownloadAgentHelperTypeName = "UnityGameFramework.Runtime.UnityWebRequestDownloadAgentHelper";
|
||||
|
||||
[SerializeField]
|
||||
private DownloadAgentHelperBase m_CustomDownloadAgentHelper = null;
|
||||
|
||||
[SerializeField]
|
||||
private int m_DownloadAgentHelperCount = 3;
|
||||
|
||||
[SerializeField]
|
||||
private float m_Timeout = 30f;
|
||||
|
||||
[SerializeField]
|
||||
private int m_FlushSize = OneMegaBytes;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置下载是否被暂停。
|
||||
/// </summary>
|
||||
public bool Paused
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_DownloadManager.Paused;
|
||||
}
|
||||
set
|
||||
{
|
||||
m_DownloadManager.Paused = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取下载代理总数量。
|
||||
/// </summary>
|
||||
public int TotalAgentCount
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_DownloadManager.TotalAgentCount;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取可用下载代理数量。
|
||||
/// </summary>
|
||||
public int FreeAgentCount
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_DownloadManager.FreeAgentCount;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取工作中下载代理数量。
|
||||
/// </summary>
|
||||
public int WorkingAgentCount
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_DownloadManager.WorkingAgentCount;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取等待下载任务数量。
|
||||
/// </summary>
|
||||
public int WaitingTaskCount
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_DownloadManager.WaitingTaskCount;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置下载超时时长,以秒为单位。
|
||||
/// </summary>
|
||||
public float Timeout
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_DownloadManager.Timeout;
|
||||
}
|
||||
set
|
||||
{
|
||||
m_DownloadManager.Timeout = m_Timeout = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置将缓冲区写入磁盘的临界大小,仅当开启断点续传时有效。
|
||||
/// </summary>
|
||||
public int FlushSize
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_DownloadManager.FlushSize;
|
||||
}
|
||||
set
|
||||
{
|
||||
m_DownloadManager.FlushSize = m_FlushSize = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前下载速度。
|
||||
/// </summary>
|
||||
public float CurrentSpeed
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_DownloadManager.CurrentSpeed;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 游戏框架组件初始化。
|
||||
/// </summary>
|
||||
protected override void Awake()
|
||||
{
|
||||
base.Awake();
|
||||
|
||||
m_DownloadManager = GameFrameworkEntry.GetModule<IDownloadManager>();
|
||||
if (m_DownloadManager == null)
|
||||
{
|
||||
Log.Fatal("Download manager is invalid.");
|
||||
return;
|
||||
}
|
||||
|
||||
m_DownloadManager.DownloadStart += OnDownloadStart;
|
||||
m_DownloadManager.DownloadUpdate += OnDownloadUpdate;
|
||||
m_DownloadManager.DownloadSuccess += OnDownloadSuccess;
|
||||
m_DownloadManager.DownloadFailure += OnDownloadFailure;
|
||||
m_DownloadManager.FlushSize = m_FlushSize;
|
||||
m_DownloadManager.Timeout = m_Timeout;
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
m_EventComponent = GameEntry.GetComponent<EventComponent>();
|
||||
if (m_EventComponent == null)
|
||||
{
|
||||
Log.Fatal("Event component is invalid.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_InstanceRoot == null)
|
||||
{
|
||||
m_InstanceRoot = new GameObject("Download Agent Instances").transform;
|
||||
m_InstanceRoot.SetParent(gameObject.transform);
|
||||
m_InstanceRoot.localScale = Vector3.one;
|
||||
}
|
||||
|
||||
for (int i = 0; i < m_DownloadAgentHelperCount; i++)
|
||||
{
|
||||
AddDownloadAgentHelper(i);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据下载任务的序列编号获取下载任务的信息。
|
||||
/// </summary>
|
||||
/// <param name="serialId">要获取信息的下载任务的序列编号。</param>
|
||||
/// <returns>下载任务的信息。</returns>
|
||||
public TaskInfo GetDownloadInfo(int serialId)
|
||||
{
|
||||
return m_DownloadManager.GetDownloadInfo(serialId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据下载任务的标签获取下载任务的信息。
|
||||
/// </summary>
|
||||
/// <param name="tag">要获取信息的下载任务的标签。</param>
|
||||
/// <returns>下载任务的信息。</returns>
|
||||
public TaskInfo[] GetDownloadInfos(string tag)
|
||||
{
|
||||
return m_DownloadManager.GetDownloadInfos(tag);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据下载任务的标签获取下载任务的信息。
|
||||
/// </summary>
|
||||
/// <param name="tag">要获取信息的下载任务的标签。</param>
|
||||
/// <param name="results">下载任务的信息。</param>
|
||||
public void GetDownloadInfos(string tag, List<TaskInfo> results)
|
||||
{
|
||||
m_DownloadManager.GetDownloadInfos(tag, results);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有下载任务的信息。
|
||||
/// </summary>
|
||||
/// <returns>所有下载任务的信息。</returns>
|
||||
public TaskInfo[] GetAllDownloadInfos()
|
||||
{
|
||||
return m_DownloadManager.GetAllDownloadInfos();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有下载任务的信息。
|
||||
/// </summary>
|
||||
/// <param name="results">所有下载任务的信息。</param>
|
||||
public void GetAllDownloadInfos(List<TaskInfo> results)
|
||||
{
|
||||
m_DownloadManager.GetAllDownloadInfos(results);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 增加下载任务。
|
||||
/// </summary>
|
||||
/// <param name="downloadPath">下载后存放路径。</param>
|
||||
/// <param name="downloadUri">原始下载地址。</param>
|
||||
/// <returns>新增下载任务的序列编号。</returns>
|
||||
public int AddDownload(string downloadPath, string downloadUri)
|
||||
{
|
||||
return AddDownload(downloadPath, downloadUri, null, DefaultPriority, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 增加下载任务。
|
||||
/// </summary>
|
||||
/// <param name="downloadPath">下载后存放路径。</param>
|
||||
/// <param name="downloadUri">原始下载地址。</param>
|
||||
/// <param name="tag">下载任务的标签。</param>
|
||||
/// <returns>新增下载任务的序列编号。</returns>
|
||||
public int AddDownload(string downloadPath, string downloadUri, string tag)
|
||||
{
|
||||
return AddDownload(downloadPath, downloadUri, tag, DefaultPriority, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 增加下载任务。
|
||||
/// </summary>
|
||||
/// <param name="downloadPath">下载后存放路径。</param>
|
||||
/// <param name="downloadUri">原始下载地址。</param>
|
||||
/// <param name="priority">下载任务的优先级。</param>
|
||||
/// <returns>新增下载任务的序列编号。</returns>
|
||||
public int AddDownload(string downloadPath, string downloadUri, int priority)
|
||||
{
|
||||
return AddDownload(downloadPath, downloadUri, null, priority, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 增加下载任务。
|
||||
/// </summary>
|
||||
/// <param name="downloadPath">下载后存放路径。</param>
|
||||
/// <param name="downloadUri">原始下载地址。</param>
|
||||
/// <param name="userData">用户自定义数据。</param>
|
||||
/// <returns>新增下载任务的序列编号。</returns>
|
||||
public int AddDownload(string downloadPath, string downloadUri, object userData)
|
||||
{
|
||||
return AddDownload(downloadPath, downloadUri, null, DefaultPriority, userData);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 增加下载任务。
|
||||
/// </summary>
|
||||
/// <param name="downloadPath">下载后存放路径。</param>
|
||||
/// <param name="downloadUri">原始下载地址。</param>
|
||||
/// <param name="tag">下载任务的标签。</param>
|
||||
/// <param name="priority">下载任务的优先级。</param>
|
||||
/// <returns>新增下载任务的序列编号。</returns>
|
||||
public int AddDownload(string downloadPath, string downloadUri, string tag, int priority)
|
||||
{
|
||||
return AddDownload(downloadPath, downloadUri, tag, priority, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 增加下载任务。
|
||||
/// </summary>
|
||||
/// <param name="downloadPath">下载后存放路径。</param>
|
||||
/// <param name="downloadUri">原始下载地址。</param>
|
||||
/// <param name="tag">下载任务的标签。</param>
|
||||
/// <param name="userData">用户自定义数据。</param>
|
||||
/// <returns>新增下载任务的序列编号。</returns>
|
||||
public int AddDownload(string downloadPath, string downloadUri, string tag, object userData)
|
||||
{
|
||||
return AddDownload(downloadPath, downloadUri, tag, DefaultPriority, userData);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 增加下载任务。
|
||||
/// </summary>
|
||||
/// <param name="downloadPath">下载后存放路径。</param>
|
||||
/// <param name="downloadUri">原始下载地址。</param>
|
||||
/// <param name="priority">下载任务的优先级。</param>
|
||||
/// <param name="userData">用户自定义数据。</param>
|
||||
/// <returns>新增下载任务的序列编号。</returns>
|
||||
public int AddDownload(string downloadPath, string downloadUri, int priority, object userData)
|
||||
{
|
||||
return AddDownload(downloadPath, downloadUri, null, priority, userData);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 增加下载任务。
|
||||
/// </summary>
|
||||
/// <param name="downloadPath">下载后存放路径。</param>
|
||||
/// <param name="downloadUri">原始下载地址。</param>
|
||||
/// <param name="tag">下载任务的标签。</param>
|
||||
/// <param name="priority">下载任务的优先级。</param>
|
||||
/// <param name="userData">用户自定义数据。</param>
|
||||
/// <returns>新增下载任务的序列编号。</returns>
|
||||
public int AddDownload(string downloadPath, string downloadUri, string tag, int priority, object userData)
|
||||
{
|
||||
return m_DownloadManager.AddDownload(downloadPath, downloadUri, tag, priority, userData);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据下载任务的序列编号移除下载任务。
|
||||
/// </summary>
|
||||
/// <param name="serialId">要移除下载任务的序列编号。</param>
|
||||
/// <returns>是否移除下载任务成功。</returns>
|
||||
public bool RemoveDownload(int serialId)
|
||||
{
|
||||
return m_DownloadManager.RemoveDownload(serialId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据下载任务的标签移除下载任务。
|
||||
/// </summary>
|
||||
/// <param name="tag">要移除下载任务的标签。</param>
|
||||
/// <returns>移除下载任务的数量。</returns>
|
||||
public int RemoveDownloads(string tag)
|
||||
{
|
||||
return m_DownloadManager.RemoveDownloads(tag);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除所有下载任务。
|
||||
/// </summary>
|
||||
/// <returns>移除下载任务的数量。</returns>
|
||||
public int RemoveAllDownloads()
|
||||
{
|
||||
return m_DownloadManager.RemoveAllDownloads();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 增加下载代理辅助器。
|
||||
/// </summary>
|
||||
/// <param name="index">下载代理辅助器索引。</param>
|
||||
private void AddDownloadAgentHelper(int index)
|
||||
{
|
||||
DownloadAgentHelperBase downloadAgentHelper = Helper.CreateHelper(m_DownloadAgentHelperTypeName, m_CustomDownloadAgentHelper, index);
|
||||
if (downloadAgentHelper == null)
|
||||
{
|
||||
Log.Error("Can not create download agent helper.");
|
||||
return;
|
||||
}
|
||||
|
||||
downloadAgentHelper.name = Utility.Text.Format("Download Agent Helper - {0}", index);
|
||||
Transform transform = downloadAgentHelper.transform;
|
||||
transform.SetParent(m_InstanceRoot);
|
||||
transform.localScale = Vector3.one;
|
||||
|
||||
m_DownloadManager.AddDownloadAgentHelper(downloadAgentHelper);
|
||||
}
|
||||
|
||||
private void OnDownloadStart(object sender, GameFramework.Download.DownloadStartEventArgs e)
|
||||
{
|
||||
m_EventComponent.Fire(this, DownloadStartEventArgs.Create(e));
|
||||
}
|
||||
|
||||
private void OnDownloadUpdate(object sender, GameFramework.Download.DownloadUpdateEventArgs e)
|
||||
{
|
||||
m_EventComponent.Fire(this, DownloadUpdateEventArgs.Create(e));
|
||||
}
|
||||
|
||||
private void OnDownloadSuccess(object sender, GameFramework.Download.DownloadSuccessEventArgs e)
|
||||
{
|
||||
m_EventComponent.Fire(this, DownloadSuccessEventArgs.Create(e));
|
||||
}
|
||||
|
||||
private void OnDownloadFailure(object sender, GameFramework.Download.DownloadFailureEventArgs e)
|
||||
{
|
||||
Log.Warning("Download failure, download serial id '{0}', download path '{1}', download uri '{2}', error message '{3}'.", e.SerialId, e.DownloadPath, e.DownloadUri, e.ErrorMessage);
|
||||
m_EventComponent.Fire(this, DownloadFailureEventArgs.Create(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9259de0854ba2624aa59aca15c6af063
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,119 @@
|
||||
//------------------------------------------------------------
|
||||
// Game Framework
|
||||
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
|
||||
// Homepage: https://gameframework.cn/
|
||||
// Feedback: mailto:ellan@gameframework.cn
|
||||
//------------------------------------------------------------
|
||||
|
||||
using GameFramework;
|
||||
using GameFramework.Event;
|
||||
|
||||
namespace UnityGameFramework.Runtime
|
||||
{
|
||||
/// <summary>
|
||||
/// 下载失败事件。
|
||||
/// </summary>
|
||||
public sealed class DownloadFailureEventArgs : GameEventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// 下载失败事件编号。
|
||||
/// </summary>
|
||||
public static readonly int EventId = typeof(DownloadFailureEventArgs).GetHashCode();
|
||||
|
||||
/// <summary>
|
||||
/// 初始化下载失败事件的新实例。
|
||||
/// </summary>
|
||||
public DownloadFailureEventArgs()
|
||||
{
|
||||
SerialId = 0;
|
||||
DownloadPath = null;
|
||||
DownloadUri = null;
|
||||
ErrorMessage = null;
|
||||
UserData = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取下载失败事件编号。
|
||||
/// </summary>
|
||||
public override int Id
|
||||
{
|
||||
get
|
||||
{
|
||||
return EventId;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取下载任务的序列编号。
|
||||
/// </summary>
|
||||
public int SerialId
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取下载后存放路径。
|
||||
/// </summary>
|
||||
public string DownloadPath
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取下载地址。
|
||||
/// </summary>
|
||||
public string DownloadUri
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取错误信息。
|
||||
/// </summary>
|
||||
public string ErrorMessage
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取用户自定义数据。
|
||||
/// </summary>
|
||||
public object UserData
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建下载失败事件。
|
||||
/// </summary>
|
||||
/// <param name="e">内部事件。</param>
|
||||
/// <returns>创建的下载失败事件。</returns>
|
||||
public static DownloadFailureEventArgs Create(GameFramework.Download.DownloadFailureEventArgs e)
|
||||
{
|
||||
DownloadFailureEventArgs downloadFailureEventArgs = ReferencePool.Acquire<DownloadFailureEventArgs>();
|
||||
downloadFailureEventArgs.SerialId = e.SerialId;
|
||||
downloadFailureEventArgs.DownloadPath = e.DownloadPath;
|
||||
downloadFailureEventArgs.DownloadUri = e.DownloadUri;
|
||||
downloadFailureEventArgs.ErrorMessage = e.ErrorMessage;
|
||||
downloadFailureEventArgs.UserData = e.UserData;
|
||||
return downloadFailureEventArgs;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清理下载失败事件。
|
||||
/// </summary>
|
||||
public override void Clear()
|
||||
{
|
||||
SerialId = 0;
|
||||
DownloadPath = null;
|
||||
DownloadUri = null;
|
||||
ErrorMessage = null;
|
||||
UserData = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2b3227ba2aea8444397bb69d11e667fb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,119 @@
|
||||
//------------------------------------------------------------
|
||||
// Game Framework
|
||||
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
|
||||
// Homepage: https://gameframework.cn/
|
||||
// Feedback: mailto:ellan@gameframework.cn
|
||||
//------------------------------------------------------------
|
||||
|
||||
using GameFramework;
|
||||
using GameFramework.Event;
|
||||
|
||||
namespace UnityGameFramework.Runtime
|
||||
{
|
||||
/// <summary>
|
||||
/// 下载开始事件。
|
||||
/// </summary>
|
||||
public sealed class DownloadStartEventArgs : GameEventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// 下载开始事件编号。
|
||||
/// </summary>
|
||||
public static readonly int EventId = typeof(DownloadStartEventArgs).GetHashCode();
|
||||
|
||||
/// <summary>
|
||||
/// 初始化下载开始事件的新实例。
|
||||
/// </summary>
|
||||
public DownloadStartEventArgs()
|
||||
{
|
||||
SerialId = 0;
|
||||
DownloadPath = null;
|
||||
DownloadUri = null;
|
||||
CurrentLength = 0L;
|
||||
UserData = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取下载开始事件编号。
|
||||
/// </summary>
|
||||
public override int Id
|
||||
{
|
||||
get
|
||||
{
|
||||
return EventId;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取下载任务的序列编号。
|
||||
/// </summary>
|
||||
public int SerialId
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取下载后存放路径。
|
||||
/// </summary>
|
||||
public string DownloadPath
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取下载地址。
|
||||
/// </summary>
|
||||
public string DownloadUri
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前大小。
|
||||
/// </summary>
|
||||
public long CurrentLength
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取用户自定义数据。
|
||||
/// </summary>
|
||||
public object UserData
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建下载开始事件。
|
||||
/// </summary>
|
||||
/// <param name="e">内部事件。</param>
|
||||
/// <returns>创建的下载开始事件。</returns>
|
||||
public static DownloadStartEventArgs Create(GameFramework.Download.DownloadStartEventArgs e)
|
||||
{
|
||||
DownloadStartEventArgs downloadStartEventArgs = ReferencePool.Acquire<DownloadStartEventArgs>();
|
||||
downloadStartEventArgs.SerialId = e.SerialId;
|
||||
downloadStartEventArgs.DownloadPath = e.DownloadPath;
|
||||
downloadStartEventArgs.DownloadUri = e.DownloadUri;
|
||||
downloadStartEventArgs.CurrentLength = e.CurrentLength;
|
||||
downloadStartEventArgs.UserData = e.UserData;
|
||||
return downloadStartEventArgs;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清理下载开始事件。
|
||||
/// </summary>
|
||||
public override void Clear()
|
||||
{
|
||||
SerialId = 0;
|
||||
DownloadPath = null;
|
||||
DownloadUri = null;
|
||||
CurrentLength = 0L;
|
||||
UserData = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ee21721798f32c7459a235204468db6c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,119 @@
|
||||
//------------------------------------------------------------
|
||||
// Game Framework
|
||||
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
|
||||
// Homepage: https://gameframework.cn/
|
||||
// Feedback: mailto:ellan@gameframework.cn
|
||||
//------------------------------------------------------------
|
||||
|
||||
using GameFramework;
|
||||
using GameFramework.Event;
|
||||
|
||||
namespace UnityGameFramework.Runtime
|
||||
{
|
||||
/// <summary>
|
||||
/// 下载成功事件。
|
||||
/// </summary>
|
||||
public sealed class DownloadSuccessEventArgs : GameEventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// 下载成功事件编号。
|
||||
/// </summary>
|
||||
public static readonly int EventId = typeof(DownloadSuccessEventArgs).GetHashCode();
|
||||
|
||||
/// <summary>
|
||||
/// 初始化下载成功事件的新实例。
|
||||
/// </summary>
|
||||
public DownloadSuccessEventArgs()
|
||||
{
|
||||
SerialId = 0;
|
||||
DownloadPath = null;
|
||||
DownloadUri = null;
|
||||
CurrentLength = 0L;
|
||||
UserData = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取下载成功事件编号。
|
||||
/// </summary>
|
||||
public override int Id
|
||||
{
|
||||
get
|
||||
{
|
||||
return EventId;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取下载任务的序列编号。
|
||||
/// </summary>
|
||||
public int SerialId
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取下载后存放路径。
|
||||
/// </summary>
|
||||
public string DownloadPath
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取下载地址。
|
||||
/// </summary>
|
||||
public string DownloadUri
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前大小。
|
||||
/// </summary>
|
||||
public long CurrentLength
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取用户自定义数据。
|
||||
/// </summary>
|
||||
public object UserData
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建下载成功事件。
|
||||
/// </summary>
|
||||
/// <param name="e">内部事件。</param>
|
||||
/// <returns>创建的下载成功事件。</returns>
|
||||
public static DownloadSuccessEventArgs Create(GameFramework.Download.DownloadSuccessEventArgs e)
|
||||
{
|
||||
DownloadSuccessEventArgs downloadSuccessEventArgs = ReferencePool.Acquire<DownloadSuccessEventArgs>();
|
||||
downloadSuccessEventArgs.SerialId = e.SerialId;
|
||||
downloadSuccessEventArgs.DownloadPath = e.DownloadPath;
|
||||
downloadSuccessEventArgs.DownloadUri = e.DownloadUri;
|
||||
downloadSuccessEventArgs.CurrentLength = e.CurrentLength;
|
||||
downloadSuccessEventArgs.UserData = e.UserData;
|
||||
return downloadSuccessEventArgs;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清理下载成功事件。
|
||||
/// </summary>
|
||||
public override void Clear()
|
||||
{
|
||||
SerialId = 0;
|
||||
DownloadPath = null;
|
||||
DownloadUri = null;
|
||||
CurrentLength = 0L;
|
||||
UserData = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b0d6ca8ee116c7c43b319992b5a4cdbf
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,119 @@
|
||||
//------------------------------------------------------------
|
||||
// Game Framework
|
||||
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
|
||||
// Homepage: https://gameframework.cn/
|
||||
// Feedback: mailto:ellan@gameframework.cn
|
||||
//------------------------------------------------------------
|
||||
|
||||
using GameFramework;
|
||||
using GameFramework.Event;
|
||||
|
||||
namespace UnityGameFramework.Runtime
|
||||
{
|
||||
/// <summary>
|
||||
/// 下载更新事件。
|
||||
/// </summary>
|
||||
public sealed class DownloadUpdateEventArgs : GameEventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// 下载更新事件编号。
|
||||
/// </summary>
|
||||
public static readonly int EventId = typeof(DownloadUpdateEventArgs).GetHashCode();
|
||||
|
||||
/// <summary>
|
||||
/// 初始化下载更新事件的新实例。
|
||||
/// </summary>
|
||||
public DownloadUpdateEventArgs()
|
||||
{
|
||||
SerialId = 0;
|
||||
DownloadPath = null;
|
||||
DownloadUri = null;
|
||||
CurrentLength = 0L;
|
||||
UserData = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取下载更新事件编号。
|
||||
/// </summary>
|
||||
public override int Id
|
||||
{
|
||||
get
|
||||
{
|
||||
return EventId;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取下载任务的序列编号。
|
||||
/// </summary>
|
||||
public int SerialId
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取下载后存放路径。
|
||||
/// </summary>
|
||||
public string DownloadPath
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取下载地址。
|
||||
/// </summary>
|
||||
public string DownloadUri
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前大小。
|
||||
/// </summary>
|
||||
public long CurrentLength
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取用户自定义数据。
|
||||
/// </summary>
|
||||
public object UserData
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建下载更新事件。
|
||||
/// </summary>
|
||||
/// <param name="e">内部事件。</param>
|
||||
/// <returns>创建的下载更新事件。</returns>
|
||||
public static DownloadUpdateEventArgs Create(GameFramework.Download.DownloadUpdateEventArgs e)
|
||||
{
|
||||
DownloadUpdateEventArgs downloadUpdateEventArgs = ReferencePool.Acquire<DownloadUpdateEventArgs>();
|
||||
downloadUpdateEventArgs.SerialId = e.SerialId;
|
||||
downloadUpdateEventArgs.DownloadPath = e.DownloadPath;
|
||||
downloadUpdateEventArgs.DownloadUri = e.DownloadUri;
|
||||
downloadUpdateEventArgs.CurrentLength = e.CurrentLength;
|
||||
downloadUpdateEventArgs.UserData = e.UserData;
|
||||
return downloadUpdateEventArgs;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清理下载更新事件。
|
||||
/// </summary>
|
||||
public override void Clear()
|
||||
{
|
||||
SerialId = 0;
|
||||
DownloadPath = null;
|
||||
DownloadUri = null;
|
||||
CurrentLength = 0L;
|
||||
UserData = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: da30c910f3da16f4cb29770ee035496b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
//------------------------------------------------------------
|
||||
// Game Framework
|
||||
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
|
||||
// Homepage: https://gameframework.cn/
|
||||
// Feedback: mailto:ellan@gameframework.cn
|
||||
//------------------------------------------------------------
|
||||
|
||||
using GameFramework;
|
||||
using GameFramework.Download;
|
||||
using System;
|
||||
#if UNITY_5_4_OR_NEWER
|
||||
using UnityEngine.Networking;
|
||||
#else
|
||||
using UnityEngine.Experimental.Networking;
|
||||
#endif
|
||||
|
||||
namespace UnityGameFramework.Runtime
|
||||
{
|
||||
public partial class UnityWebRequestDownloadAgentHelper : DownloadAgentHelperBase, IDisposable
|
||||
{
|
||||
private sealed class DownloadHandler : DownloadHandlerScript
|
||||
{
|
||||
private readonly UnityWebRequestDownloadAgentHelper m_Owner;
|
||||
|
||||
public DownloadHandler(UnityWebRequestDownloadAgentHelper owner)
|
||||
: base(owner.m_CachedBytes)
|
||||
{
|
||||
m_Owner = owner;
|
||||
}
|
||||
|
||||
protected override bool ReceiveData(byte[] data, int dataLength)
|
||||
{
|
||||
if (m_Owner != null && m_Owner.m_UnityWebRequest != null && dataLength > 0)
|
||||
{
|
||||
DownloadAgentHelperUpdateBytesEventArgs downloadAgentHelperUpdateBytesEventArgs = DownloadAgentHelperUpdateBytesEventArgs.Create(data, 0, dataLength);
|
||||
m_Owner.m_DownloadAgentHelperUpdateBytesEventHandler(this, downloadAgentHelperUpdateBytesEventArgs);
|
||||
ReferencePool.Release(downloadAgentHelperUpdateBytesEventArgs);
|
||||
|
||||
DownloadAgentHelperUpdateLengthEventArgs downloadAgentHelperUpdateLengthEventArgs = DownloadAgentHelperUpdateLengthEventArgs.Create(dataLength);
|
||||
m_Owner.m_DownloadAgentHelperUpdateLengthEventHandler(this, downloadAgentHelperUpdateLengthEventArgs);
|
||||
ReferencePool.Release(downloadAgentHelperUpdateLengthEventArgs);
|
||||
}
|
||||
|
||||
return base.ReceiveData(data, dataLength);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fc6b62e45f7a521499fdd86e1e9071df
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,248 @@
|
||||
//------------------------------------------------------------
|
||||
// Game Framework
|
||||
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
|
||||
// Homepage: https://gameframework.cn/
|
||||
// Feedback: mailto:ellan@gameframework.cn
|
||||
//------------------------------------------------------------
|
||||
|
||||
using GameFramework;
|
||||
using GameFramework.Download;
|
||||
using System;
|
||||
#if UNITY_5_4_OR_NEWER
|
||||
using UnityEngine.Networking;
|
||||
#else
|
||||
using UnityEngine.Experimental.Networking;
|
||||
#endif
|
||||
using Utility = GameFramework.Utility;
|
||||
|
||||
namespace UnityGameFramework.Runtime
|
||||
{
|
||||
/// <summary>
|
||||
/// 使用 UnityWebRequest 实现的下载代理辅助器。
|
||||
/// </summary>
|
||||
public partial class UnityWebRequestDownloadAgentHelper : DownloadAgentHelperBase, IDisposable
|
||||
{
|
||||
private const int CachedBytesLength = 0x1000;
|
||||
private readonly byte[] m_CachedBytes = new byte[CachedBytesLength];
|
||||
|
||||
private UnityWebRequest m_UnityWebRequest = null;
|
||||
private bool m_Disposed = false;
|
||||
|
||||
private EventHandler<DownloadAgentHelperUpdateBytesEventArgs> m_DownloadAgentHelperUpdateBytesEventHandler = null;
|
||||
private EventHandler<DownloadAgentHelperUpdateLengthEventArgs> m_DownloadAgentHelperUpdateLengthEventHandler = null;
|
||||
private EventHandler<DownloadAgentHelperCompleteEventArgs> m_DownloadAgentHelperCompleteEventHandler = null;
|
||||
private EventHandler<DownloadAgentHelperErrorEventArgs> m_DownloadAgentHelperErrorEventHandler = null;
|
||||
|
||||
/// <summary>
|
||||
/// 下载代理辅助器更新数据流事件。
|
||||
/// </summary>
|
||||
public override event EventHandler<DownloadAgentHelperUpdateBytesEventArgs> DownloadAgentHelperUpdateBytes
|
||||
{
|
||||
add
|
||||
{
|
||||
m_DownloadAgentHelperUpdateBytesEventHandler += value;
|
||||
}
|
||||
remove
|
||||
{
|
||||
m_DownloadAgentHelperUpdateBytesEventHandler -= value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 下载代理辅助器更新数据大小事件。
|
||||
/// </summary>
|
||||
public override event EventHandler<DownloadAgentHelperUpdateLengthEventArgs> DownloadAgentHelperUpdateLength
|
||||
{
|
||||
add
|
||||
{
|
||||
m_DownloadAgentHelperUpdateLengthEventHandler += value;
|
||||
}
|
||||
remove
|
||||
{
|
||||
m_DownloadAgentHelperUpdateLengthEventHandler -= value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 下载代理辅助器完成事件。
|
||||
/// </summary>
|
||||
public override event EventHandler<DownloadAgentHelperCompleteEventArgs> DownloadAgentHelperComplete
|
||||
{
|
||||
add
|
||||
{
|
||||
m_DownloadAgentHelperCompleteEventHandler += value;
|
||||
}
|
||||
remove
|
||||
{
|
||||
m_DownloadAgentHelperCompleteEventHandler -= value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 下载代理辅助器错误事件。
|
||||
/// </summary>
|
||||
public override event EventHandler<DownloadAgentHelperErrorEventArgs> DownloadAgentHelperError
|
||||
{
|
||||
add
|
||||
{
|
||||
m_DownloadAgentHelperErrorEventHandler += value;
|
||||
}
|
||||
remove
|
||||
{
|
||||
m_DownloadAgentHelperErrorEventHandler -= value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过下载代理辅助器下载指定地址的数据。
|
||||
/// </summary>
|
||||
/// <param name="downloadUri">下载地址。</param>
|
||||
/// <param name="userData">用户自定义数据。</param>
|
||||
public override void Download(string downloadUri, object userData)
|
||||
{
|
||||
if (m_DownloadAgentHelperUpdateBytesEventHandler == null || m_DownloadAgentHelperUpdateLengthEventHandler == null || m_DownloadAgentHelperCompleteEventHandler == null || m_DownloadAgentHelperErrorEventHandler == null)
|
||||
{
|
||||
Log.Fatal("Download agent helper handler is invalid.");
|
||||
return;
|
||||
}
|
||||
|
||||
m_UnityWebRequest = new UnityWebRequest(downloadUri);
|
||||
m_UnityWebRequest.downloadHandler = new DownloadHandler(this);
|
||||
#if UNITY_2017_2_OR_NEWER
|
||||
m_UnityWebRequest.SendWebRequest();
|
||||
#else
|
||||
m_UnityWebRequest.Send();
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过下载代理辅助器下载指定地址的数据。
|
||||
/// </summary>
|
||||
/// <param name="downloadUri">下载地址。</param>
|
||||
/// <param name="fromPosition">下载数据起始位置。</param>
|
||||
/// <param name="userData">用户自定义数据。</param>
|
||||
public override void Download(string downloadUri, long fromPosition, object userData)
|
||||
{
|
||||
if (m_DownloadAgentHelperUpdateBytesEventHandler == null || m_DownloadAgentHelperUpdateLengthEventHandler == null || m_DownloadAgentHelperCompleteEventHandler == null || m_DownloadAgentHelperErrorEventHandler == null)
|
||||
{
|
||||
Log.Fatal("Download agent helper handler is invalid.");
|
||||
return;
|
||||
}
|
||||
|
||||
m_UnityWebRequest = new UnityWebRequest(downloadUri);
|
||||
m_UnityWebRequest.SetRequestHeader("Range", Utility.Text.Format("bytes={0}-", fromPosition));
|
||||
m_UnityWebRequest.downloadHandler = new DownloadHandler(this);
|
||||
#if UNITY_2017_2_OR_NEWER
|
||||
m_UnityWebRequest.SendWebRequest();
|
||||
#else
|
||||
m_UnityWebRequest.Send();
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过下载代理辅助器下载指定地址的数据。
|
||||
/// </summary>
|
||||
/// <param name="downloadUri">下载地址。</param>
|
||||
/// <param name="fromPosition">下载数据起始位置。</param>
|
||||
/// <param name="toPosition">下载数据结束位置。</param>
|
||||
/// <param name="userData">用户自定义数据。</param>
|
||||
public override void Download(string downloadUri, long fromPosition, long toPosition, object userData)
|
||||
{
|
||||
if (m_DownloadAgentHelperUpdateBytesEventHandler == null || m_DownloadAgentHelperUpdateLengthEventHandler == null || m_DownloadAgentHelperCompleteEventHandler == null || m_DownloadAgentHelperErrorEventHandler == null)
|
||||
{
|
||||
Log.Fatal("Download agent helper handler is invalid.");
|
||||
return;
|
||||
}
|
||||
|
||||
m_UnityWebRequest = new UnityWebRequest(downloadUri);
|
||||
m_UnityWebRequest.SetRequestHeader("Range", Utility.Text.Format("bytes={0}-{1}", fromPosition, toPosition));
|
||||
m_UnityWebRequest.downloadHandler = new DownloadHandler(this);
|
||||
#if UNITY_2017_2_OR_NEWER
|
||||
m_UnityWebRequest.SendWebRequest();
|
||||
#else
|
||||
m_UnityWebRequest.Send();
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重置下载代理辅助器。
|
||||
/// </summary>
|
||||
public override void Reset()
|
||||
{
|
||||
if (m_UnityWebRequest != null)
|
||||
{
|
||||
m_UnityWebRequest.Abort();
|
||||
m_UnityWebRequest.Dispose();
|
||||
m_UnityWebRequest = null;
|
||||
}
|
||||
|
||||
Array.Clear(m_CachedBytes, 0, CachedBytesLength);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放资源。
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放资源。
|
||||
/// </summary>
|
||||
/// <param name="disposing">释放资源标记。</param>
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (m_Disposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (disposing)
|
||||
{
|
||||
if (m_UnityWebRequest != null)
|
||||
{
|
||||
m_UnityWebRequest.Dispose();
|
||||
m_UnityWebRequest = null;
|
||||
}
|
||||
}
|
||||
|
||||
m_Disposed = true;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (m_UnityWebRequest == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!m_UnityWebRequest.isDone)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
bool isError = false;
|
||||
#if UNITY_2020_2_OR_NEWER
|
||||
isError = m_UnityWebRequest.result != UnityWebRequest.Result.Success;
|
||||
#elif UNITY_2017_1_OR_NEWER
|
||||
isError = m_UnityWebRequest.isNetworkError || m_UnityWebRequest.isHttpError;
|
||||
#else
|
||||
isError = m_UnityWebRequest.isError;
|
||||
#endif
|
||||
if (isError)
|
||||
{
|
||||
DownloadAgentHelperErrorEventArgs downloadAgentHelperErrorEventArgs = DownloadAgentHelperErrorEventArgs.Create(m_UnityWebRequest.responseCode == RangeNotSatisfiableErrorCode, m_UnityWebRequest.error);
|
||||
m_DownloadAgentHelperErrorEventHandler(this, downloadAgentHelperErrorEventArgs);
|
||||
ReferencePool.Release(downloadAgentHelperErrorEventArgs);
|
||||
}
|
||||
else
|
||||
{
|
||||
DownloadAgentHelperCompleteEventArgs downloadAgentHelperCompleteEventArgs = DownloadAgentHelperCompleteEventArgs.Create((long)m_UnityWebRequest.downloadedBytes);
|
||||
m_DownloadAgentHelperCompleteEventHandler(this, downloadAgentHelperCompleteEventArgs);
|
||||
ReferencePool.Release(downloadAgentHelperCompleteEventArgs);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a19b280dcc736764fb78ce804a6c4d88
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,246 @@
|
||||
//------------------------------------------------------------
|
||||
// Game Framework
|
||||
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
|
||||
// Homepage: https://gameframework.cn/
|
||||
// Feedback: mailto:ellan@gameframework.cn
|
||||
//------------------------------------------------------------
|
||||
|
||||
#if !UNITY_2018_3_OR_NEWER
|
||||
|
||||
using GameFramework;
|
||||
using GameFramework.Download;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UnityGameFramework.Runtime
|
||||
{
|
||||
/// <summary>
|
||||
/// WWW 下载代理辅助器。
|
||||
/// </summary>
|
||||
public class WWWDownloadAgentHelper : DownloadAgentHelperBase, IDisposable
|
||||
{
|
||||
private WWW m_WWW = null;
|
||||
private int m_LastDownloadedSize = 0;
|
||||
private bool m_Disposed = false;
|
||||
|
||||
private EventHandler<DownloadAgentHelperUpdateBytesEventArgs> m_DownloadAgentHelperUpdateBytesEventHandler = null;
|
||||
private EventHandler<DownloadAgentHelperUpdateLengthEventArgs> m_DownloadAgentHelperUpdateLengthEventHandler = null;
|
||||
private EventHandler<DownloadAgentHelperCompleteEventArgs> m_DownloadAgentHelperCompleteEventHandler = null;
|
||||
private EventHandler<DownloadAgentHelperErrorEventArgs> m_DownloadAgentHelperErrorEventHandler = null;
|
||||
|
||||
/// <summary>
|
||||
/// 下载代理辅助器更新数据流事件。
|
||||
/// </summary>
|
||||
public override event EventHandler<DownloadAgentHelperUpdateBytesEventArgs> DownloadAgentHelperUpdateBytes
|
||||
{
|
||||
add
|
||||
{
|
||||
m_DownloadAgentHelperUpdateBytesEventHandler += value;
|
||||
}
|
||||
remove
|
||||
{
|
||||
m_DownloadAgentHelperUpdateBytesEventHandler -= value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 下载代理辅助器更新数据大小事件。
|
||||
/// </summary>
|
||||
public override event EventHandler<DownloadAgentHelperUpdateLengthEventArgs> DownloadAgentHelperUpdateLength
|
||||
{
|
||||
add
|
||||
{
|
||||
m_DownloadAgentHelperUpdateLengthEventHandler += value;
|
||||
}
|
||||
remove
|
||||
{
|
||||
m_DownloadAgentHelperUpdateLengthEventHandler -= value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 下载代理辅助器完成事件。
|
||||
/// </summary>
|
||||
public override event EventHandler<DownloadAgentHelperCompleteEventArgs> DownloadAgentHelperComplete
|
||||
{
|
||||
add
|
||||
{
|
||||
m_DownloadAgentHelperCompleteEventHandler += value;
|
||||
}
|
||||
remove
|
||||
{
|
||||
m_DownloadAgentHelperCompleteEventHandler -= value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 下载代理辅助器错误事件。
|
||||
/// </summary>
|
||||
public override event EventHandler<DownloadAgentHelperErrorEventArgs> DownloadAgentHelperError
|
||||
{
|
||||
add
|
||||
{
|
||||
m_DownloadAgentHelperErrorEventHandler += value;
|
||||
}
|
||||
remove
|
||||
{
|
||||
m_DownloadAgentHelperErrorEventHandler -= value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过下载代理辅助器下载指定地址的数据。
|
||||
/// </summary>
|
||||
/// <param name="downloadUri">下载地址。</param>
|
||||
/// <param name="userData">用户自定义数据。</param>
|
||||
public override void Download(string downloadUri, object userData)
|
||||
{
|
||||
if (m_DownloadAgentHelperUpdateBytesEventHandler == null || m_DownloadAgentHelperUpdateLengthEventHandler == null || m_DownloadAgentHelperCompleteEventHandler == null || m_DownloadAgentHelperErrorEventHandler == null)
|
||||
{
|
||||
Log.Fatal("Download agent helper handler is invalid.");
|
||||
return;
|
||||
}
|
||||
|
||||
m_WWW = new WWW(downloadUri);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过下载代理辅助器下载指定地址的数据。
|
||||
/// </summary>
|
||||
/// <param name="downloadUri">下载地址。</param>
|
||||
/// <param name="fromPosition">下载数据起始位置。</param>
|
||||
/// <param name="userData">用户自定义数据。</param>
|
||||
public override void Download(string downloadUri, long fromPosition, object userData)
|
||||
{
|
||||
if (m_DownloadAgentHelperUpdateBytesEventHandler == null || m_DownloadAgentHelperUpdateLengthEventHandler == null || m_DownloadAgentHelperCompleteEventHandler == null || m_DownloadAgentHelperErrorEventHandler == null)
|
||||
{
|
||||
Log.Fatal("Download agent helper handler is invalid.");
|
||||
return;
|
||||
}
|
||||
|
||||
Dictionary<string, string> header = new Dictionary<string, string>
|
||||
{
|
||||
{ "Range", Utility.Text.Format("bytes={0}-", fromPosition) }
|
||||
};
|
||||
|
||||
m_WWW = new WWW(downloadUri, null, header);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过下载代理辅助器下载指定地址的数据。
|
||||
/// </summary>
|
||||
/// <param name="downloadUri">下载地址。</param>
|
||||
/// <param name="fromPosition">下载数据起始位置。</param>
|
||||
/// <param name="toPosition">下载数据结束位置。</param>
|
||||
/// <param name="userData">用户自定义数据。</param>
|
||||
public override void Download(string downloadUri, long fromPosition, long toPosition, object userData)
|
||||
{
|
||||
if (m_DownloadAgentHelperUpdateBytesEventHandler == null || m_DownloadAgentHelperUpdateLengthEventHandler == null || m_DownloadAgentHelperCompleteEventHandler == null || m_DownloadAgentHelperErrorEventHandler == null)
|
||||
{
|
||||
Log.Fatal("Download agent helper handler is invalid.");
|
||||
return;
|
||||
}
|
||||
|
||||
Dictionary<string, string> header = new Dictionary<string, string>
|
||||
{
|
||||
{ "Range", Utility.Text.Format("bytes={0}-{1}", fromPosition, toPosition) }
|
||||
};
|
||||
|
||||
m_WWW = new WWW(downloadUri, null, header);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重置下载代理辅助器。
|
||||
/// </summary>
|
||||
public override void Reset()
|
||||
{
|
||||
if (m_WWW != null)
|
||||
{
|
||||
m_WWW.Dispose();
|
||||
m_WWW = null;
|
||||
}
|
||||
|
||||
m_LastDownloadedSize = 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放资源。
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放资源。
|
||||
/// </summary>
|
||||
/// <param name="disposing">释放资源标记。</param>
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (m_Disposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (disposing)
|
||||
{
|
||||
if (m_WWW != null)
|
||||
{
|
||||
m_WWW.Dispose();
|
||||
m_WWW = null;
|
||||
}
|
||||
}
|
||||
|
||||
m_Disposed = true;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (m_WWW == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int deltaLength = m_WWW.bytesDownloaded - m_LastDownloadedSize;
|
||||
if (deltaLength > 0)
|
||||
{
|
||||
m_LastDownloadedSize = m_WWW.bytesDownloaded;
|
||||
DownloadAgentHelperUpdateLengthEventArgs downloadAgentHelperUpdateLengthEventArgs = DownloadAgentHelperUpdateLengthEventArgs.Create(deltaLength);
|
||||
m_DownloadAgentHelperUpdateLengthEventHandler(this, downloadAgentHelperUpdateLengthEventArgs);
|
||||
ReferencePool.Release(downloadAgentHelperUpdateLengthEventArgs);
|
||||
}
|
||||
|
||||
if (m_WWW == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!m_WWW.isDone)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(m_WWW.error))
|
||||
{
|
||||
DownloadAgentHelperErrorEventArgs dodwnloadAgentHelperErrorEventArgs = DownloadAgentHelperErrorEventArgs.Create(m_WWW.error.StartsWith(RangeNotSatisfiableErrorCode.ToString(), StringComparison.Ordinal), m_WWW.error);
|
||||
m_DownloadAgentHelperErrorEventHandler(this, dodwnloadAgentHelperErrorEventArgs);
|
||||
ReferencePool.Release(dodwnloadAgentHelperErrorEventArgs);
|
||||
}
|
||||
else
|
||||
{
|
||||
byte[] bytes = m_WWW.bytes;
|
||||
DownloadAgentHelperUpdateBytesEventArgs downloadAgentHelperUpdateBytesEventArgs = DownloadAgentHelperUpdateBytesEventArgs.Create(bytes, 0, bytes.Length);
|
||||
m_DownloadAgentHelperUpdateBytesEventHandler(this, downloadAgentHelperUpdateBytesEventArgs);
|
||||
ReferencePool.Release(downloadAgentHelperUpdateBytesEventArgs);
|
||||
|
||||
DownloadAgentHelperCompleteEventArgs downloadAgentHelperCompleteEventArgs = DownloadAgentHelperCompleteEventArgs.Create(bytes.LongLength);
|
||||
m_DownloadAgentHelperCompleteEventHandler(this, downloadAgentHelperCompleteEventArgs);
|
||||
ReferencePool.Release(downloadAgentHelperCompleteEventArgs);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 30217a4100363014a9c8c671b5848a61
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user