Question by
LpJack · Oct 23, 2021 at 07:27 PM ·
coroutinecoroutinesienumeratorprogress-barprogress
How to get the Progress from a IEnumerator
Im have a Loading Screen for my game with a loading Bar and it all works fine. But the progress of the bar is just from the AsyncOperation LoadSceneAsync. But I want, when I'm loading the scene, also get the progress of some more things, like creating the environment(I'm spawning trees random on the map). So I'm wondering if it was possible not just to write into the coroutine: yield return null; float progressBar=10; but instead updating the progressBar every Frame in Update-Function. Thanks for any help!!(Sorry for my bad english)
void Start()
{
StartCoroutine(LoadingProcess(2));
}
IEnumerator LoadingProcess(float duration)
{
loadingOperation = SceneManager.LoadSceneAsync(LoadingData.sceneToLoad);//Loading Data is an extra static Script with the static string sceneToLoad
float startValue = canvasGroup.alpha;
float time = 0;
while (time < duration)
{
canvasGroup.alpha = Mathf.Lerp(startValue, 1, time / duration);
time += Time.deltaTime;
yield return null;
}
canvasGroup.alpha = 1;
yield return progressOperation;
}
public void Update()
{
progressBar.value = Mathf.Clamp01(loadingOperation.progress / 0.9f);
}
This is what I use for the Loading Screen. And this is what I use for the tree spawning:
public void spawnTrees()
{
for (int i = 0; i < 99; i++)
{
GameObject tree = Instantiate(singleTree, treesTransform, false);
tree.transform.parent = treesTransform;
}
int treeNumber = treesTransform.childCount;
System.Random rand = new System.Random();
for(int index = 0; index < treeNumber; index++)
{
Transform treeChild = trees.transform.GetChild(index);
GameObject treeChildObject = treeChild.gameObject;
treeChild.position = new Vector3(rand.Next(0, 1001), 61, -rand.Next(0, 1001));
if(treeChild.position.z < -420 & treeChild.position.z > -562)
{
index--;
}
}
}
And I want to implement this into the progress of the loading bar of the loading Screen
Comment