πŸ›‘Attention System

System to implement attention across multiple objects

Attention system unitypackage

The idea

Attention 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.

Idea scheme

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.

public class ShopCanvas : UICanvasController, IAttentionTarget

Now we need implementation

#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:

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 OnAttentionNeedChange event that will update AttentionMark that is connected to this panel.

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

that red square is attention sign
how attention sign looks in the inspector

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:

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

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

}

_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:

Referenced top attention object in the inspector

Full example you can find in unitypackage.

Last updated