U3d Quick Start

Source: Internet
Author: User

It is difficult to get started with U3D. First, you need to understand the five most important interfaces of U3D. First, you need to use the scenario (Sence) to build the game. Second: hierarchy, where all game objects in the scenario are listed. Third: Inspector. the settings of the selected resource or object are a set of variables and components. Fourth: Game. The demo window is only used for demonstration in playback mode. Fifth: Project, the list of some resources, the same concept as the library.

Then, let's take a look at the eight menus in the main menu bar: File, Edit, resource, game object, Component, and Terrain ), window (Help), familiar with each of these menu commands will be of great Help for future game production.

In U3D, you must have an understanding of Coordinates. The Coordinates of U3D are arranged in the order of (x, y, z). Remember. Familiar with coordinates makes it easier to play games.

If you do not have any programming basics, you can also learn Javascript (or C #), and I do not understand programming when I study. It is fine to learn the Javascript language first, because this engine is mainly a programming tool. Open the Script help document and Monodevelop writer, and start with the simplest shift (transform. Translate.

The basic Unity3D operations are easy to grasp, followed by the core part of the game system: script.

What is a Script )? In short, it is the special text that uses code to execute a series of action commands. It needs the compiler to interpret it again. How to interpret scripts in U3D is not our concern-this is the activity of engine developers, and what we need to know is the script usage rules.

[Features of the three languages]

U3D supports coding in C #, JavaScript, and BOO formats. First, let's briefly introduce the features of these three languages:

For U3D, this is an entry-level scripting language. ud3d built-in functions can be conveniently called through JS. Syntactically, JavaScript is similar to the traditional C language. It requires a semicolon Terminator, variable type definition, braces ...... However, its variable type definition is connected to the right of the variable through a colon, such as: Name: string = "Li ". Compared to the other two languages, using JS syntax, many functions can be directly used without instantiation, such:

Vector3 direction = vector3 (1, 2, 3 ). If you use C #, you need to use the new Keyword: vector3 direction = new vector3 (, 3 ). JavaScript directly inherits from the U3D MonoBehaviour class. Therefore, unlike C # And BOO, you need to use Using or Import to load class libraries. This seems worry-free, but because of the lack of loading special class libraries, there are not many third-party functions that JavaScript can call (of course, we can load the net class library to call JavaScript, although it looks a bit strange ......).

* Note: JavaScript is not Java, and the JavaScript in U3D is also different from the independent JavaScript language.

C # (pronounced C Sharp) is an object-oriented programming language developed by Microsoft. With powerful net class library support and many cross-platform languages derived from this, C # has gradually become a programming language that U3D developers admire. In the U3D built-in script example, C # script also occupies a large part (other scripts are JavaScript scripts ). In addition, on a computer equipped with Visual Studio, we can also use Microsoft's script editing tool to write U3D scripts. At the beginning of C, the syntax is very close to that of C, except for some characteristics of object-oriented language. Of course, I don't need to explain too much here, because C # has a lot of learning materials.

BOO is a new Python-based language. In terms of syntax, BOO and Python are similar. They all end the statement by wrapping a line. They omit semicolons, braces, and even parentheses of conditional statements. Python has been used in many large 3D graphics software. It can be seen that its cross-platform performance is very good. I also choose to use Python to compile the maya special effect script. However, for the compilation of game events, I personally feel that this simplified syntax is somewhat difficult to adapt. For example, the basic variable type definition makes BOO (Python-like) syntax less convenient: direction as vector3 = vector3 (1, 2, 3 ). Game events are different from special effects scripts. The former is the interaction in the process, while the latter only needs to see the results. Therefore, games often require a large number of variables with clear types. The advantage of the BOO language that can omit the variable type is easy to cause problems.

During engine compilation, the execution efficiency of the three languages is the same, because U3D will convert its own language format internally. Although we can call variables and methods between scripts written in different languages, I do not recommend this because there are some unexpected problems in the test. When writing multiple scripts in different languages, try to avoid direct connection between scripts.

Finally, I personally think that C # is the best choice for U3D scripting language on windows.

[Script usage Rules]

