[Unity3D] Unity3D game development: Recording scenarios asynchronously and reading progress bars

Source: Internet
Author: User

Hello everyone, I'm Qin Yuanpei. Welcome to follow my blog. My blog address is blog.csdn.net/qinyuanpei. Finally, I am relieved of all kinds of silent essay assignments, so I am eager to update my blog here. The blogger originally planned to develop the Unity3D game starting from the Battle arms game (I) -- after the goal tracking article, I will write another article titled Unity3D game development starting from the Battle arms game (lower). However, the progress of the blogger's project is a little slow at present, so I want to share it with you after the project is stable. As everyone waits for the bloggers to update their blogs, let's talk about the asynchronous loading of game scenarios in Unity3D today. So what is asynchronous loading in game scenarios? Asynchronous loading means that the system can execute other tasks when loading data (such as map data, model data, and player data). It is actually a familiar multithreading. On the contrary, in a single thread, we usually need to wait until the data loading is complete before continuing to execute the task. We can find such examples in games such as legend of the legend. For example, the courage to read the article in legend 4:


For another example, "Gu jianqitan" incorporates the reading effect of the game features:


From the above examples, we can see that in the game design, a reasonable loading mode is used for game scenarios to reduce the waiting time for players, for a game, it is a question that needs to be considered during design. In the Web design, a good Web design usually needs to minimize the user's waiting time for page response, so as not to lead to the lack of user traffic. In an era that focuses on user experience, no matter what kind of product is designed, it is most important to bring the most extreme user experience. We noticed that both games adopt asynchronous loading, because the system is executing the drawing of GUI elements during game loading. Well, after learning about the concept of asynchronous loading and the principle of asynchronous loading, we will implement today's content, we hope that different prompts can be displayed at the bottom of the progress bar during game loading:



How can this problem be implemented? In Unity3D, A LoadLevelAsync () method is provided. This method can be used to asynchronously load scenario resources. This method returns an AsyncOperation type returned value. The returned value has two important attributes: 1. isDone: Can Be Used to query whether loading is complete; 2. progress: A value between 0 and 1, indicates the loading progress.

Now, with these two attributes and the method, we can implement today's content. Let's get started!

First, create a scenario. Here we use the default GUI system,




This interface is our loading interface today. During the loading process, we can display the Tips and game progress in the game under the background. Generally, we need three interfaces to complete a complete interface loading effect. The first interface is called the trigger interface, which is mainly responsible for responding to user trigger operations. The second is the loading interface we are talking about today. It is mainly responsible for reading and refreshing the UI. The third interface is the interface that we finally want to present to players. For example, in the legend of the legend, when a player enters this range, it will trigger the display of the loading interface, so that we can see the reading results of the Five Poisonous animals. When the reading ends, the third interface is displayed, which is the final interface for players .. Well, this is a complete process of Asynchronously loading game scenarios. Let's write a script to implement today's functions. For the loading process interface, we mainly use our LoadLevelAsync () method:

Using UnityEngine; using System. collections; public class Loading: MonoBehaviour {// asynchronous object private AsyncOperation mAsyn; // GUITextprivate GUIText mTip bound to the Tip; // a collection of Tip, in actual development, you need to read private string [] mTips = new string [] {"you can browse game strategy during asynchronous loading" from external files ", "You can view the current progress during asynchronous loading", "you can determine whether the loading is complete during asynchronous loading", "the blogger does not understand why the reading speed of Xuan 6 is so slow ", "Isn't it because DOOM doesn't know Unity3D",}; // update the Tip time private const float UpdateTime = 2.0F; // The Last finer time private float lastTime = 0.0 F; void Start () {mTip = GameObject. find ("GUITips "). getComponent <GUIText> (); StartCoroutine ("Load");} IEnumerator Load () {mAsyn = Application. loadLevelAsync ("Main"); yield return mAsyn;} void Update () {// Tip if (mAsyn! = Null &&! MAsyn. isDone) {// if the update Time is reached, if (Time. time-lastTime> = UpdateTime) {lastTime = Time. time; mTip. guiText. text = mTips [Random. range (0, 5)] + "(" + (float) mAsyn. progress * 100 + "%" + ")" ;}} else {Application. loadLevel ("Main ");}}}


The code here is easy to understand. If the load is complete, the final interface will be loaded. Otherwise, the Tips and loading progress will be displayed randomly. In order to save time, the blogger uses the scenario in the previous article,


For the interface that triggers the loading event, we only need to use the normal loading method:

Using UnityEngine; using System. collections; public class TriggerToLoad: MonoBehaviour {void OnGUI () {if (GUILayout. button ("Loading scenario", GUILayout. height (30) {Application. loadLevel ("Scene2 ");}}}
Obviously, this is a method for loading scenarios when a user clicks a button, so there is no more explanation. This method only supports the Pro version of Unity3D, so the free version of the blogger Unity3D is doomed to be unable to run, so it is not possible to give you a program demonstration here, I hope you understand. There are two common problems in the Unity3D asynchronous loading scenario:

1. Use AsyncOperation's progress to get the loading progress without changing the loading progress

2. events that cannot be captured due to scenario loading: DontDestroyOnLoad can be used to solve the problem.

Well, today's content is like this. The capabilities of the bloggers are limited. I can only understand so much. No matter what, I still hope to grow with everyone.

Reference: Unity3D Research Institute: asynchronous Loading of game scenarios and asynchronous Loading of game resource progress bars asynchronous transition of game scenarios using the Loading Interface

Daily Rumor: brave is not a person without tears, but a person who smiles and runs with tears.


If you like my blog, please remember my name: Qin Yuanpei. My blog address is blog.csdn.net/qinyuanpei.
Reprinted please indicate the source, Author: Qin Yuanpei, the source of this article: http://blog.csdn.net/qinyuanpei/article/details/30836567



Related Article

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.