UnityEssentials
Small but useful tools and features for Unity
DoOnce.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 DoOnce
11  {
15  [Tooltip("The events called at the invoke")]
16  [SerializeField] public UnityEvent calledEvent;
20  [NonSerialized] public bool eventInvoked = false;
21 
22  public DoOnce(UnityAction unityAction)
23  {
24  UnityEvent unityEvent = new UnityEvent();
25  unityEvent.AddListener(unityAction);
26  this.calledEvent = unityEvent;
27  }
28 
29  public DoOnce(UnityEvent calledEvent)
30  {
31  this.calledEvent = calledEvent;
32  }
33 
37  public void Invoke()
38  {
39  if (!eventInvoked)
40  {
41  calledEvent?.Invoke();
42  eventInvoked = true;
43  }
44  }
45 
46  }
47 
48 }
Limits the execution of an event so it can only be executed one time.
Definition: DoOnce.cs:11
void Invoke()
Invokes all registered events callbacks (runtime and persistent).
Definition: DoOnce.cs:37
bool eventInvoked
Weather or not the even has been executed already.
Definition: DoOnce.cs:20
UnityEvent calledEvent
The events called at the invoke.
Definition: DoOnce.cs:16
DoOnce(UnityEvent calledEvent)
Definition: DoOnce.cs:29
DoOnce(UnityAction unityAction)
Definition: DoOnce.cs:22