The U3D script works in an interesting way. I call it the drag-and-drop method ". Whether it is a specific scenario object or a batch object, the script must first be attached to an element in the scenario before it can be executed. It is easy to assign a script to an object by holding down the left mouse button and dragging the script file to the attribute Panel of the object (you can also drag it to the object in the scene ). U3D has the concept that component (component) is a node similar to Maya. Including scripts, all element attributes are the component of Game objects. Adding, deleting, disabling, reading, and writing component information is what the script has to do (although the script is also a component ).

Net Language C #, when calling variables and methods between different scripts, if the script is located in the same path, you only need to instantiate the new non-static (static) members. For example,. cs and B. cs: to call a non-static variable cc in script a, you need to write: a c = new a () in script B, and then c. the cc format is called. However, as a component, to call members between different scripts, the U3D rule is to use the GetComponent function to complete (in fact, it is equivalent to the role of new, but U3D does not support calling between scripts ). For example:

SomeScript = GetComponent ();

If the JavaScript script is called in the C # script, the force type conversion syntax is used:

SomeScript = GetComponent ("ExampleScript") as ExampleScript;

* <> This special symbol indicates that the generic function in C # is used to avoid forced type conversion and reduce the amount of packing (the value type is designed for reference type operations ).

You can perform the following operations based on the script usage:

1. The script is located on the same object.

You can directly use the generic or type conversion syntax.

For example, someScript = GetComponent ();

2. The script is located on different objects.

You need to use the Find or related search function to obtain the object information of the specified name, and then + ". GetComponent" function. For example, GameObject. Find ("stone"). GetComponent ().

3. The script is located in the same path or the called script is located in and below the main script path (whether the script is used by objects or not ).

Use static identifiers for the members (variables or methods) in the called script, and then directly call the code in the "script. member" format. For example:

ScriptA. CS

...

Public static mm ();

...

ScriptB. CS

...

ScriptA. mm ();

...

However, although the call of static members improves the efficiency, it must be used with caution when a large amount of system resources are required because it is resident in the memory.

* Static is a keyword that defines the variable or method type in C #. You can directly call static variables or methods without the need for new instantiation.

Script content]

In addition to JavaScript Functions, C # And BOO scripts must be preloaded to the class library. Take C # as an example:

Using UnityEngine;

Using System. Collections;

Public class NewBehaviourScript: MonoBehaviour {

}

NewBehaviourScript is the script name, which must be the same as the external name of the script file (if different, the script cannot be executed on the object ). All game execution statements are included in this self-created script inherited from the MonoBehaviour class (in braces ).

