2.

Unity C# でよく使うクラス一覧完全リファレンス

編集
この記事の要点
  • GameObject / Component / Transform がすべての土台
  • 挙動を書く Script は MonoBehaviour を継承
  • 位置回転は Vector3 / Quaternion、時間は Time、入力は Input
  • 物理は Rigidbody / Collider、レンダリングは Camera / Light / Renderer
  • 音は AudioSource、アニメは Animator、データ保持は ScriptableObject

Unity C# クラスのカテゴリ

Unity でゲームを作る際によく使うクラスをカテゴリ別にまとめます。すべて UnityEngine 名前空間にあります。

1. 基本クラス: GameObject / Component / Transform

// GameObject: シーン内のすべてのオブジェクトの基底
GameObject obj = new GameObject("MyObject");
GameObject prefab = Resources.Load("Prefabs/Enemy");
GameObject instance = Instantiate(prefab, position, Quaternion.identity);
Destroy(instance, 2f);   // 2 秒後に削除

// Component: GameObject にアタッチされるすべての機能
Rigidbody rb = obj.GetComponent();
Rigidbody added = obj.AddComponent();

// Transform: 位置・回転・スケール(GameObject に必ず 1 つ)
obj.transform.position = new Vector3(0, 10, 0);
obj.transform.SetParent(parentTransform);

2. MonoBehaviour(スクリプトの基底)

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    void Awake() { /* インスタンス化直後(1 回) */ }
    void Start() { /* 最初のフレームの直前(1 回) */ }
    void Update() { /* 毎フレーム */ }
    void FixedUpdate() { /* 物理更新ごと(固定間隔) */ }
    void LateUpdate() { /* Update 後 */ }
    void OnEnable() { /* 有効化時 */ }
    void OnDisable() { /* 無効化時 */ }
    void OnDestroy() { /* 破棄時 */ }
    void OnCollisionEnter(Collision c) { /* 衝突開始 */ }
    void OnTriggerEnter(Collider c) { /* トリガー進入 */ }
}

3. 数学系: Vector / Quaternion / Mathf

クラス用途主要メンバ
Vector22D ベクトル(UI / 2D ゲーム)x, y, magnitude, normalized
Vector33D ベクトル(位置・方向)x, y, z, forward, up, right
Vector44 次元(色 RGBA など)x, y, z, w
Quaternion回転(4 元数)Euler, AngleAxis, LookRotation
Mathf数学関数(float 版)Sin, Cos, Lerp, Clamp, PI
Random乱数Range, value, insideUnitSphere
Vector3 v = new Vector3(1, 2, 3);
float length = v.magnitude;
Vector3 dir = v.normalized;
float dot = Vector3.Dot(a, b);
Vector3 cross = Vector3.Cross(a, b);
Vector3 lerped = Vector3.Lerp(a, b, 0.5f);

Quaternion rot = Quaternion.Euler(0, 90, 0);
Quaternion look = Quaternion.LookRotation(target.position - transform.position);

float clamped = Mathf.Clamp(value, 0f, 1f);
float lerped2 = Mathf.Lerp(0, 100, t);
float pi = Mathf.PI;

float r = Random.Range(0f, 1f);
int n = Random.Range(0, 10);   // 0-9
Vector3 p = Random.insideUnitSphere * radius;

4. 時間と入力: Time / Input

// Time
float dt = Time.deltaTime;         // 前フレームからの経過秒
float fixedDt = Time.fixedDeltaTime;
float time = Time.time;            // 起動からの秒数
Time.timeScale = 0;                // ポーズ(0 で停止)
Time.timeScale = 0.5f;             // スローモーション

// Input(旧 Input System)
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
if (Input.GetKeyDown(KeyCode.Space)) { /* ジャンプ */ }
if (Input.GetMouseButton(0)) { /* 左クリック中 */ }
Vector3 mouse = Input.mousePosition;

// 新 Input System なら using UnityEngine.InputSystem;

5. 物理: Rigidbody / Collider

// Rigidbody: 重力・力・速度
Rigidbody rb = GetComponent();
rb.AddForce(Vector3.up * 500f);
rb.velocity = new Vector3(5, 0, 0);
rb.useGravity = false;
rb.isKinematic = true;             // 力の影響を受けない

// Collider: 衝突判定の形
// BoxCollider / SphereCollider / CapsuleCollider / MeshCollider
BoxCollider box = GetComponent();
box.size = new Vector3(1, 2, 1);
box.isTrigger = true;              // 通り抜け+イベントだけ

