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
- if (Nextsceneid = = (int) GlobeEnum.SceneDefine.LOGIN)
- Yield return Startcoroutine (Loadnormalscene (scenetable.resname));
- Else
- 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
- IEnumerator loadnormalscene (string scenename, float startpercent = 0)
- {
- GameRoot.Instance.CurrentSceneId = (int) Loadingwindow.nextsceneid;
- Loadingtext.text = "load scene ...";
- int startprogress = (int) (startpercent * 100);
- int displayprogress = startprogress;
- int toprogress = startprogress;
- AsyncOperation op = application.loadlevelasync (Scenename);
- Op.allowsceneactivation = false;
- While (Op.progress < 0.9f)
- {
- Toprogress = startprogress + (int) (op.progress * (1.0f-startpercent) * 100);
- While (Displayprogress < toprogress)
- {
- ++displayprogress;
- Setprogress (displayprogress);
- Yield return null;
- }
- Yield return null;
- }
- toprogress = 100;
- While (Displayprogress < toprogress)
- {
- ++displayprogress;
- Setprogress (displayprogress);
- Yield return null;
- }
- Op.allowsceneactivation = true;
- }
- IEnumerator loadbundlescene (string scenename)
- {
- string path = Bundlemanager.getbundleloadpath (Bundlemanager.pathscenedata, Scenename + ". Data");
- www www = new www (path);
- Loadingtext.text = "Load resource bundle ...";
- int displayprogress = 0;
- int toprogress = 0;
- While (!www.isdone)
- {
- Toprogress = (int) (www.progress * m_bundlepercent * 100);
- While (Displayprogress < toprogress)
- {
- ++displayprogress;
- Setprogress (displayprogress);
- Yield return null;
- }
- Yield return null;
- }
- Toprogress = (int) (m_bundlepercent * 100);
- While (Displayprogress < toprogress)
- {
- ++displayprogress;
- Setprogress (displayprogress);
- Yield return null;
- }
- Yield return www;
- if (null! = Www.assetBundle)
- {
- M_lastscenebundle = Www.assetBundle;
- Yield return Startcoroutine (Loadnormalscene (Scenename, m_bundlepercent));
- }
- }
- void setprogress (int progress)
- {
- Loadingbar.value = progress * 0.01F;
- Loadingprogress.text = progress. ToString () + "%";
- }
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