C # Developing Unity Game Tutorial property variables for Game objects

Source: Internet
Author: User
Tags naming convention

C # Developing Unity Game Tutorial property variables for Game objects Unity game object Properties--variables

By learning from the previous chapter, the reader should be aware that the properties on the game object and the variables in the script, the way to establish a connection is to give the script to the game object. The previous chapter simply introduced the variables in the script, the reader must not be fun, and now, the author will use an entire chapter to introduce variables and attributes.

Properties of Unity Game objects

In unity, the properties of the game object in the game scene can be viewed in the Inspector view to see which game object's properties, using the mouse to select which game object. For example, to see the properties of the main camera object in the game scene, simply select the main camera in the hierarchy view and the Inspector view will show all its properties, as shown in 3-1.

 

Figure 3-1 Viewing the properties of the main camera object in the Inspector View

From the previous chapter, the reader should be aware that attributes are displayed under the component, the essence of which is the class, and the nature of the attribute is the variable, except that the user cannot view the code of Unity's built-in component classes. Therefore, the following scripts are written to illustrate the components of the class defined in the script, attributes to the variables in the script, the correspondence between them.

(1 in Project view, click create| C # script command, create a script and name it myscript, double-click to open the script, and add the following code to the script:

    • Using Unityengine;
    • Using System.Collections;
    • 03
    • public class Myscript:monobehaviour
    • 05 {
    • public int ivalue;
    • public string strvalue;
    • public float Fvalue;
    • 09}

Tip: The newly created script, which has a lot of unity Auto-added code, can be deleted by the reader for code that has no practical meaning.

(2 Drag the MyScript script onto the main camera object, use the Inspector view again to see the properties of the main camera object, 3-2, and you'll see that the script becomes a component and becomes part of the main camera object. And the properties shown under this component are the variables in the script.

Figure 3-2 Script to component, variable to attribute

Unity Middle Finger The name of the generation data

Since the nature of the object's properties is the variable, what are the variables in the script? In the previous chapter, it was explained to the reader that a variable is actually a unit that stores data, and the "variable" that the reader sees in the script should be exactly "variable name". "Variable name" means the name of the variable, which is the role of the variable, or refers to the data in the variable. For the example in the previous section, the variable names in the script are shown in Ivalue, strvalue, and fvalue,3-3.

Figure 3-3 the variable name in the script

Unity Variable Naming conventions

The variable name in the script looks like a string of letters, but it has its own naming convention, which is defined by C # syntax, that is: variable names can only consist of letters, numbers, and underscores, and can only start with a letter or underscore. The explanation for this rule is as follows:

    • Q The case of the same letter is different for the variable name. For example, Ivalue and ivalue are different variable names;
    • Q cannot start with a number. For example, Ivalue and _value are legal, but 1Value is not legal;

Unity variable naming convention

"Rules" must be adhered to, mandatory, non-compliance, there will be grammatical errors. and the "convention" is to see the intention, the reader can not abide by the Convention, and will not make any grammatical mistakes. Although it is not mandatory, readers should also be aware of it, as adherence to the Conventions facilitates the reading of code and the collaborative work of game programmers.

1. Select a meaningful name

It's a good idea to choose a meaningful name for the variable, otherwise the reader will forget the name of a variable for a long time, and what kind of data the variable name wants to refer to. Here is an example to illustrate:

    • public bool Thebearmakesbigpottyinthewoods = true;

This variable name is really long! But it does clearly describe itself, so the instant reader sees this variable 10 years from now, and can still learn from its name what a deadly variable name is, and what kind of data the variable name wants to refer to. Now change the name of the variable to another simple point:

    • public bool Potty = true;

The name is really much more concise! After all, a lot of descriptive information is missing. Perhaps the reader remembers its role in naming the variable, but it doesn't have to be in a few weeks. By then, the reader will have to read more code to speculate on the role of this variable name.

2. Start with lowercase letters

Use lowercase letters as the first letter of the variable name to differentiate the variable name from the class name, which makes it easy for code readers to know whether the name is a variable name or a class name. Because the class name usually starts with a larger letter, it's a good idea to have the variable name start with a lowercase letter. For example:

    • Transform; Class name
    • Transform Variable name

If strictly abide by the naming convention, it is easy to know that the first is the class name, the second is the variable name.

tip: in the chapters later in this book, the reader will see the naming convention for the class, at which point the author will tell you that the class name should preferably start with a capital letter. Readers can also use other naming conventions, but regardless of the naming convention, the purpose is consistent, in order to improve the readability of the code to facilitate the collaboration of multiple game programmers.

Unity variable name is slightly different from property name

Through the first section of this chapter, the reader has learned that the nature of the properties on the game object is the variable, and the property name and variable name are also consistent. But careful readers may find that the attribute name is not exactly the same as the variable name, at least the first letter is case-insensitive, as shown in 3-4.

Figure 3-4 The variable name is not exactly the same as the property name

In fact, the same problem occurs when the script name (class name) is inconsistent with the component name. For example, the component name of MyScript (script name, class name) is my script. This may be trivial, but it embodies unity's thoughtfulness and thoughtful, does the reader not think that the changes made by unity make the variable name more readable after it becomes the attribute name? That's the way it is.

The visibility of unity property names

Readers should never assume that as long as there are variables in the script, they will appear in the list of properties under the component after the script becomes a component. The idea is wrong, because deciding whether a variable will appear under a component is a property factor, that is, if the modifier is public or private. As shown in the previous section of this chapter, the variables are preceded by a public adornment, so they are displayed in the properties under the component, 3-5.

Figure 3-5 the modified part before the variable-- Public

If you change the pre-strvalue modifier public to private, after you save the changes to the script code, look at the properties under the component again, you will find that the property named Str value is missing, as shown in 3-6.

Figure 3-6 The property that was modified by private is missing

Note: The modification of variables can be omitted in the script, that is, there is no grammatical error in writing public and private. At this point, unity will default on the variable being private, so this variable will not appear as an attribute under the component, as shown in 3-7.

Figure 3-7 omitting the modified variable

The C # syntax stipulates that variables that are modified by public can be accessed and modified by the outside world, which means that the data stored by the variable is allowed to be modified outside the script. So developers can modify the data stored in the variables in the script through attributes in unity. The variables that are modified by private are just the opposite of public, refusing to be accessed and modified by the outside world. Therefore, the data in the variable cannot be modified by the outside world, and unity will not display such variables as attributes in the Inspector view. So it's a very sensible decision!

Unity sets the data type of the property

Attributes are variables, and they are just different titles of the same thing in different situations. In unity, you should say that attributes have different data types, but if you're in a script, you can say that variables have different data types. What is the data type? The reader is actually already seen in the script, as shown in 3-8.

Figure 3-8 3 types of data in a script

Data types in Unity

Computers can store a wide variety of data, and computers store and process different types of data differently. Therefore, in order for the computer to better process the specified data, it is best to specify the type for the data. The data types in C # can be divided into the categories shown in Figure 3-9.

Figure 3-9 Classification of data types

The data types commonly used in unity are simple value types, as shown in table 3.1.

Table 3.1 Simple value types

Set the type for a variable or property to see which range the data is in. For example, 262 can only be stored with a long type, but for 50, you can use all of the integer type storage, which is the most appropriate type to see.

The embodiment of the Unity property data type

For a property, a property of different data types can store different kinds of data, so its data input form in inspector will be different, as shown in 3-10.

Figure 3-10 Inspector view, different kinds of attributes

    • The data for the Q int, float, and double types are numeric, so unity automatically assigns a value of 0 to the attribute when no specific value is specified for the ivalue, Fvalue, and dvalue data.
    • Q The data of type string is a character, so unity automatically assigns a value of "null character" to the attribute when the specific value of the data referred to by strvalue is not specified;
    • The value of the Q bool type can only be true or false, so unity uses a check box to represent the data referred to by bvalue, and the default setting is False, indicating that the value is true when the check box is checked;

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 Unity Game Tutorial property variables for Game objects

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.