官术网_书友最值得收藏!

Introducing the class

The class plays a major role in Unity. Most of your code will be written inside classes. Think about it like a container for variables and methods.

You just learned about variables and methods. These two items are the building blocks used in Unity scripts. The term "script" is used everywhere in discussions and documents. Look for it in the dictionary, and you will see that it can generally be described as written text. Sure enough, that's what we have. However, since we aren't just writing a screenplay or passing a note to someone, we need to learn the actual terms used in programming.

Unity calls the code it creates a C# script. However, people like me have to teach you some basic programming skills and tell you that a script is really a class.

Note

In the previous section about methods, we created a class (script) called LearningScript. It contained a couple of variables and a method. The main concept, or idea, of a class is that it's a container of data, stored in variables, and methods that process that data in some fashion. Because I don't have to constantly write class (script), I will be using the word "script" most of the time. However, I will also be using "class" when getting more specific with C#. Just remember that a script is a class that is attached to a GameObject.

A script is like a blueprint or a written description. In other words, it's just a single file in a folder on our hard drive. We can see it right there in the Projects panel. It can't do anything by just sitting there. When we tell Unity to attach it to a GameObject, we aren't creating another copy of the file. All we're doing is telling Unity that we want the behaviors described in our script to be a component of the GameObject.

When we click on the Play button, Unity loads the GameObject into the computer's memory. Since the script is attached to a GameObject, Unity also has to make a place in the computer's memory to store a component as part of the GameObject. The component has the capabilities specified in the script (blueprint) that we created.

It is worth knowing that not every class is a Unity component. In object-oriented programming, we use classes to organize the project. The last thing I want to do is get you confused at this stage, so it's a good idea here to write some code examples. Don't worry about writing it in your MonoDevelop. Just look at the examples and try to understand what classes might be used for.

Example 1 – Student:

using UnityEngine;
using System.Collections;

public class Person : MonoBehaviour {

  public string firstName = "Greg";
  public string lastName = "Lukosek";
  public string emailAddress = "lukos86@gmail.com";
  public int age = 28;
  public float heightInMeters = 1.75f;

}

Example 2 – Car:

using UnityEngine;
using System.Collections;

public class Car : MonoBehaviour {

  public string make = "Tesla"
  public string model = "S";
  public int numberOfWheels = 4;
  public int topSpeed = 250;

}

Inheritance

Unity components inherit from MonoBehaviour. For beginners to Unity, studying C# inheritance isn't a subject you need to learn in any great detail, but you do need to know that each Unity script uses inheritance. We see the code in every script that will be attached to a GameObject. In LearningScript, the code is on line 4:

public class LearningScript : MonoBehaviour

The colon and the last word of this code mean that the LearningScript class is inheriting behaviors from the MonoBehaviour class. This simply means that the MonoBehaviour class is making a few of its variables and methods available to the LearningScript class. It's no coincidence that the variables and methods inherited look like some of the code that we saw in the Unity Scripting Reference.

The following are the two inherited behaviors in the LearningScript class:

Line 10: void Start ()
Line 15: void Update ()

You don't have to call these methods; Unity calls them behind the scenes. So, the code that you place in these methods gets executed automatically.

主站蜘蛛池模板: 西吉县| 图木舒克市| 荃湾区| 新巴尔虎右旗| 藁城市| 佳木斯市| 惠东县| 汝城县| 恩施市| 怀宁县| 文安县| 柯坪县| 平遥县| 两当县| 雷波县| 株洲市| 漠河县| 乃东县| 上饶县| 荔波县| 基隆市| 沁水县| 潢川县| 丰原市| 静乐县| 通河县| 黄山市| 新乡市| 分宜县| 桦南县| 保定市| 汉源县| 县级市| 罗甸县| 石阡县| 兖州市| 饶阳县| 德令哈市| 庆云县| 延长县| 法库县|