Unity5 Official tutorial Note (2D Rogue like) 01--game Manager

Source: Internet
Author: User

As a new unity novice, I would like to make my own game with unity as soon as possible. In order to urge themselves to learn, but also familiar with the unity of the use of some of the skills, and to find and explore some issues, decided to write a blog to achieve the above purposes, so there will be the following series of several blog posts.

To the author's current skills, the production of 2d games is a more appropriate choice, so they chose the official tutorial 2d rogue like as a project of their own to learn. If you crossing in reading on the way to find out what problems, but also want to point out in time.

Attached Tutorial Address: http://unity3d.com/cn/learn/tutorials/projects/2d-roguelike-tutorial

========== The following is the body =========================================

Every game in the running process, there will be a lot of values to be calculated and recorded, so in order to facilitate management, you can use a "game manager" to manage this information uniformly. And this time I will try to explain what the game manager has done and how it is done in this tutorial.

The first is the Gamemanager code structure (some are slightly different from the official, but the whole is the same)

As you can see, the main features provided by Gamemanager are:

    1. Control the game process, including the start delay of the control level, the delay between each turn, whether the control allows players to operate, etc., to determine whether the game is over or not;
    2. Initialize the game information, including the generation of maps, etc.;
    3. Record some of the game's values, including the player's health, current level, etc.
    4. Hold references to enemies and other parts of the object;
    5. Control the display state of the game, such as whether it should display a map or display auxiliary information such as loading;
    6. Controls the game's UI.

And since the game manager controls all the information that is common in the game, we don't need two game managers at the same time, so the game manager will be a singleton, so it also needs to manage some of the information about the singleton operation itself.

The next step is to explain the specific role of each method.

When the CS script is loaded, the awake () method is executed first, with the following details:

Since the awake () method will be executed before all other methods, this method is generally used to initialize some necessary variables, and so on. In this case, the method first determines whether the Gamemanager's singleton has a value, if not, points to the current object, or if there is a value, destroys the game object (Gameobject) of the previous value; and then uses the Dontdestoryinload () method to mark the current Gamemanager as the next scene to be created without being destroyed, this method preserves the values recorded in the previous gateway, and then allocates memory for the enemies (enemy units) array and Boardscript (map Manager). The last execution of the Initgame () method initializes the contents of the current shutdown.

It is noteworthy in these that, first of all, unity will destroy all the objects created on the previous level at the time of the new one load, but since we have a lot of data in the Gamemanager to be used in the next, we cannot let ourselves be destroyed directly. So you need to use the Dontdestoryonload () method to keep yourself. Boardmanager is a class for managing map generation and storing map data, which will be used as a separate article (estimate the next bar ┗|* ' 0′*|┛).

Next look at the Initgame () method.

First, the Doingsetup tag is modified to true, by modifying this tag, and in the update function to determine the level of the logical update can be paused, to avoid the game in the initialization process has started to run, the next is to update the UI, so that the level in the initialization process will be "X days The information to the overlay, rather than directly displaying the next level of the map, so that if the need to initialize the content is very useful, and can give people a more obvious sense of the transition, the next is to clear the manager before the enemy units held by the reference and the final generation of the map.

It is worth noting here that the Invoke () method is used. The Invoke () method in unity is used to call another method after the specified length of time for the method is called. The method has two parameters, the first one is the method name that needs to be called, and the second is the length of time to wait. In this case, the "Hidelevelimage" method is called after "levelstartdelay" seconds. The contents are as follows:

That is: Remove the information that is overwritten on the level map and set the Doingsetup tag to false so that the game logic is executed in the update function. You can see that the Gameobject.setactive () method is called, and the content of the method is to mark a gameobject as active or inactive, and unity displays only the objects that are marked as active. It is important to note that if an object is a child of another gameobject and its parent object is not marked as active, it will still not appear on the screen even if it is marked as active. However, it can be obtained through the Gameobject.activeself property to whether it is activated.

The onlevelwasloaded () method is used to do some work after the scene switch is over, and is one of the APIs that unity provides.

The function here is to turn the level +1, and then initialize the game. It is important to note that the author looks like the first scene in the beginning of the game (after clicking on the play button to enter the first scene is loaded), the method will not be triggered, only after the scene is loaded after the completion will not be triggered, so do not have to worry about the first close to enter the second level directly. (even if it is triggered, the workaround is simple).

As for the start () method, the author has previously intended to initialize some of the game's data here, but since his execution sequence is awake () and onlevelwasloaded (), it will cause many of the objects needed in the first two methods to be called before they are initialized. So put those work in the awake (), but whether this method is the correct way to use may also need to further explore, since it is a beginner first to achieve the function of the main bar (Power lazy \ (^o^)/~).

The Gameover () method is used to terminate the entire game with the following contents:

The main function is to hide the game's map interface and display the final score. You can see that the Enabled property of Gamemanager is set to false, which will stop the Gamemanager update () execution, which will stop the entire game's logic from being updated.

Addenemytolist () method,

It's simple, called by a script that creates an enemy unit, to let Gamemanager hold a reference to an enemy unit for ease of operation.

The Update () method is also very simple, if the "non-player round, enemy units are not moving, the game is not in the set operation" of the three conditions, then open the Moveenemies ().

The specific process is what, how to achieve, this chapter is not detailed explanation (actually let me say I also said not clear), simply need to know now it is to use a single-threaded way to achieve multi-threading effect on the line, specific in this example, the Moveenemies () method, is to allow the local units to move according to the position of the player, and not because the calculation causes the whole game to completely lose its response during a certain period of time.

In unity, there are two main characteristics that are performed by way of a co-process:

    1. The return value is of type IEnumerator and has no parameters
    2. Yield keyword appears in the method

There may be a lot of things that Ctrip can do, but the broader usage is to do something that needs to be delayed but not to get the program to lose its response during the delay. Because unity's logical processing is single-threaded, it can cause the experience to degrade in some places if the delay is not implemented by this method.

In the method, the enemiesmoving is first marked as true so that the update () method does not open another process causing confusion. With the Yeild return new Waitforsecond () method, you can pause a period after the method executes to that sentence, and then continue to execute from this place. While in the process of waiting, unity takes precedence over some other work, and after the regression condition is met (which is the end of the waiting time), the following statement continues to execute. When all enemy units have been moved, the method will mark Playersturn as true, Mark enemiesmoving as false, and allow the system to enter the player's turn.

The above is the structure of the whole gamemanager. Because the first time to write such articles, the structure is somewhat scattered and because part of the content is intended to continue to explain the next article, so there may be some methods of the content is not introduced clearly, please forgive me.

Unity5 Official tutorial Note (2D Rogue like) 01--game Manager

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.