---
title: "类"
content_type: tutorial
url: "https://learn.unity.com/tutorial/lei-l92v5?language=zh-cn"
language: zh-cn
difficulty: beginner
duration_minutes: 10
unity_version:
  - "2019.3"
  - "2019.2"
  - "2019.1"
  - "2018.4"
  - "2018.3"
  - "2018.2"
  - "2018.1"
  - "2017.4"
  - "2017.3"
  - "2017.2"
  - "2017.1"
  - "5.x"
  - "4.x"
supported_unity_versions:
  - name: "2019.3"
    url: "https://learn.unity.com/tutorial/lei-l92v5.md?language=zh-cn&version=2019.3"
  - name: "2019.2"
    url: "https://learn.unity.com/tutorial/lei-l92v5.md?language=zh-cn&version=2019.2"
  - name: "2019.1"
    url: "https://learn.unity.com/tutorial/lei-l92v5.md?language=zh-cn&version=2019.1"
  - name: "2018.4"
    url: "https://learn.unity.com/tutorial/lei-l92v5.md?language=zh-cn&version=2018.4"
  - name: "2018.3"
    url: "https://learn.unity.com/tutorial/lei-l92v5.md?language=zh-cn&version=2018.3"
  - name: "2018.2"
    url: "https://learn.unity.com/tutorial/lei-l92v5.md?language=zh-cn&version=2018.2"
  - name: "2018.1"
    url: "https://learn.unity.com/tutorial/lei-l92v5.md?language=zh-cn&version=2018.1"
  - name: "2017.4"
    url: "https://learn.unity.com/tutorial/lei-l92v5.md?language=zh-cn&version=2017.4"
  - name: "2017.3"
    url: "https://learn.unity.com/tutorial/lei-l92v5.md?language=zh-cn&version=2017.3"
  - name: "2017.2"
    url: "https://learn.unity.com/tutorial/lei-l92v5.md?language=zh-cn&version=2017.2"
  - name: "2017.1"
    url: "https://learn.unity.com/tutorial/lei-l92v5.md?language=zh-cn&version=2017.1"
  - name: "5.x"
    url: "https://learn.unity.com/tutorial/lei-l92v5.md?language=zh-cn&version=5.x"
  - name: "4.x"
    url: "https://learn.unity.com/tutorial/lei-l92v5.md?language=zh-cn&version=4.x"
available_languages:
  - name: "en"
    url: "https://learn.unity.com/tutorial/classes-5.md?language=en"
  - name: "es"
    url: "https://learn.unity.com/tutorial/clases-wm.md?language=es"
  - name: "ja"
    url: "https://learn.unity.com/tutorial/classes-5-jp.md?language=ja"
  - name: "ko"
    url: "https://learn.unity.com/tutorial/keulraeseu.md?language=ko"
  - name: "zh-cn"
    url: "https://learn.unity.com/tutorial/lei-l92v5.md?language=zh-cn"
last_updated: "2026-08-04T14:24:42.000Z"
---

# 类

学习在 C# 中使用类组织数据，并编写构造函数来初始化类的各个部分。

如何使用类来存储和组织信息，以及如何创建构造函数以便处理类的各个部分。

