How to write the U3D game engine game architecture script?

Source: Internet
Author: User
Tags android games

In this article, Momo mainly wants to explain how I write game scripts in unity3d game development. For the unity3d game engine, it is very fast to get started, but it is very difficult to do well. The purpose of this article is to let friends who have used the unity3d game engine learn how to better write game scripts. Of course, this article is closely related to my understanding of game development over the years, you can also have your own opinions. First, let's look at the main components of a game, as shown in. The core of any game on any platform is composed of three parts: data, logic, and rendering.

When you write "=" 2 games ", you will find that game development is" easy ". Why is it" easy? Because at this time, you will find that the game development modes on all platforms, such as the "data" and "logic" are exactly the same, these two parts are irrelevant to the game development platform. However, the "rendering" section is closely related to the gaming platform, because the rendering interfaces of different gaming platforms are different. This proves that programmers who can write a well-written j2_game will surely be able to write the same well for iOS and Android games. When reading this article, please take a look at your company's situation. You may find that your technical director can get started with unity3d game development cocos2d game development in two or three days, this is not a thorough understanding of the game platform, but a strong control of the game data, so he can quickly play with the development of each platform.

As shown in, the unity3d game engine is weighted in game development. The rendering part contains about 100% of the rendering part and about 50% of the logic part. (Because unity3d encapsulates many logic-related APIs for developers to use)

Next we will go back to the compilation of the unity3d script architecture. We know that unity3d can create game scenarios and create game objects in each game scenario, integrating game objects in each scenario is a 3D game. The relationship between game scenarios belongs to the same level. In order to allow previous game scenarios to interact with each other, we need a script that prevails over all scenarios. I call it a "global script ". As shown in, all scenarios can interact with this unique global script. For example, when switching a scenario, you can write the temporary Logical Data to the global script. After switching, you can retrieve the previously saved data from the global script to achieve interaction. (Of course there are other ways to achieve this, but I think this will be better and data will be safer)

Then we enter the scene where the game scene is composed of several game objects. Let's talk about the game objects. A game object must be bound to a game script to complete its lifecycle. The mission of the script is especially important. Because there are a lot of game objects, there will inevitably be interactions with the script, as shown in, many of the unity3d projects in the early stages will be compiled into this. The complexity of interaction with each other makes it possible for your game to develop such scripts. However, when you develop your team during maintenance, you will find that your scripts are very messy, other colleagues don't even know how to change it. (Obviously this is totally wrong)

Let's look at why scripts need to interact with each other for a simple reason. This is because the script needs to use/call another script or a certain data/method of the game object corresponding to another script. In order to solve this problem, the final script is very messy. To avoid this problem, I will do this during development, as shown in. Do not directly interact with each other between scripts, but only indirectly interact with each other. Each game scenario has a singleton script that is above all game objects and stores the public data of all scripts in the scenario. The overall logic update for this scenario is completed in this Singleton script. Each script only interacts with this Singleton script and does not interact with other scripts. (Indirect interaction)

Note that when writing a script, you only need to do the most important thing, just like the functions in the code. Remember not to worry about things unrelated to the script. Do not do too much mutual work in the script, so that all the joint work will be done in the global Singleton script.

Here is an example. The main character is a hacker or a skill attack monster. After the monster is injured, a victory animation is played on the screen.

1. The main character object launches an attack. The Global Singleton script notifies the main character script to play the attack animation after receiving the button event.

2. when an enemy object receives an attack message from the main character, it starts to play the injured animation. When the enemy script receives the collision message from the main character, it asks whether the main character of the single script is "normal attack or skill attack ", then, the enemy plays the corresponding injured animation and begins to reduce blood based on the attack type.

3. Repeat the above operations when the enemy's blood volume is equal to 0. The enemy destroys its own objects, and the enemy script tells the singleton script that it has died. In this case, the singleton script calls the "Victory Animation" object to play the victory animation effect.

The above logic is completely written according to the method described in the figure just now. This can effectively avoid confusion in interaction and interaction, in fact, all situations similar to this type of interaction in development can be well solved using this global Singleton script. I hope that the majority of unity3d developers can discuss with me, because I know that architecture design is not best but better. Too many !!

 

Http://tech.ddvip.com/2013-02/1359996528190113.html

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.