UnityEssentials
Small but useful tools and features for Unity
All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties
Sequence.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using UnityEngine.Events;
4 
5 namespace UnityEngine
6 {
10  [Serializable]
11  public class Sequence
12  {
16  [Tooltip("Should the order of execution of the events be random? If false, the order will be the same as in the events array.")]
17  [SerializeField] public bool randomizeOrder;
21  [NonSerialized] private RandomEssentials random = new RandomEssentials();
25  [Tooltip("Each of the events to execute at every call of the Invoke method.")]
26  [SerializeField] public UnityEvent[] events;
30  [NonSerialized] private UnityEvent nextEvent = null;
34  [NonSerialized] private int nextEventIndex = 0;
35 
36  public Sequence(UnityAction[] actions, bool randomizeOrder = false, int randomizationSeed = -1)
37  {
38  List<UnityEvent> events = new List<UnityEvent>();
39 
40  foreach (UnityAction unityAction in actions)
41  {
42  UnityEvent unityEvent = new UnityEvent();
43  unityEvent.AddListener(unityAction);
44  events.Add(unityEvent);
45  }
46 
47  this.events = events.ToArray();
48  this.randomizeOrder = randomizeOrder;
49 
50  if (randomizationSeed == -1)
51  random = new RandomEssentials();
52  else
53  random = new RandomEssentials(randomizationSeed);
54  }
55 
56  public Sequence(UnityEvent[] events, bool randomizeOrder = false, int randomizationSeed = -1)
57  {
58  this.events = events;
59  this.randomizeOrder = randomizeOrder;
60 
61  if (randomizationSeed == -1)
62  random = new RandomEssentials();
63  else
64  random = new RandomEssentials(randomizationSeed);
65  }
66 
67  public void Invoke()
68  {
69  if (randomizeOrder && random == null)
70  random = new RandomEssentials();
71 
72  if (nextEvent == null)
73  {
74  if (!randomizeOrder)
75  {
76  nextEvent = events[0];
77  }
78  else
79  {
82  }
83  }
84 
85  nextEvent?.Invoke();
86 
87  if (!randomizeOrder)
88  nextEventIndex = nextEventIndex.GetLooped(events.Length);
89  else
91 
93  }
94 
95  }
96 
97 }
Allows you to execute different events every time the Invoke method of this is class called.
Definition: Sequence.cs:12
UnityEvent[] events
Each of the events to execute at every call of the Invoke method.
Definition: Sequence.cs:26
int nextEventIndex
The index of the next UnityEvent to be executed.
Definition: Sequence.cs:34
RandomEssentials random
The randomness generation object to choose the next event to execute.
Definition: Sequence.cs:21
bool randomizeOrder
Should the order of execution of the events be random? If false, the order will be the same as in the...
Definition: Sequence.cs:17
Sequence(UnityEvent[] events, bool randomizeOrder=false, int randomizationSeed=-1)
Definition: Sequence.cs:56
UnityEvent nextEvent
The next UnityEvent to be executed. If null, firstEvent is going to be executed next .
Definition: Sequence.cs:30
Sequence(UnityAction[] actions, bool randomizeOrder=false, int randomizationSeed=-1)
Definition: Sequence.cs:36
An easier to use and a feature-rich class to generate pseudo-random results.