本教程包含在“[初级编程](https://learn.unity.com/project/chu-ji-bian-cheng?language=zh)”项目中。

上一教程：[数据类型](https://learn.unity.com/tutorial/shu-ju-lei-xing?language=zh&projectId=5e3afd88edbc2a2998252c77)

下一教程：[Instantiate](https://learn.unity.com/tutorial/instantiate-1?language=zh&projectId=5e3afd88edbc2a2998252c77)

## 1. 类

[Video](https://connect-mediagw.unity.com/h1/20200206/videos/15e194eb-cef2-48d5-af9d-fc3dedeaa3ca_22_C__Classes_in_Unity!___Beginner_Scripting_Tutorial.mp4) ([Transcript (zh_cn)](https://connect-mediagw.unity.com/h1/20200206/ff17bf86-be4e-4634-aa63-3043cde5c7f2_22_C__Classes_in_Unity____Beginner_Scripting_Tutorial___Chinese.vtt))

**SingleCharacterScript**

```csharp
using UnityEngine;
using System.Collections;

public class SingleCharacterScript : MonoBehaviour
{
    public class Stuff
    {
        public int bullets;
        public int grenades;
        public int rockets;
        
        public Stuff(int bul, int gre, int roc)
        {
            bullets = bul;
            grenades = gre;
            rockets = roc;
        }
    }
    
    
    public Stuff myStuff = new Stuff(10, 7, 25);
    public float speed;
    public float turnSpeed;
    public Rigidbody bulletPrefab;
    public Transform firePosition;
    public float bulletSpeed;
    
    
    void Update ()
    {
        Movement();
        Shoot();
    }
    
    
    void Movement ()
    {
        float forwardMovement = Input.GetAxis("Vertical") * speed * Time.deltaTime;
        float turnMovement = Input.GetAxis("Horizontal") * turnSpeed * Time.deltaTime;
        
        transform.Translate(Vector3.forward * forwardMovement);
        transform.Rotate(Vector3.up * turnMovement);
    }
    
    
    void Shoot ()
    {
        if(Input.GetButtonDown("Fire1") && myStuff.bullets > 0)
        {
            Rigidbody bulletInstance = Instantiate(bulletPrefab, firePosition.position, firePosition.rotation) as Rigidbody;
            bulletInstance.AddForce(firePosition.forward * bulletSpeed);
            myStuff.bullets--;
        }
    }
}
```

## **Inventory**

```csharp
using UnityEngine;
using System.Collections;

public class Inventory : MonoBehaviour
{
    public class Stuff
    {
        public int bullets;
        public int grenades;
        public int rockets;
        public float fuel;
        
        public Stuff(int bul, int gre, int roc)
        {
            bullets = bul;
            grenades = gre;
            rockets = roc;
        }
        
        public Stuff(int bul, float fu)
        {
            bullets = bul;
            fuel = fu;
        }
        
        // 构造函数
        public Stuff ()
        {
            bullets = 1;
            grenades = 1;
            rockets = 1;
        }
    }
    

    // 创建 Stuff 类的实例（对象）
    public Stuff myStuff = new Stuff(50, 5, 5);
    
    public Stuff myOtherStuff = new Stuff(50, 1.5f);
    
    void Start()
    {
        Debug.Log(myStuff.bullets); 
    }
}
```

## **MovementControls**

```csharp
using UnityEngine;
using System.Collections;

public class MovementControls : MonoBehaviour
{
    public float speed;
    public float turnSpeed;
    
    
    void Update ()
    {
        Movement();
    }
    
    
    void Movement ()
    {
        float forwardMovement = Input.GetAxis("Vertical") * speed * Time.deltaTime;
        float turnMovement = Input.GetAxis("Horizontal") * turnSpeed * Time.deltaTime;
        
        transform.Translate(Vector3.forward * forwardMovement);
        transform.Rotate(Vector3.up * turnMovement);
    }
}
```

## **Shooting**

```csharp
using UnityEngine;
using System.Collections;

public class Shooting : MonoBehaviour
{
    public Rigidbody bulletPrefab;
    public Transform firePosition;
    public float bulletSpeed;
    
    
    private Inventory inventory;
    
    
    void Awake ()
    {
        inventory = GetComponent<Inventory>();
    }
    
    
    void Update ()
    {
        Shoot();
    }
    
    
    void Shoot ()
    {
        if(Input.GetButtonDown("Fire1") && inventory.myStuff.bullets > 0)
        {
            Rigidbody bulletInstance = Instantiate(bulletPrefab, firePosition.position, firePosition.rotation) as Rigidbody;
            bulletInstance.AddForce(firePosition.forward * bulletSpeed);
            inventory.myStuff.bullets--;
        }
    }
}
```
