NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/Net/Client/AppConfigDownload.cs

58 lines
1.4 KiB
C#
Raw Normal View History

2024-03-14 15:56:56 +08:00
using UnityEngine;
using UnityEngine.Networking;
2024-09-10 16:38:03 +08:00
using PhxhSDK.AOT.PreConfig;
using Cysharp.Threading.Tasks;
2024-03-14 15:56:56 +08:00
namespace Gameplay.Net
{
2024-09-10 16:38:03 +08:00
public interface ACDownloadHanlder
2024-03-14 15:56:56 +08:00
{
2024-09-10 16:38:03 +08:00
string URL { get; }
void OnRequestFinished(UnityWebRequest request);
2024-03-14 15:56:56 +08:00
}
2024-09-10 16:38:03 +08:00
public class AppConfigDownload : MonoBehaviour
2024-03-14 15:56:56 +08:00
{
2024-09-10 16:38:03 +08:00
enum LocalStatus
{
None = 0,
Start,
Wait,
Done,
}
2024-03-14 15:56:56 +08:00
2024-09-10 16:38:03 +08:00
ACDownloadHanlder m_handler;
LocalStatus Status;
public void Init(ACDownloadHanlder handler)
2024-03-14 15:56:56 +08:00
{
2024-09-10 16:38:03 +08:00
m_handler = handler;
Status = LocalStatus.Start;
2024-03-14 15:56:56 +08:00
}
2024-09-10 16:38:03 +08:00
void Update()
2024-03-14 15:56:56 +08:00
{
2024-09-10 16:38:03 +08:00
if (Status == LocalStatus.Start)
{
GetRequest().Forget();
Status = LocalStatus.Wait;
}
else if (Status == LocalStatus.Done)
{
Destroy(gameObject);
}
2024-03-14 15:56:56 +08:00
}
2024-09-10 16:38:03 +08:00
async UniTask GetRequest()
2024-03-14 15:56:56 +08:00
{
2024-09-10 16:38:03 +08:00
Debug.Log("Start GetRequest:" + m_handler.URL);
using UnityWebRequest webRequest = UnityWebRequest.Get(await PreConfig.instance.GetAppConfigUrl());
2024-03-14 15:56:56 +08:00
// Request and wait for the desired page.
2024-09-10 16:38:03 +08:00
await webRequest.SendWebRequest();
2024-03-14 15:56:56 +08:00
m_handler.OnRequestFinished(webRequest);
Status = LocalStatus.Done;
}
}
}