Problems encountered when creating the Loading progress bar in Unity3d

Source: Internet
Author: User

Background

Generally, the main scenario of a game contains a large amount of resources, which leads to a long loading time. To avoid this problem, you can first load the Loading scenario and then load the main scenario through the Loading scenario. Because the Loading scenario contains fewer resources, the Loading speed is fast. When Loading the main scenario, a progress bar is usually displayed on the Loading interface to inform the player of the current Loading progress. In Unity, you can callApplication.LoadLevelAsyncFunction to asynchronously load game scenarios and queryAsyncOperation.progressTo get the loading progress of the scenario.

Try -- encountered a problem

The first step is to call the followingLoadGameThe function starts to load the game scenario. The asynchronous Loading method is used to load scenario 1 (the Loading scenario is 0, and the home scenario is 1). Through the Coroutine mechanism provided by Unity, we can conveniently callSetLoadingPercentageFunction to update the value of the progress bar displayed on the interface.

public void LoadGame() {    StartCoroutine(StartLoading_1(1));}private IEnumerator StartLoading_1(int scene) {    AsyncOperation op = Application.LoadLevelAsync(scene);    while(!op.isDone) {                    SetLoadingPercentage(op.progress * 100);        yield return new WaitForEndOfFrame();    }        }

The progress bar is displayed as follows:

The progress bar does not display the loading progress continuously. Instead, it switches to a number at a pause, switches to a number at a pause, and switches to the main scenario if no 100% is displayed. The reason is thatApplication.LoadLevelAsyncIt is not a real background loading. It loads some game resources at each frame and gives a progress value. Therefore, the game will be stuck during loading,AsyncOperation.progressIs not accurate enough. After the main scenario is loaded, Unity automatically switches to the scenario. Therefore, the code in the while loop in the above Code will not be called, and the progress bar will not display 100%.

Patch-100% completed

To enable the progress bar to display 100%, you can useAsyncOperation.progressThe value of is multiplied by 2, so that 50% is displayed on the page when it is loaded to 100%. The disadvantage is that when 100% is displayed on the interface, the user still has to wait for a while before entering the game. In fact, Unity provides a way to manually switch the scenario.AsyncOperation.allowSceneActivationSetfalseYou can disable automatic switch after Unity is loaded.StartLoading_2The Code is as follows:

// this function is not workprivate IEnumerator StartLoading_2(int scene) {    AsyncOperation op = Application.LoadLevelAsync(scene);    op.allowSceneActivation = false;    while(!op.isDone) {        SetLoadingPercentage(op.progress * 100);        yield return new WaitForEndOfFrame();    }    op.allowSceneActivation = true;   }

FirstAsyncOperation.allowSceneActivationSetfalse, And then settrue. The Code does not seem to be wrong, but the execution result is that the progress bar will last on 90%, and the scenario will not be switched. Discover by printing logsAsyncOperation.isDoneAlwaysfalse,AsyncOperation.progressAfter the value is increased to 0.9, it will remain unchanged, that is, the scenario will never be loaded.

I found the answer in this post.allowSceneActivationSetfalseThen, Unity will only load the scenario to 90%, and the remaining 10% will waitallowSceneActivationSettrueThis is a pitfall. So the code is changed as follows. WhenAsyncOperation.progressAfter reaching 0.9, update the value of the progress bar to 100% and setAsyncOperation.allowSceneActivationIstureTo allow Unity to continue loading unfinished scenarios.

private IEnumerator StartLoading_3(int scene) {    AsyncOperation op = Application.LoadLevelAsync(scene);    op.allowSceneActivation = false;    while(op.progress < 0.9f) {        SetLoadingPercentage(op.progress * 100);        yield return new WaitForEndOfFrame();    }    SetLoadingPercentage(100);    yield return new WaitForEndOfFrame();    op.allowSceneActivation = true;   }

The final effect is as follows:

Polishing-add an animation

Although the above progress bar solves the problem of 100% display, it does not look natural and beautiful because the numerical update of the progress bar is not continuous. To seem like loading continuously, you can insert a transition value every time you update the progress bar. Here, my strategy is to obtainAsyncOperation.progressAfter the value of, do not update the value of the progress bar immediately, but add 1 to the original value of each frame, which will produce the animation effect of the number rolling continuously, this method is used to display the download progress in thunder.

private IEnumerator StartLoading_4(int scene) {    int displayProgress = 0;    int toProgress = 0;    AsyncOperation op = Application.LoadLevelAsync(scene);    op.allowSceneActivation = false;    while(op.progress < 0.9f) {        toProgress = (int)op.progress * 100;        while(displayProgress < toProgress) {            ++displayProgress;            SetLoadingPercentage(displayProgress);            yield return new WaitForEndOfFrame();        }    }    toProgress = 100;    while(displayProgress < toProgress){        ++displayProgress;        SetLoadingPercentage(displayProgress);        yield return new WaitForEndOfFrame();    }    op.allowSceneActivation = true;}

displayProgressUsed to record the value to be displayed on the progress bar. The animation of the progress bar is as follows:

Compare the progress bar of the first type

Summary

If you still need to parse data tables, generate object pools, and perform network connections before loading the game's main scenario, you can assign these operations a weight, you can use these weights to calculate the loading progress. If your scenario loading speed is very fast, you can use a fake progress bar to let players watch the loading animation for several seconds and then load the scenario. In short, although the progress bar is small, it is not easy to do well.

Reference
  1. A high. Unity displays the Loading progress to correct popular online methods
  2. Unity3d official forum. using allowSceneActivation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.