---
title: "イベント"
content_type: tutorial
url: "https://learn.unity.com/tutorial/events-uh-jp?language=ja"
language: ja
difficulty: intermediate
duration_minutes: 5
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/events-uh-jp.md?language=ja&version=2019.3"
  - name: "2019.2"
    url: "https://learn.unity.com/tutorial/events-uh-jp.md?language=ja&version=2019.2"
  - name: "2019.1"
    url: "https://learn.unity.com/tutorial/events-uh-jp.md?language=ja&version=2019.1"
  - name: "2018.4"
    url: "https://learn.unity.com/tutorial/events-uh-jp.md?language=ja&version=2018.4"
  - name: "2018.3"
    url: "https://learn.unity.com/tutorial/events-uh-jp.md?language=ja&version=2018.3"
  - name: "2018.2"
    url: "https://learn.unity.com/tutorial/events-uh-jp.md?language=ja&version=2018.2"
  - name: "2018.1"
    url: "https://learn.unity.com/tutorial/events-uh-jp.md?language=ja&version=2018.1"
  - name: "2017.4"
    url: "https://learn.unity.com/tutorial/events-uh-jp.md?language=ja&version=2017.4"
  - name: "2017.3"
    url: "https://learn.unity.com/tutorial/events-uh-jp.md?language=ja&version=2017.3"
  - name: "2017.2"
    url: "https://learn.unity.com/tutorial/events-uh-jp.md?language=ja&version=2017.2"
  - name: "2017.1"
    url: "https://learn.unity.com/tutorial/events-uh-jp.md?language=ja&version=2017.1"
  - name: "5.x"
    url: "https://learn.unity.com/tutorial/events-uh-jp.md?language=ja&version=5.x"
  - name: "4.x"
    url: "https://learn.unity.com/tutorial/events-uh-jp.md?language=ja&version=4.x"
last_updated: "2026-08-04T14:23:50.000Z"
---

# イベント

イベントを活用してロジックを疎結合に保つ、動的なブロードキャスト型メッセージシステムを構築します。

イベントを使用して動的な「ブロードキャスト」システムを作成する方法を学びます。

## イベント

[Video](http://www.youtube.com/watch?v=k4JlFxPcqlg)

**EventManager クラスのコード**

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

public class EventManager : MonoBehaviour 
{
    public delegate void ClickAction();
    public static event ClickAction OnClicked;


    void OnGUI()
    {
        if(GUI.Button(new Rect(Screen.width / 2 - 50, 5, 100, 30), "Click"))
        {
            if(OnClicked != null)
                OnClicked();
        }
    }
}
```

**TeleportScript クラスのコード**

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

public class TeleportScript : MonoBehaviour 
{
    void OnEnable()
    {
        EventManager.OnClicked += Teleport;
    }


    void OnDisable()
    {
        EventManager.OnClicked -= Teleport;
    }


    void Teleport()
    {
        Vector3 pos = transform.position;
        pos.y = Random.Range(1.0f, 3.0f);
        transform.position = pos;
    }
}
```

**TurnColorScript クラスのコード**

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

public class TurnColorScript : MonoBehaviour 
{
    void OnEnable()
    {
        EventManager.OnClicked += TurnColor;
    }


    void OnDisable()
    {
        EventManager.OnClicked -= TurnColor;
    }


    void TurnColor()
    {
        Color col = new Color(Random.value, Random.value, Random.value);
        renderer.material.color = col;
    }
}
```
