C # developing the scene view of the Unity Game tutorial and the use of scripts

Source: Internet
Author: User

C # developing the scene view of the Unity Game tutorial and script using the quick action of the scene view in Unity

Scene view is the most frequently-operated view when developers develop a game. Because once a game object is added to the game's scene, you need to first use the mouse to set the appropriate state for the game object. Also, developers need to look at the various game objects in the game scene in multiple angles. For these reasons, Unity provides a number of quick actions to support the various actions the developer makes to the scene view, which are commonly used in the following ways:

    • Press the Q, W, E, R keys directly on the keyboard to select the top left corner of unity, the 4 buttons on the toolbar, and the buttons correspond to key one by one, eliminating the hassle of developers using mouse clicks.
    • Using the mouse's scroll wheel, you can control the scene view "window" with the distance of each game object, 2-14, and scroll up close, scroll away.

 

Figure 2-14 using the mouse's scroll wheel to control the distance between scene view and the game objects in the scene

    • Using the mouse to double-click the name of the game object on the hierarchy view, the scene view moves until the corresponding object is in the middle of the scene view, as shown in 2-15. This is a quick way to find out when a developer can't find a corresponding game object in the scene.

 

Figure 2-15 Mouse Double-click the object name, scene view speed to the corresponding object, and is displayed in the center of the scene view

    • In the scene view, press the middle mouse button (or scroll wheel), the mouse becomes, and then move the mouse, you can move the scene arbitrarily .
    • As shown in 2-16,
    • 。 Its
    • function is equivalent to the first button on a toolbar.
    • In the scene view, press the ALT key on the keyboard, the mouse will become, and then press the left mouse button drag, can be moved around the specified game object. As shown in 2-17.

Figure 2-16 scene in any moving scene view figure 2-17 moving around an object in the scene view

    • In the scene view, press the right mouse button, the mouse will become the look of the eye, and then press the "W, S, A, D" key on the keyboard, you can simulate the first person in the scene forward, after, left, right, moving the mouse equivalent to rotating the head of the character. As shown in 2-18.

Figure 1-18 simulating the first person, moving the Gizmo tool in the scene view in Figure 2-19

    • Readers must note that there is an axis model in the upper-right corner of the scene view, which is referred to as gizmo,2-19 in unity. It allows you to quickly switch to a predefined viewing angle by using the scene view, and clicking on the gizmo's axis. 2-20, switch three perspectives to see the game objects in the scene view.  

Figure 2-20 viewing game objects in a view with a different perspective

Using Scripts in Unity

Using the method described earlier in this chapter, the reader can change the position of the game objects in the game scene, or rotate and so on. However, it does not seem like a game style to adjust the state of each game in a manual way alone. After all, the player only want to control a game object, as for other game objects, that is the computer needs to consider things! Yes, in order for the game object to move itself, it had to use a script. This section first takes the reader through the script and its effects, and the reader does not care about the implementation details of the code, which is explained in a later section.

Example effects in Unity Show

Here's an example that will move the cube edge of the scene and change its size before starting to move! Isn't it amazing? Here is the entire code of the script that lets the cube rotate and move:

  • Using Unityengine;
  • Using System.Collections;
  • 03
  • public class Mytransform:monobehaviour
  • 05 {
  • public float scalevalue = 1.5f;
  • public float xposition = 0.1f;
  • public int yrotation = 10;
  • Initialization//Use
  • Ten void Start ()
  • 11 {
  • Transform.localscale = new Vector3 (1,scalevalue,1);
  • 13}
  • +//Update is called once per frame
  • void Update ()
  • 16 {
  • Transform. Translate (New Vector3 (xposition,0,0), Space.world);
  • Transform. Rotate (Vector3.up * yrotation,space.world);
  • 19}
  • 20}

This script allows the cube to produce a motion effect of 2-21 as shown.

Figure 2-21 in the game view, the cube changes size first, then moves the cube that rotates on one side

The reader must be wondering why the script can change the state of the cube? In fact, the way it changes the cube state is the same as the way we change the cube state, right! It also achieves the goal by modifying the properties of the transform component on the cube!

The composition of scripts in unity

The code in all the scripts actually defines a class that has the same name as the script. In the class, variables (variable) and methods (methord)are defined, as shown in 2-22.

Figure 2-22 the various components of the script

tip: in the script code, the line of code that starts with a double slash is a comment. is added to illustrate the role of the code and does not have any effect on the script itself.

1. Class

The reader has seen the components of the game object before, and at that time the author tells the reader that it is a generic term for a class of attributes. In fact, the essence of a component is Class! Components and classes are just different names for the same thing under different circumstances. The transformation of the salutation is like this: the class definition is in the script, the script can be added to the game object, and finally becomes part of the game object in the form of a component, as shown in 2-23.

Figure 2-23 classes, scripts, components

2. Variables

The variables in the script are used to store the data, and the reader can think of it as a container. In fact, the properties under the component are variables in nature, as well as the different names of the same things under different scenarios, as shown in 2-24. When you expand a script that becomes a component, you can see that the property name under it is basically the same as the variable name, because the case of letters may be different.

Figure 2-24 variables, attributes

The data stored in the variable becomes the value of the Component property default setting.

3. Methods

A method in a script that accomplishes a specific action or task, for example, in the example in this section, making the cube larger, moving, and rotating is the task that the method completes in the script, as shown in 2-25.

Figure 2-25 The role of the method

Ways to assign scripts to game objects in unity

In unity, there are several ways to assign a script to a game object, and this section describes two of the most common methods, shown in 2-26.

    • Q Using the mouse, drag the script directly from Project view onto the game object specified in the hierarchy view;
    • Q First select the game object specified in the hierarchy view, the Inspector view displays all the components of this game object, using the mouse to drag the script from Project view to the Inspector view;

Finally, by looking at the Inspector view, if a component with the same name as the script appears, you can verify that the script was successfully assigned to the game object.

For the game example in this chapter, the script mytransform is given the cube object (named Mycube). In the course of the game, the cube object will change state, because the script code modifies the properties of the transform component on the cube object, thus realizing the effect of changing the state of the game object.

Figure 2-26 Two ways to assign a script to a game object

Running Games in Unity

In the middle part of unity, there are 3 buttons at the location of the toolbar, 2-27 of which are running the game, aborting the game, and running the game in one step. The reader can click the corresponding button to decide how to run the game.

Figure 2-27 Control 3 toolbar buttons for game run

Summary

The contents of this chapter can be divided into three parts. The first part is to build the game scene, the reader can learn the action of the game scene, as well as the method of adding the game object, the second part is to change the state of the game object, that is, the position, orientation and size of the game object, to understand the transform component, and the third part is to use the script, The script eliminates the hassle of manually changing the state of the game object, and also makes the project look like a game, so that it understands the various components of the script, namely, classes, variables and methods, and the relationship between the components of the script and the game object.

This article is selected from: C # Game Development Quick Start University PA Internal information, reproduced please indicate the source, respect the technology respect the IT person!

C # developing the scene view of the Unity Game tutorial and the use of scripts

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.