As you can see in my code, I have attempted to create my own Loading Bar using an image’s fillAmount value of Horizontal left to right.
So far, I am getting an error for:
LoadingBar.fillAmount = async.progress.ToString ();
“Cannot implicity convert a ‘string’ to ‘float’”
Also this kind of code that I have created, will it even work? I am new to all this Loading bar stuff.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ExampleClass : MonoBehaviour {
public Image LoadingBar;
private AsyncOperation async;
IEnumerator Start() {
async = Application.LoadLevelAsync("L2Easy");
//async.progress = LoadingBar.fillAmount;
yield return async;
}
void Update()
{
LoadingBar.fillAmount = async.progress.ToString ();
if (LoadingBar.fillAmount >= 1)
{
StartCoroutine(LoadLevel());
}
}
IEnumerator LoadLevel()
{
yield return async;
}
}
Funny how I just wrote this question and now I am answering it.
IF YOU ARE LOOKING FOR A WORKING LOAD BAR
‘Load Level with out a button pressed’
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class LoadLevel : MonoBehaviour {
public Image LoadingBar;
private AsyncOperation async;
IEnumerator Start()
{
yield return new WaitForSeconds (0.1f);
async = Application.LoadLevelAsync ("L2Easy");
}
void Update()
{
LoadingBar.fillAmount = async.progress;
}
}
‘Load Level with a button pressed’
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class LoadLevel : MonoBehaviour {
public Image LoadingBar;
private AsyncOperation async;
IEnumerator Start()
{
yield return new WaitForSeconds (0.1f);
async = Application.LoadLevelAsync ("L2Easy");
async.allowSceneActivation = false;
}
void Update()
{
LoadingBar.fillAmount = async.progress;
if (LoadingBar.fillAmount == 0.9f)
{
if(Input.GetKeyDown(KeyCode.Space))
{
async.allowSceneActivation = true;
}
}
}
}
- Insert this script on to any game object so when your level finishes, it activates the gameobject that holds this script.
- Assign your UI Image and change the Image Type to Filled and set the Fill Amount to 0.
- You are done! Now when this gameobject activates, it begins the loading process.
You need to add if(async!=null) because the first update cycle occurs before the “WaitForSeconds (0.1f);”
void Update () {
if(async!=null)LoadingBar.fillAmount=async.progress;
}