// Raycast
Ray ray = new Ray(transform.position, Vector3.forward);
if (Physics.Raycast(ray, out RaycastHit hit, 100f))
{
    Debug.Log($"Hit: {hit.collider.name}");
}

// 2D 用は Rigidbody2D / BoxCollider2D / Physics2D

6. レンダリング: Camera / Light / Renderer

// Camera
Camera cam = Camera.main;
cam.fieldOfView = 60;
cam.backgroundColor = Color.black;
Vector3 screenPos = cam.WorldToScreenPoint(transform.position);
Ray ray = cam.ScreenPointToRay(Input.mousePosition);

// Light
Light light = GetComponent();
light.color = Color.yellow;
light.intensity = 1.5f;
light.type = LightType.Directional;   // Directional / Point / Spot / Area

// Renderer(基底クラス、MeshRenderer / SpriteRenderer などが派生)
Renderer rend = GetComponent();
rend.material.color = Color.red;
rend.material.SetFloat("_Metallic", 0.5f);
rend.enabled = false;                  // 非表示

7. 音とアニメーション

// AudioSource
AudioSource audioSrc = GetComponent();
audioSrc.clip = bgmClip;
audioSrc.loop = true;
audioSrc.Play();
audioSrc.PlayOneShot(seClip, 0.8f);    // 効果音

// AudioListener はカメラに 1 つ

// Animator(Mecanim)
Animator anim = GetComponent();
anim.SetBool("isWalking", true);
anim.SetFloat("speed", 5f);
anim.SetTrigger("jump");
anim.Play("Attack");

// Animation(古い、レガシー)
Animation animLegacy = GetComponent();
animLegacy.Play("Idle");

8. データ保持: ScriptableObject

// アセットとして保存できるデータコンテナ
[CreateAssetMenu(fileName = "WeaponData", menuName = "Data/Weapon")]
public class WeaponData : ScriptableObject
{
    public string weaponName;
    public int damage;
    public float attackSpeed;
    public AudioClip swingSound;
    public GameObject hitEffect;
}

// 使用側
public class Player : MonoBehaviour
{
    public WeaponData weapon;

    void Attack()
    {
        Debug.Log($"{weapon.weaponName} で攻撃: {weapon.damage} ダメージ");
    }
}

9. コルーチン(Coroutine)

void Start()
{
    StartCoroutine(SpawnLoop());
}

IEnumerator SpawnLoop()
{
    while (true)
    {
        Instantiate(enemyPrefab, RandomPos(), Quaternion.identity);
        yield return new WaitForSeconds(2f);   // 2 秒待つ
    }
}

IEnumerator FadeOut(SpriteRenderer sr)
{
    for (float t = 1f; t > 0; t -= Time.deltaTime)
    {
        sr.color = new Color(1, 1, 1, t);
        yield return null;       // 1 フレーム待つ
    }
}

10. UI と Texture / Material

// UI(using UnityEngine.UI;)
Button btn = GetComponent

頻度別早見表

クラス
★★★GameObject, MonoBehaviour, Transform, Vector3, Time, Debug
★★★Input, Rigidbody, Collider, Quaternion, Mathf
★★Camera, Animator, AudioSource, Renderer, Material
★★Coroutine, ScriptableObject, Color, Random, RaycastHit
Light, Texture, Sprite, Canvas, Button, Text, TMP_Text
NavMeshAgent, ParticleSystem, LineRenderer, Gizmos

FAQ

Q: GameObject.Find は遅い?
A: 重い。シーン内全 GameObject を走査する。Inspector で参照を設定するか、FindObjectOfType を Start で 1 回呼ぶのが望ましい。

Q: MonoBehaviour を new できない
A: 不可。AddComponent() か Prefab を Instantiate する。

Q: ScriptableObject と class の違い
A: ScriptableObject はアセットとして保存できる。複数 Prefab で共有するデータ(武器ステータス、敵パラメータ等)に最適。

編集
Post Share
子ページ
  1. MonoBehaviour
同階層のページ
  1. スクリプトの作成と実行
  2. C# のクラス一覧
  3. シーンの移動方法
  4. オブジェクトの移動・回転
  5. キーボードの入力値を受け取る
  6. オブジェクトの取得とコンポーネントの取得
  7. 衝突時の処理
  8. Webページを開く
  9. コンポーネントの取得
  10. 処理を一定時間待つ
  11. コンソールへのログ出力方法
  12. 飛行機の加速と減速
  13. ジェットエンジンのエフェクトとオーディオ