forked from yankooliveira/signals
-
Notifications
You must be signed in to change notification settings - Fork 8
/
BasicExampleSignalTest.cs
100 lines (85 loc) · 2.41 KB
/
BasicExampleSignalTest.cs
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ExampleSignalTest.cs" company="Supyrb">
// Copyright (c) Supyrb. All rights reserved.
// </copyright>
// <author>
// Johannes Deml
// </author>
// --------------------------------------------------------------------------------------------------------------------
using System.Collections;
using UnityEngine;
namespace Supyrb
{
public class BasicExampleSignalTest : MonoBehaviour
{
[SerializeField]
private bool logListeners = true;
private BasicExampleSignal basicExampleSignal;
private void Awake()
{
Signals.Get(out basicExampleSignal);
SubscribeListeners();
}
private void Start()
{
DispatchSignal();
}
private void SubscribeListeners()
{
basicExampleSignal.AddListener(FirstListener, -100);
basicExampleSignal.AddListener(PauseTwoSecondsListener, -10);
basicExampleSignal.AddListener(DefaultListener);
basicExampleSignal.AddListener(ConsumeEventListener, 10);
basicExampleSignal.AddListener(LastListener, 100);
}
private void OnDestroy()
{
basicExampleSignal.RemoveListener(FirstListener);
basicExampleSignal.RemoveListener(PauseTwoSecondsListener);
basicExampleSignal.RemoveListener(DefaultListener);
basicExampleSignal.RemoveListener(ConsumeEventListener);
basicExampleSignal.RemoveListener(LastListener);
}
[ContextMenu("DispatchSignal")]
public void DispatchSignal()
{
basicExampleSignal.Dispatch();
}
private void FirstListener()
{
ConditionalLog("First Listener (Order -100)");
}
private void PauseTwoSecondsListener()
{
ConditionalLog("Pausing for 2 seconds (Order -10)");
basicExampleSignal.Pause();
StartCoroutine(ContinueAfterDelay(basicExampleSignal, 2f));
}
private void DefaultListener()
{
ConditionalLog("Default order Listener (Order 0)");
}
private void ConsumeEventListener()
{
ConditionalLog("Consume Signal (Order 10)");
basicExampleSignal.Consume();
}
private void LastListener()
{
ConditionalLog("Won't be called, since the signal was consumed. (Order 100)");
}
private IEnumerator ContinueAfterDelay(Signal signal, float delay)
{
yield return new WaitForSeconds(delay);
signal.Continue();
}
private void ConditionalLog(string text)
{
if (logListeners)
{
Debug.Log(text, this);
}
}
}
}