The following describes some commonly used built-in running functions (when defining a function, the JavaScript keyword is function, C # is void, and BOO is def. For example, void Start ().

Awake: called during game operation for initialization.

Start: it is executed only once when the game starts and after the Awake () function;

Update: each frame of the game is executed once and after the Start () function;

LateUpdate: Same as Update, but it will be executed after the Update () function is executed;

FixedUpdate: When a rigid-body system is introduced into the game, the physical clock can be synchronized in an adaptive manner to make the dynamics more accurate;

OnGUI: The function used to draw the game interface. Because each frame is executed multiple times, some time-related functions should be avoided directly within it.

OnMouseOver: the content of the function executed when the mouse stays on the object.

OnMouseEnter: the content of the function executed when the mouse enters the object range. Unlike OnMouseOver, this function is executed only once.

OnMouseExit: the content of the function executed when the mouse leaves the object range.

OnMouseDown: the content of the function executed when the mouse is pressed.

OnMouseUp: the content of the function executed when the mouse is released.

OnMouseDrag: the content of the function executed when you hold down the mouse.

The OnMouse series functions are for specified objects. To use global mouse control, you need to use Ray-related functions. There are also many special game trigger event functions, which are not listed here.

The U3D built-in code has a naming rule. The first uppercase phrase at the beginning belongs to a class or function, and the lower-case phrase at the beginning is a variable. New users often confuse the differences between them. Simply put, a function phrase can be used as the type of a variable and can directly execute a function. The phrase is followed by parentheses. A variable is the branch of the corresponding function, the implementation is to control a specific attribute. For example:

Camera and camera. There is a default main Camera in the scene. When the script is on the Camera, Camera. mainCamera. transform and camera. transform is equivalent. If the script is on another object. mainCamera. transform still directly obtains the main camera, while camera. transform must use the Find function to first Find the camera named GameObject. find ("mainCamera "). camera. transform. Of course, the details are listed here for comparison. In actual use, the script is located on a special element and we do not need to declare the category of this element. For example, camera. transform can be simplified to transform.

GameObject and gameObject. The former contains functions for searching, destroying, and generating game objects. At the same time, a variable can be defined as a "game object" type. The latter contains rich game object attributes, such as names, deformation information, animation, rendering, etc.

For the camera listed above, gameObject can also be omitted for scripts directly acting on the specified object. However, when learning to write code, writing a complete variable name can help us better remember the relationship between each attribute and the variable.

Through the comparison above, we can understand that a function is generally a global control (including objects other than a script), and a variable is a specific application object.

* If the statement is not within the scope of the function when writing code, the compiler cannot intelligently display the full name of some variables.

For more functions, refer to the official help documentation.

[Simple example]

When you press the left mouse button on a box, the box starts to rotate, and the light is lit, the "Test" is displayed on the screen.

1. Click Create in the Hierarchy column to Create a Cube, Plane, and Spotlight.

2. In the Assets Directory, Create a Folder (right-click, Create-> Folder) to store various game resources: models, animations, materials, scripts, and Prefab.

3. Double-click the myScript folder and create a C # script.

4. Name the script, and then double-click to enable the script editing tool (if the Script Name is different from the internal class name, make sure to correct it ).

5. Add the mouse-related functions. Here I need to use OnMouseOver, OnMouseExit, and OnMouseDown. Such special functions do not have smart spelling.

6. In terms of syntax, it is not a step-by-step process. The implementation idea is as follows:

When you move the mouse over an object, the color of the object's material turns yellow, and the value of a Boolean variable 1 whose initial value is false is real. When the mouse leaves, the color of the object's material is restored, the value of Boolean 1 is false. When you press the left mouse button and the value of Boolean 1 is true, the value of Boolean 2 is true.

* The OnMouse function is a function that is executed once. Therefore, animation-related control functions cannot be placed in it for execution. A boolean switch is usually used to control the animation functions in the Update function.

7. Implement the following in the Update function:

If the value of Boolean 2 is true, the object is rotated.

8. Drag the written script to the square in the scenario (or select the Square first and drag the script to the attribute bar of the square), adjust the camera position, and perform the test;

9. Because the script only acts on the objects it is attached to, to control the light, we have two options: Create a new script and use the lookup object function. Obviously, I do not need to add a new script for such a simple function. Therefore, I use the Find function to obtain the light intensity attribute.

* When such a long statement is encountered in a script, a custom variable is usually used instead, and the computing workload is often reduced. For example:

Float LightInt = GameObject. Find ("Spotlight"). light. intensity. If initialized in the Start function, the search function does not need to be executed every frame in the Update function to reduce the game efficiency. However, as a test, I ignore it easily.

10. Use of GUI. To display various UI information in the game view, you must use the U3D UI component or GUI function. For ease of reading, I collapse the content of other functions and use the Label of the GUI function to create a simple text display function (for more information about the GUI, see the official documentation, I will discuss it later ).

11. Final test:

12. Release. Run File-> buildset.pdf to open the publishing interface. Set the platform for publishing the game and some release information. Click the Build button to publish a complete game client. The Platform Supported by U3D is related to user authorization and operating systems. For example, IOS games require U3D on MAC platforms for release. Android requires Android SDK packages to be installed on the system, next Generation host platform requires users to purchase additional solutions from the official website.

* Due to the differences in business strategies, U3D just announced that it would suspend the subsequent development of the flash Platform. It seems that the official U3D webpage plug-in should be installed first to play web games developed by U3D.

[Conclusion]

It is not easy to make a game. Here is just a small example of getting started, mainly for script usage. We will learn more about U3D functions later.

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.