Unity3d Hand Tour Development Diary (3)-The perfect solution for the scene loading progress bar

Source: Internet
Author: User

I thought making a progress bar is very simple, divided into minutes to solve, the result of a day to fix, unity has a lot of pits, to do the perfect need to solve each.

Issue 1: The simplest method does not achieve 100% progress

The simplest way to do this is not to achieve 100% progress because unity loads the new scene and activates the new scene immediately, unable to show the final progress. The solution is to use allowsceneactivation to control the timing of entering the scene.

Issue 2: Progress card after using allowsceneactivation in 90%

This issue is also discussed in the official website forum, the solution is to manually repair the last 10%,

Question 3: The progress bar increases in a single meal. No smoothing

Workaround manual Interpolation Smoothing

Problem 4:www and loadlevelasync the integration of two parts of progress

Most of the scene is a bundle package, first to the WWW load, then loadlevelasync, two parts of the progress to be integrated together,

A small number of scenarios are not bundles, such as login scenarios,

[CSharp]View PlainCopy
    1. if (Nextsceneid = = (int) GlobeEnum.SceneDefine.LOGIN)
    2. Yield return Startcoroutine (Loadnormalscene (scenetable.resname));
    3. Else
    4. Yield return Startcoroutine (Loadbundlescene (scenetable.resname));

Issue 5: Use yield return null instead of update () to process the progress interface update for each frame.

Use yield return null to handle updates, such as in the update function, the code is more concise, but note that one problem is the while dead loop problem

Each while place must correspond to a yield return null,

After the above processing, the progress bar seems to be a lot of perfection, finally can satisfy my perfectionist's request. (o_o)

The code is as follows:

[CSharp]View PlainCopy
  1. IEnumerator loadnormalscene (string scenename, float startpercent = 0)
  2. {
  3. GameRoot.Instance.CurrentSceneId = (int) Loadingwindow.nextsceneid;
  4. Loadingtext.text = "load scene ...";
  5. int startprogress = (int) (startpercent * 100);
  6. int displayprogress = startprogress;
  7. int toprogress = startprogress;
  8. AsyncOperation op = application.loadlevelasync (Scenename);
  9. Op.allowsceneactivation = false;
  10. While (Op.progress < 0.9f)
  11. {
  12. Toprogress = startprogress + (int) (op.progress * (1.0f-startpercent) * 100);
  13. While (Displayprogress < toprogress)
  14. {
  15. ++displayprogress;
  16. Setprogress (displayprogress);
  17. Yield return null;
  18. }
  19. Yield return null;
  20. }
  21. toprogress = 100;
  22. While (Displayprogress < toprogress)
  23. {
  24. ++displayprogress;
  25. Setprogress (displayprogress);
  26. Yield return null;
  27. }
  28. Op.allowsceneactivation = true;
  29. }
  30. IEnumerator loadbundlescene (string scenename)
  31. {
  32. string path = Bundlemanager.getbundleloadpath (Bundlemanager.pathscenedata, Scenename + ". Data");
  33. www www = new www (path);
  34. Loadingtext.text = "Load resource bundle ...";
  35. int displayprogress = 0;
  36. int toprogress = 0;
  37. While (!www.isdone)
  38. {
  39. Toprogress = (int) (www.progress * m_bundlepercent * 100);
  40. While (Displayprogress < toprogress)
  41. {
  42. ++displayprogress;
  43. Setprogress (displayprogress);
  44. Yield return null;
  45. }
  46. Yield return null;
  47. }
  48. Toprogress = (int) (m_bundlepercent * 100);
  49. While (Displayprogress < toprogress)
  50. {
  51. ++displayprogress;
  52. Setprogress (displayprogress);
  53. Yield return null;
  54. }
  55. Yield return www;
  56. if (null! = Www.assetBundle)
  57. {
  58. M_lastscenebundle = Www.assetBundle;
  59. Yield return Startcoroutine (Loadnormalscene (Scenename, m_bundlepercent));
  60. }
  61. }
  62. void setprogress (int progress)
  63. {
  64. Loadingbar.value = progress * 0.01F;
  65. Loadingprogress.text = progress.  ToString () + "%";
  66. }


Loadnoramlscene to load a scenario that is not packaged

Loadbundlescene to load packaged scenes

M_bundlepercent indicates the percentage of total progress that the bundle bundle is loading, default 0.7f

Unity3d Hand Tour Development Diary (3)-The perfect solution for the scene loading progress bar

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.