This is to show how to create a simple loading progress using unity AsyncOperation.
Step 1; Create a UI image and name it as Progressbar and set the pivot point of the image to left center and width = 50 , height = 20.
Step 2: Create a new C# script and name it as LoadingBar.
Step 3 ; Copy and paste the below Code.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
using System.Collections.Generic;
public class LoadingBar : MonoBehaviour
{
public Image loader;
RectTransform objectRectTransform;
AsyncOperation async;
float tempval;
IEnumerator Start()
{
objectRectTransform = loader.GetComponent<RectTransform>();
tempval = objectRectTransform.rect.width;
Debug.Log(tempval);
yield return new WaitForSeconds(0.1f);
async = Application.LoadLevelAsync("Scene1");//add the next scene name that to be loaded
yield return async;
}
void Update()
{
try
{
Debug.Log(async.progress);
tempval += async.progress;
loader.rectTransform.sizeDelta = new Vector2(tempval, 20f);
}
catch (NullReferenceException ex)
{
Debug.Log("Async not loaded!");
}
}
}
Step 4: Create a Empty Game object and drag the LoadingBar script on to it.
Step 5: In inspector add the ProgressBar image to variable loader.
Step 6 : Replace “Scene1” with next scene name that to be loaded in line async = Application.LoadLevelAsync(“Scene1”); on the above code
Step 7; Verify that you have added the current scene and next scene in Build settings.
Step 8: That’s it. You can now play the scene