UnityEssentials
Small but useful tools and features for Unity
FlipFlop.cs
Go to the documentation of this file.
1 using System;
2 using UnityEngine.Events;
3 
4 namespace UnityEngine
5 {
9  [Serializable]
10  public class FlipFlop
11  {
15  [Tooltip("A set of events to execute together")]
16  [SerializeField] public UnityEvent firstEvent;
20  [Tooltip("A set of events to execute together")]
21  [SerializeField] public UnityEvent secondEvent;
25  [NonSerialized] private UnityEvent nextEvent = null;
26 
27  public FlipFlop(UnityAction firstAction, UnityAction secondAction)
28  {
29  UnityEvent firstEventConstructor = new UnityEvent();
30  firstEventConstructor.AddListener(firstAction);
31 
32  UnityEvent secondEventConstructor = new UnityEvent();
33  secondEventConstructor.AddListener(secondAction);
34 
35  this.firstEvent = firstEventConstructor;
36  this.secondEvent = secondEventConstructor;
37  }
38 
39  public FlipFlop(UnityEvent firstEvent, UnityEvent secondEvent)
40  {
41  this.firstEvent = firstEvent;
42  this.secondEvent = secondEvent;
43  }
44 
48  public void Invoke()
49  {
51 
52  nextEvent?.Invoke();
53 
54  if (nextEvent == firstEvent)
56  else
58  }
59 
60  }
61 
62 }
Allows you to alternate between the execution of two events with each call of the Invoke method.
Definition: FlipFlop.cs:11
void Invoke()
Invokes the firstEvent or the secondEvent alternating.
Definition: FlipFlop.cs:48
FlipFlop(UnityAction firstAction, UnityAction secondAction)
Definition: FlipFlop.cs:27
UnityEvent firstEvent
A set of events to execute together.
Definition: FlipFlop.cs:16
UnityEvent secondEvent
A set of events to execute together.
Definition: FlipFlop.cs:21
UnityEvent nextEvent
The next UnityEvent to be executed. If null, firstEvent is going to be executed next .
Definition: FlipFlop.cs:25
FlipFlop(UnityEvent firstEvent, UnityEvent secondEvent)
Definition: FlipFlop.cs:39