How to preloading on Unity. I’ll show the Scripts waiting few seconds after loading.

Unity

Preloading is one of the best ways which reduce user’s stress easily.

So, I created the loading.cs for preloading.

If you don’t need animation system, please get rid of the part,

” Update(){——-} “

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Loading : MonoBehaviour
{

  
  private AsyncOperation async;
  public Text loadingText;
  public float waitSec = 5.0f;
  public float cycle=0.8f;
  float time=0;
  void Start()
  {
    StartCoroutine("LoadData");
  }

    private void Update()
    {
    time += Time.deltaTime;

        if(time <= (cycle/5f))
      loadingText.text = "Now loading";
    else if (time <= (cycle * 2 / 5f))
      loadingText.text = "Now loading.";
    else if (time <= (cycle * 3 / 5f))
      loadingText.text = "Now loading..";
    else if (time <= (cycle * 4 / 5f))
      loadingText.text = "Now loading...";
    else if (time <= cycle)
        {
      time = 0;
        }
    

  }
    IEnumerator LoadData()
  {
    // シーンの読み込みを開始
    async = SceneManager.LoadSceneAsync("MainScene");
    // ロードが完了していても,シーンのアクティブ化は許可しない
    async.allowSceneActivation = false;
    
    while (async.progress<0.9f)
    {
      yield return null;
    }
    //ロード完了後。任意秒 待ってシーン遷移。
    yield return new WaitForSeconds(waitSec);
    async.allowSceneActivation = true;
  }

}

BTW, be careful about “AsyncOperation”

On my script, it couldn’t work when written while (!asny.isDone)  .

should use while (async.progress<0.9f)

Because when you define async.allowSceneActivation = false; at first,

“async.isDone” always return false

and

“async.progress” couldn’t reach 1.0, but 0.9

the max state will be 0.9.

Thank you for reading. Best regards.

I write this contents to improve my English skill. Don’t mind some spelling mistake.

コメント

タイトルとURLをコピーしました