Unity3d How to organize code effectively?

Source: Internet
Author: User

Welcome to Unity Learning, unity training, Unity Enterprise training and education zone, there are many u3d resources, u3d training videos, u3d tutorials, U3d Frequently asked questions, U3d Project source code, we are committed to creating the industry Unity3d training, learning the first brand.

Unity3d can be said to be the height of component-based Architecture,
At the same time its library provides a large number of global variables.

To be precise, code as a resource in a unity project should extend to how unity resources are organized. Simply talk about our experience:
1. Unity has some of its own conventions, such as the Editor,plugins in the project as the editor, plugin directory and so on. Well-known plugins will store a directory of their own, such as Ngui.
So our own code, the general directory name will be underlined beginning, such as "_scripts", "_prefabs" and so on.
For directories such as scenes, documents, and so on, underline them with two lines so that they can be at the top of the line.

2. Code in C #, do not use JS. Use namespace to enclose your code, if necessary. We are using namespace to store our own public libraries.

3. C # Comments to write carefully, play///can help you complete, no reason to lazy.

4. Each program file at the beginning of a note to write a modified log, who changed what simple leave a note. Even if you use Unity version management or GIT, those logs will eventually be lost, only to carefully write the log in the code, will be conscious to seriously optimize it.

5. Unity's scripting logic, in terms of functionality is broadly divided into two, one is relatively independent, such as 1 seconds after the explosion disappeared, such a separate script to bind to the target.


It's more about interacting with other scripts in the script. Unity provides a kind of balm method is SendMessage, this method performance is slightly poor, if you call the frequency is not high, casually use also no harm. Another method is to invoke it directly from an instance of the object.
Our approach is to write a few common controllers that let them do their job and take charge of their own things:
1. Write a GlobalManager.cs to control the game's global variables and global methods. Static class mode. For example, the current play to the first few mark the number of small clearance, the player's gold coins and so on.

2. Write a GameController.cs to control the current game process. Single-instance mode. The main loop of the game is also controlled by it. Initialization, victory, failure determination and so on.

3. Write a InputController.cs to control all user input. Single-instance mode. Mouse, keyboard, touch screen, we do the game is guaranteed to support these three kinds of input, because most of the time is tested on the PC.
The connection between Gamecontroller and Inputcontroller is a bit tangled. In general, the GameController.Instance.Foo () execution method is called in Inputcontoller. or directly to input written listener mode, let Gamecontroller to listen.

4. Other similar menu controllers, sound Controllers, achievement controllers, IAP virtual prop controllers, etc., are also managed using a similar approach.

5. About the operation of the Playerpref, the unified written static class Get/set mode, the program where to use the direct read and write.

6. If the number of scenes in your project is small (<5), then the resources dragged into the scene can be arbitrary. If the number of scenes (dozens of, some puzzle game each level is a scene), then drag into the scene of the number of prefab must be less.

7. Design your prefab resources, you want to imagine when other people get these resources, whether directly dragged into an empty scene can run, at most a few simple settings. If the resources you design don't do that, think again.


Write these, feel can't write down.
Want to thoroughly understand unity, at least have to make several products put on the line. The process of really doing products will encounter a variety of unexpected problems, code is constantly being reconstructed and compromised, there is no best solution.
Write these for the time being, hope to be able to initiate.

(PS: Of course, this is based on the company's big project, each company has its own set of norms and framework, slightly different.) If you want to hit a plane, 2048 of these games are not so troublesome. Just look at it.

For more highlights, please click http://www.gopedu.com/

Unity3d How to organize code effectively?

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.