---
title: Inspector で設定可能なカスタムイベント
product: Unity Editor
version: '6000.0'
latest_version: '7000.0'
status: This page documents version 6000.0. The latest version is 7000.0.
canonical: https://docs.unity.com/ja-jp/engine/6000.0/manual/scripting/object-oriented-development/managing-update-order/unity-events
latest_url: https://docs.unity.com/ja-jp/engine/7000.0/manual/scripting/object-oriented-development/managing-update-order/unity-events.md
generated_at: 2026-11-14T04:48:41.058Z
locale: ja-jp
---

# Inspector で設定可能なカスタムイベント

> 独自のカスタムイベントを作成して、Inspector ウィンドウで永続化された (lasting between Edit mode and Play mode) コールバックを設定します。

Unity は、標準の C# [イベントとデリゲート](https://learn.microsoft.com/en-us/dotnet/standard/events/)に代わる Unity 固有の方法として [UnityEvent](/engine/6000.0/script-reference/unityengine/events/unityevent.md) API を提供しています。標準の C# イベントに対する Unity イベントの主な利点は、Unity イベントがシリアライズ可能である、つまり [Inspector ウィンドウ](/engine/6000.0/manual/unity-editor/editor-windows-views-reference/using-the-inspector.md)で設定できることです。

`UnityEvent`は任意の`MonoBehaviour`に追加でき、標準の C# デリゲートのようにランタイムに実行されます。`UnityEvent`が`MonoBehaviour`で宣言されると、Editorウィンドウに表示され、編集時からランタイムの間に存続するコールバックを定義できます。

Unity イベントには、標準の C# デリゲートと同様の制限があります。

* ターゲットオブジェクトへの参照を保持し、ターゲットオブジェクトのガベージコレクションを停止します。
* マネージ （C#） `UnityEngine.Object`をターゲットとし、アンマネージ （C++） の対応するオブジェクトが破棄されている場合、コールバックは実行されません。詳細については、 [オブジェクト](/engine/6000.0/manual/scripting/object-oriented-development/fundamental-unity-types/class-object.md) を参照してください。

## Unity イベントの設定##configure-unity-events

### 前提条件##prerequisites

* `using UnityEngine.Events`を含む MonoBehaviour スクリプトの作成
* `UnityEvent` 型のフィールドを少なくとも 1 つ宣言する

### Inspector ウィンドウでコールバックを設定します。##configure-callbacks-in-the-inspector-window:

1. 宣言した`UnityEvent`フィールドを含むスクリプトコンポーネントを持つゲームオブジェクトを選択します。

2. コールバックのスロットを追加するには、イベント名の下の + ボタンをクリックします。

3. コールバックを受け取る [UnityEngine.オブジェクト](/engine/6000.0/manual/scripting/object-oriented-development/fundamental-unity-types/class-object.md)を選択します。オブジェクト セレクタを使用するか、オブジェクトをフィールドにドラッグアンドドロップできます。

4. イベントが発生したときに呼び出す機能を選択します。ドロップダウンセレクタには、ゲームオブジェクトとそのコンポーネントで使用可能な[適切なメソッド](#static-dynamic-calls)のフィルタリングされたリストが表示されます。

5. 必要に応じて手順 1 \~ 4 を繰り返し、同じイベントにコールバックを追加します。


**Configuring callbacks for events called Trigger Entered and Trigger Exited in the Inspector window:**
![](/api/media?file=/engine/6000.0/media/images/unityevents-inspector.PNG)

### 静的および動的コール##static-and-dynamic-calls

**Inspector** ウィンドウで`UnityEvent`を設定する場合、次の 2 種類の機能呼び出しがサポートされます。

* **静的**呼び出しは、オーサリング時にターゲットとパラメーターの値が Inspector ウィンドウで定義され、完全に事前設定されます。コールバックが発生すると、Inspector で定義されたパラメーター値を使用してターゲット機能が実行されます。これは、ランタイムに変動しない値に適しています。例えば、特定の衝突が発生するたびに、設定した量だけ体力値を減分したい場合などです。静的にバインドされた関数は、機能選択リストの**静的パラメータ**の下に表示されます。
* **動的**呼び出しは、コードから、呼び出される`UnityEvent`の型に一致するパラメータを使用してプログラムで呼び出されます。これはランタイムで変化する値、例えばキャラクターが各攻撃で受けるダメージの変数量を表す`float`に適しています。UI はコールバックをフィルタリングし、`UnityEvent`の種類に有効なシグネチャを持つ動的関数のみを表示します。例えば、`UnityEvent<string>`がある場合、機能セレクターは**動的文字列**ヘッダーの下に`string`パラメーターを受け取るすべての関数を表示します。


**Choosing between static or dynamic functions whose signatures match the event type in the Inspector window:**
![](/api/media?file=/engine/6000.0/media/images/unityevents-dynamic-static.PNG)

## UnityEvent のジェネリック サポート##generic-support-in-unityevent

デフォルトでは、`Monobehaviour`内の`UnityEvent`は`void`機能に動的にバインドされます。ただし、以下の例に示すように、最大 4 つのジェネリック型パラメータを持つ`UnityEvent`を作成できます。

```lang-cs
using UnityEngine;
using UnityEngine.Events;

public class GenericTest :MonoBehaviour
{
    public UnityEvent<int, int, bool, string> myEvent;
    
    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        if (myEvent == null)
        {
            myEvent = new UnityEvent<int, int, bool, string>();
        }
        myEvent.AddListener(Ping);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.anyKeyDown && myEvent != null)
        {
            myEvent.Invoke(5, 6, true, "Hello");
        }
    }

    void Ping(int i, int j, bool print, string text)
    {
        if (print)
        {
            Debug.Log("Ping: " + text + i + j);
        }
    }
}
```

## 追加リソース##additional-resources

* [ドラッグアンドドロップ イベントを使用してドア、トリガーなどを設定する](https://youtu.be/tmmvhxQcbJk)
* [UnityEvent APIリファレンス](/engine/6000.0/script-reference/unityengine/events/unityevent.md)
