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

Classes and object-oriented programming

A class is an amalgam of many related variables and functions, all brought together into a self-contained unit or "thing". To put it another way, if you think about a game (such as a fantasy RPG), it's filled with many independent things such as wizards, orcs, trees, houses, the player, quests, inventory items, weapons, spells, doorways, bridges, force fields, portals, guards, and so on. Many of these objects parallel objects in the real world too. However, crucially, each of these things is an independent object; a wizard is different and separate from a force field, and a guard is different and separate from a tree. Each of these things, then, can be thought of as an object—a custom type. If we focus our attention on one specific object, an orc enemy, for example, we can identify the properties and behaviors in this object. The orc will have a position, rotation, and scale; these correspond to variables.

The orc might have several kinds of attacks too, such as a melee attack with an axe and a ranged attack with a crossbow. These attacks are performed through functions. In this way, a collection of variables and functions are brought together into a meaningful relationship. The process of bringing these things together is known as encapsulation. In this example, an orc has been encapsulated into a class. The class, in this case, represents the template for a general, abstract orc (the concept of an orc). Objects, in contrast, are particular, concrete instantiations of the Orc class in the level. In Unity, script files define a class. To instantiate the class as an object in the level, add it to GameObject. As we've seen, classes are attached to game objects as components. Components are objects, and multiple components together form a GameObject. Refer to code sample 1-10 for a sample Orc class stub:

01 using UnityEngine;
02 using System.Collections;
03 
04 public class Orc : MonoBehaviour 
05 {
06 //Reference to the transform component of orc (position, rotation, scale)
07 private Transform ThisTransform = null;
08 
09 //Enum for states of orc
10 public enum OrcStates {NEUTRAL, ATTACK_MELEE, ATTACK_RANGE};
11 
12 //Current state of orc
13 public OrcStates CurrentState = OrcStates.NEUTRAL;
14 
15 //Movement speed of orc in meters per second
16      public float OrcSpeed = 10.0f;
17 
18 //Is orc friendly to player
19 public bool isFriendly = false;
20 
21 //--------------------------------------------------
22 // Use this for initialization
23 void Start ()
24 {
25       //Get transform of orc
26       ThisTransform = transform;
27 }
28 //--------------------------------------------------
29 // Update is called once per frame
30 void Update ()
31 {
32 }
33 //--------------------------------------------------
34 //State actions for orc
35 public void AttackMelee()
36 {
37        //Do melee attack here
38 }
39 //--------------------------------------------------
40 public void AttackRange()
41 {
42        //Do range attack here
43 }
44 //--------------------------------------------------
45 }

The following are the comments for code sample 1-10:

  • Line 04: Here, the class keyword is used to define a class named Orc. This class derives from MonoBehaviour. The next section of this chapter will consider inheritance and derived classes further.
  • Lines 09-19: Several variables and an enum are added to the Orc class. The variables are of different types, but all are related to the concept of an orc.
  • Lines 35-45: The orc has two methods: AttackMelee and AttackRange.

    Tip

    More information on classes and their usage in C# can be found at http://msdn.microsoft.com/en-gb/library/x9afc042.aspx.

主站蜘蛛池模板: 濉溪县| 彰武县| 仙游县| 同仁县| 五台县| 开封县| 固安县| 涿鹿县| 田阳县| 巩留县| 高青县| 湟中县| 黄梅县| 平陆县| 渝北区| 明水县| 柳州市| 安图县| 白玉县| 凤山县| 皋兰县| 城步| 辉县市| 阳谷县| 沁源县| 枣强县| 房产| 仙居县| 巴青县| 汽车| 冀州市| 泌阳县| 巴塘县| 都安| 鄂尔多斯市| 萝北县| 兴宁市| 凭祥市| 绥江县| 财经| 怀集县|