# EventComponent

EventComponent can be written in a C# script.

## How to create

1. Click the “+” button in the Project window.
2. Select Logic Toolkit > Scripts > Event Component C# Script from the menu.
3. Enter the script name and confirm with Enter.

## How to write a script

- Create a class that inherits from `LogicToolkit.EventComponent`.
- Apply `System.SerializableAttribute` to the type.
- `protected override void OnActivated()` is called when activated.
  It mainly processes event subscriptions.
- `protected override void OnDeactivated()` is called when it becomes inactive.
  Mainly handles unsubscribing from events.
- Implement `LogicToolkit.IUpdateReceiver` as necessary, and use `void OnUpdate()` to judge the status.
- Call `OnEvent()` when determining that the event has been called.

## Code example

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using LogicToolkit;

    [System.Serializable]
    public class EventExample : EventComponent
    {
        // OnActivated is called when activated.
        protected override void OnActivated()
        {
            // Subscribe to some event and call OnEvent() from your callback.
            Application.focusChanged += OnFocusChanged;
        }

        void OnFocusChanged(bool focus)
        {
            Debug.Log(focus);
            OnEvent();
        }

        // OnDeactivated is called when it is deactivated.
        protected override void OnDeactivated()
        {
            // Unsubscribe from some event
            Application.focusChanged -= OnFocusChanged;
        }
    }

In this example, when the focus of the application is switched, the presence or absence of focus is output to the console and the node is notified that an event has occurred.
When testing on UnityEditor, check by switching the focus by alternately clicking on the Game window and other windows.

A similar script can be created by selecting `Application.focusChanged` when generating the script.