Rework latest version stuff and add splash texts & more
This commit is contained in:
68
Assets/Scripts/PresistentData.cs
Normal file
68
Assets/Scripts/PresistentData.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using System.Threading.Tasks;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
public class PresistentData : MonoBehaviour
|
||||
{
|
||||
public static PresistentData Instance;
|
||||
private bool loadingDone = false;
|
||||
public static string latestVersion = null;
|
||||
public static string[] randomSplashes = null;
|
||||
|
||||
private async void Awake()
|
||||
{
|
||||
if (Instance != null && Instance != this)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
|
||||
Instance = this;
|
||||
DontDestroyOnLoad(gameObject);
|
||||
if (!loadingDone)
|
||||
{
|
||||
loadingDone = true;
|
||||
await GetLatestVersion();
|
||||
await GetRandomSplashes();
|
||||
}
|
||||
}
|
||||
|
||||
private async static Task GetLatestVersion()
|
||||
{
|
||||
using UnityWebRequest request = UnityWebRequest.Get(Endpoints.LATEST_VERSION_ENDPOINT);
|
||||
request.SetRequestHeader("Requester", "BerryDashClient");
|
||||
request.SetRequestHeader("ClientVersion", Application.version);
|
||||
request.SetRequestHeader("ClientPlatform", Application.platform.ToString());
|
||||
await request.SendWebRequest();
|
||||
latestVersion = request.downloadHandler.text ?? "-1";
|
||||
if (
|
||||
SceneManager.GetActiveScene().name == "MainMenu" &&
|
||||
latestVersion != "-1" &&
|
||||
GameObject.Find("Canvas/LatestVersionText").TryGetComponent<TextMeshProUGUI>(out var text)
|
||||
)
|
||||
{
|
||||
text.text = "Latest: v" + latestVersion ?? "N/A";
|
||||
text.transform.GetChild(0).gameObject.SetActive(latestVersion != Application.version);
|
||||
}
|
||||
}
|
||||
|
||||
private async static Task GetRandomSplashes()
|
||||
{
|
||||
using UnityWebRequest request = UnityWebRequest.Get(Endpoints.RANDOM_SPLASHES_ENDPOINT);
|
||||
request.SetRequestHeader("Requester", "BerryDashClient");
|
||||
request.SetRequestHeader("ClientVersion", Application.version);
|
||||
request.SetRequestHeader("ClientPlatform", Application.platform.ToString());
|
||||
await request.SendWebRequest();
|
||||
randomSplashes = request.downloadHandler.text.Split("\n") ?? null;
|
||||
if (
|
||||
SceneManager.GetActiveScene().name == "MainMenu" &&
|
||||
randomSplashes != null &&
|
||||
GameObject.Find("Canvas/SplashText").TryGetComponent<TextMeshProUGUI>(out var text)
|
||||
)
|
||||
{
|
||||
text.text = randomSplashes[Random.Range(0, randomSplashes.Length)];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user