# Attention System

{% file src="<https://1613353771-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fygjewtla1W7kzTTIpGWq%2Fuploads%2FU9poP5M6LjRXnjJTM042%2FAttention%20System.unitypackage?alt=media&token=42406ba1-64e2-4923-8060-bff244112d4a>" %}
Attention system unitypackage
{% endfile %}

## The idea

*<mark style="color:red;">**Attention mark**</mark>* is first enter point, triggers by ***system runner*** on scene start. Attention mark has reference to ***attention target*** (panel or smth else) and if panel needs attention -> attention sign will appear. Attention target will send events on need attention change.&#x20;

<figure><img src="https://1613353771-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fygjewtla1W7kzTTIpGWq%2Fuploads%2FOOUNebYjPPwIgNuUSg2a%2Fimage.png?alt=media&#x26;token=c0042436-f8f2-48e7-9e40-86c29f43a960" alt=""><figcaption><p>Idea scheme</p></figcaption></figure>

So top Attention mark triggers by system runner once, then mark inits attention target. Attention target implementation checks if it need attention and by event it tells mark to enable or disable. We can make deep level of mark and target chain.

## How to implement

We need make implementation of IAttentionTarget that will send events to attention mark. \
Attention mark is basicaly IAttentionObject with SetAttention(bool) method and reference to target).\
\
For example, I have shop canvas where I can buy items. Shop canvas has UI script and I want to make attention to shop button so player will pay attation to button if he can buy something. ShopCanvas.cs is UICanvasController, lets implement IAttentionTarget interface in it.

```csharp
public class ShopCanvas : UICanvasController, IAttentionTarget
```

Now we need implementation

```csharp
#region Attention System Implementation
    public event Action<bool> OnAttentionNeedChange;
    private bool _wasAttention;

    public void InitAttentionTarget() {
        TryInit();

        CashManager.OnCurrencyUpdated.AddListener(OnCurrencyUpdated);
        CheckForAttention();
    }

    private void OnCurrencyUpdated(float newValue, 
                                   float delta, 
                                   Currencies currency) {
        CheckForAttention();
    }

    private void CheckForAttention() {
        bool needAttention = false;
        foreach(var button in _buyButtons) {
            bool canBuy = _cashManager.IsEnough(button.Cost);
            button.SetAttention(canBuy);
            if (canBuy) {
                needAttention = true;
            }
        }

        if (_wasAttention != needAttention) {
            OnAttentionNeedChange.Invoke(needAttention);
            _wasAttention = needAttention;
        }
    }
#endregion
```

Where:

```csharp
public void InitAttentionTarget()
```

Will be called by Attention System Runner.\
\* TryInit() method initialize ShopCanvas so I can access products and buy buttons to get information need I attention or not.\
\* I subscribe to CashManager currency update event to check for attention. Every buy button in ShopCanvas has it's own cost data (price) so I can check if player has enough currency to buy one.\
\* ShopCanvas has it's own Attention signs for each buy button and they refreshed too.\
\* On attention change I call *<mark style="color:red;">**OnAttentionNeedChange**</mark>* event that will update AttentionMark that is connected to this panel.

Now, when IAttentionTarget implemented we can add AttentionSign (prefab) to Shop button:

<figure><img src="https://1613353771-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fygjewtla1W7kzTTIpGWq%2Fuploads%2F8y6eFTxKKE9bOYFMhMGg%2Fimage.png?alt=media&#x26;token=f09ff408-56af-4b09-aa69-7ad84f7baadb" alt=""><figcaption><p>that red square is attention sign</p></figcaption></figure>

<figure><img src="https://1613353771-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fygjewtla1W7kzTTIpGWq%2Fuploads%2FENXol1hJ9H7r9jk15PWD%2Fimage.png?alt=media&#x26;token=73769aee-72e6-4269-9008-6de51d394c16" alt=""><figcaption><p>how attention sign looks in the inspector</p></figcaption></figure>

Attention Sign has AttentionMark.cs on it, where you need to link AttentionTargetGO (ShopCanvas in example, that implements IAttentionTarget interface).

Now you need run system some where in you scene. It can be in SceneSettuper or LevelManager. I wrote AttentionSystem.cs for example:

{% code lineNumbers="true" %}

```csharp
public class AttentionSystem : MonoBehaviour {
    [SerializeField] private AttentionSystemRunner _attentionSystemRunner;

    private void Start() {
        _attentionSystemRunner.Run();
    }

}
```

{% endcode %}

\_attentionSystemRunner.Run() will start attention system.\
\
Now about system setup, you need to reference to top attention mark objects. In our example it's AttentionSign that is linked to ShopCanvas:

<figure><img src="https://1613353771-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fygjewtla1W7kzTTIpGWq%2Fuploads%2Foe3FIXvMJtlzZvEO63H6%2Fimage.png?alt=media&#x26;token=e490eec0-7255-4428-ac1e-31b053bb19d4" alt=""><figcaption><p>Referenced top attention object in the inspector</p></figcaption></figure>

Full example you can find in unitypackage.
