You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.3 KiB
C#

using System.Collections;
using UnityEngine;
public interface TWS_IGameDataLoadingTask
{
string TaskName{get;}
float TaskProgress{get;}
void OnTaskStarted();
void OnTaskFinished();
}
public abstract class TWS_GameDataLoadingTask : MonoBehaviour, TWS_IGameDataLoadingTask
{
[SerializeField]
protected string taskName;
protected float progressValue;
[SerializeField]
protected float lerpDuration = 10f;
protected float elapsedTime = 0f;
public string TaskName => taskName;
public float TaskProgress => progressValue;
public abstract void OnTaskFinished();
public abstract void OnTaskStarted();
protected IEnumerator FetchData()
{
WaitForEndOfFrame waitForEndOfFrame = new ();
while (elapsedTime <= lerpDuration)
{
progressValue = FetchDataFromRemote();
yield return waitForEndOfFrame;
}
}
protected float FetchDataFromRemote()
{
// Increase the elapsed time
elapsedTime += Time.deltaTime;
// Calculate the interpolation factor between 0 and 1 based on elapsed time and lerpDuration
float t = Mathf.Clamp01(elapsedTime / lerpDuration);
// Lerp a value from 0 to 1 over time
float lerpedValue = Mathf.Lerp(0f, 1f, t);
return lerpedValue;
}
}