Loading of the asynchronous loading scenario in unity

Source: Internet
Author: User

When we load a scene, if the resources of the scene is relatively small, it will quickly jump into the next scene, if the scene resources more load will be very slow, then if the user has been waiting for the user experience is poor, so we use asynchronous loading.

Here I use Ugui do, of course, with Ngui can also, just change the components.

Using Unityengine;
Using System.Collections;
Using Unityengine.ui;
public class Excessivemenupanel:monobehaviour {
Public Scrollbar progress bar in loadscrollbar;//scene
Public text loadtext;//Display progress
Use this for initialization
void Start () {
This is the ID of the buildsetting, or the string, change the following int to string
Startcoroutine (Startloading_4 (2));


}


<summary>
Load the scene, only load the scene in Buildsettings settings, I do load assetbundle loading scene is not possible
</summary>
<param name= "Scene" ></param>
<returns></returns>
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;
}
<summary>
Set up progress bars and text
</summary>
<param name= "num" ></param>
private void Setloadingpercentage (float num)
{
loadscrollbar.size = num * 0.01F;
Loadtext.text = num. ToString () + "%";
}
}

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.