Skip to content

Commit 6ec9744

Browse files
committed
added tick update systems and some advanced examples
1 parent fb8204e commit 6ec9744

29 files changed

+661
-346
lines changed

Assets/UnityEvents/Examples/Advance.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using UnityEngine;
2+
3+
namespace UnityEvents.Example
4+
{
5+
/// <summary>
6+
/// Example of using an event system that isn't controlled by any of the update ticks. Events will only be processed
7+
/// when told to be processed.
8+
/// </summary>
9+
public class ExampleControlledEventSystem
10+
{
11+
// This system will allow subscribers and events to be queued. Events have to be told when to process isntead
12+
// of happening automatically in a tick. The example will only show regular events but this can handle both.
13+
// See ExampleSimpleJob.cs for a job example.
14+
private UnityEventSystem _system = new UnityEventSystem();
15+
16+
// I have to be an unmanaged type! Need references? Use an id and have a lookup database system.
17+
private struct EvExampleEvent
18+
{
19+
public int exampleValue;
20+
21+
public EvExampleEvent(int exampleValue)
22+
{
23+
this.exampleValue = exampleValue;
24+
}
25+
}
26+
27+
public void UseCustomSystem()
28+
{
29+
// Event systems are associated with an EventEntity, this is how an event system can know who to send
30+
// and event to but keep all events together for performance.
31+
//
32+
// For example the global sim event system is its own EventEntity. As is the Global UI system and each
33+
// GameObject is converted to an EventEntity.
34+
//
35+
// It is better to use a single UnityEventSystem with multiple entities than an UnityEventSystem for each
36+
// entity. For example if there are multiple 'global' systems that all get processed at the same time then
37+
// it is more performant to have a since UnityEventSystem and an EventEntity for each 'global system' that
38+
// talk to the same UnityEventSystem.
39+
//
40+
// GlobalEventSystem, GameObject systems, and TickEventSystem uses EventManager which uses one UnityEventSystem
41+
// for each update tick.
42+
EventEntity entity1 = EventEntity.CreateEntity();
43+
EventEntity entity2 = EventEntity.CreateEntity();
44+
45+
// Can subscribe a listener to both entities
46+
_system.Subscribe<EvExampleEvent>(entity1, OnExampleEvent);
47+
_system.Subscribe<EvExampleEvent>(entity2, OnExampleEventDoublePrint);
48+
49+
// We queue up events, avoid the send verb here since we have to manually process the events.
50+
_system.QueueEvent(entity1, new EvExampleEvent(1));
51+
52+
// Now we process the queued events. We only sent an event to the entity1 system, the listener to entity2
53+
// will NOT invoke.
54+
_system.ProcessEvents();
55+
56+
// Each listener needs to unsubscribe from the appropriate event entity
57+
_system.Unsubscribe<EvExampleEvent>(entity1, OnExampleEvent);
58+
_system.Unsubscribe<EvExampleEvent>(entity2, OnExampleEventDoublePrint);
59+
60+
// To have a global system that you control just store an EventEntity and just use that.
61+
}
62+
63+
private void OnExampleEvent(EvExampleEvent ev)
64+
{
65+
Debug.Log("Event received! Value: " + ev.exampleValue);
66+
}
67+
68+
private void OnExampleEventDoublePrint(EvExampleEvent ev)
69+
{
70+
Debug.Log("Event received! Value: " + ev.exampleValue);
71+
Debug.Log("Event received! Value: " + ev.exampleValue);
72+
}
73+
}
74+
}

Assets/UnityEvents/Scripts/GlobalUIEventSystem.cs.meta Assets/UnityEvents/Examples/Advance/ExampleControlledEventSystem.cs.meta

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using UnityEngine;
2+
3+
namespace UnityEvents.Example
4+
{
5+
/// <summary>
6+
/// Example of using a custom event system that send events on an update tick. This could be used to create custom
7+
/// local event system and custom global event system. GlobalEventSystem is a static class that holds a tick
8+
/// based event system for simulation and ui and passes subscriptions/events through it.
9+
/// </summary>
10+
public class ExampleCustomTickEventSystem
11+
{
12+
// This system will process events in the Update tick. The example will only show regular events but this can
13+
// handle both. See ExampleSimpleJob.cs for a job example.
14+
private TickEventSystem _system = new TickEventSystem(EventUpdateTick.Update);
15+
16+
// I have to be an unmanaged type! Need references? Use an id and have a lookup database system.
17+
private struct EvExampleEvent
18+
{
19+
public int exampleValue;
20+
21+
public EvExampleEvent(int exampleValue)
22+
{
23+
this.exampleValue = exampleValue;
24+
}
25+
}
26+
27+
public void UseCustomSystem()
28+
{
29+
// Subscribe to the custom event system
30+
_system.Subscribe<EvExampleEvent>(OnExampleEvent);
31+
32+
// Can send an event to it. This will be processed in the Update loop.
33+
_system.SendEvent(new EvExampleEvent(10));
34+
35+
// And unsubscribe from it
36+
_system.Unsubscribe<EvExampleEvent>(OnExampleEvent);
37+
}
38+
39+
private void OnExampleEvent(EvExampleEvent ev)
40+
{
41+
Debug.Log("Event received! Value: " + ev.exampleValue);
42+
}
43+
}
44+
}

Assets/UnityEvents/Examples/Advance/ExampleCustomTickEventSystem.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using UnityEngine;
2+
using UnityEventsInternal;
3+
4+
namespace UnityEvents.Example
5+
{
6+
/// <summary>
7+
/// Example of using event handlers directly. Similar to UnityEventSystem but only works for a specific event.
8+
/// Can be used to only allow a single event.
9+
/// </summary>
10+
public class ExampleHandlers
11+
{
12+
private EventHandlerStandard<EvExampleEvent> _standardHandler = new EventHandlerStandard<EvExampleEvent>();
13+
private EventHandlerJob<ExampleJob, EvExampleEvent> _jobHandler = new EventHandlerJob<ExampleJob, EvExampleEvent>();
14+
15+
// I have to be an unmanaged type! Need references? Use an id and have a lookup database system.
16+
private struct EvExampleEvent
17+
{
18+
public int exampleValue;
19+
20+
public EvExampleEvent(int exampleValue)
21+
{
22+
this.exampleValue = exampleValue;
23+
}
24+
}
25+
26+
private struct ExampleJob : IJobForEvent<EvExampleEvent>
27+
{
28+
// This result is stored across jobs, wipe it out at the beginning of each job if this isn't wanted!
29+
public int result;
30+
31+
public void ExecuteEvent(EvExampleEvent ev)
32+
{
33+
result += ev.exampleValue;
34+
}
35+
}
36+
37+
public void StandardHandlerExample()
38+
{
39+
// Handlers ultimately are what store the subscriptions and process events. Ideally they shouldn't need
40+
// to be used directly and TickEventSystem and UnityEventSystem would be sufficient in most cases.
41+
42+
// See ExampleControlledEventSystem.cs for a description on EventEntities.
43+
EventEntity entity = EventEntity.CreateEntity();
44+
45+
// Subscribe, Unsubscribe, and Events will seem familiar but can ONLY use EvExampleEvent. Doesn't work for
46+
// all events.
47+
_standardHandler.Subscribe(entity, OnExampleEvent);
48+
49+
// Handlers have to be told to process events so we queue an event and process it later.
50+
_standardHandler.QueueEvent(entity, new EvExampleEvent(7777));
51+
_standardHandler.ProcessEvents();
52+
53+
_standardHandler.Unsubscribe(entity, OnExampleEvent);
54+
55+
// There's a job handler as well, they are separate and won't fire on the same events
56+
_jobHandler.Subscribe(entity, new ExampleJob(), OnJobFinished);
57+
_jobHandler.QueueEvent(entity, new EvExampleEvent(111));
58+
_jobHandler.ProcessEvents();
59+
_jobHandler.Unsubscribe(entity, OnJobFinished);
60+
61+
}
62+
63+
private void OnExampleEvent(EvExampleEvent ev)
64+
{
65+
Debug.Log("Event received! Value: " + ev.exampleValue);
66+
}
67+
68+
private void OnJobFinished(ExampleJob ev)
69+
{
70+
Debug.Log("Job finished! Value: " + ev.result);
71+
}
72+
}
73+
}

Assets/UnityEvents/Examples/Advance/ExampleHandlers.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/UnityEvents/Examples/Simple.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/UnityEvents/Examples/ExampleSimple.cs Assets/UnityEvents/Examples/Simple/ExampleSimple.cs

+14-14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ namespace UnityEvents.Example
88
/// </summary>
99
public class ExampleSimple : MonoBehaviour
1010
{
11+
// I have to be an unmanaged type! Need references? Use an id and have a lookup database system.
12+
private struct EvExampleEvent
13+
{
14+
public int exampleValue;
15+
16+
public EvExampleEvent(int exampleValue)
17+
{
18+
this.exampleValue = exampleValue;
19+
}
20+
}
21+
1122
private void OnEnable()
1223
{
1324
// Subscribes to the global event system, handles events in FixedUpdate
@@ -18,7 +29,7 @@ private void OnEnable()
1829

1930
// Is the game paused but still need events for UI? There's a global UI system. Handles events in
2031
// LateUpdate
21-
GlobalUIEventSystem.Subscribe<EvExampleEvent>(OnExampleEvent);
32+
GlobalEventSystem.SubscribeUI<EvExampleEvent>(OnExampleEvent);
2233

2334
// There's also local event system for each GameObject that run in LateUpdate.
2435
gameObject.SubscribeUI<EvExampleEvent>(OnExampleEvent);
@@ -32,7 +43,7 @@ private void OnDisable()
3243
GlobalEventSystem.Unsubscribe<EvExampleEvent>(OnExampleEvent);
3344
gameObject.Unsubscribe<EvExampleEvent>(OnExampleEvent);
3445

35-
GlobalUIEventSystem.Unsubscribe<EvExampleEvent>(OnExampleEvent);
46+
GlobalEventSystem.UnsubscribeUI<EvExampleEvent>(OnExampleEvent);
3647
gameObject.UnsubscribeUI<EvExampleEvent>(OnExampleEvent);
3748
}
3849

@@ -47,7 +58,7 @@ public void SendEvents()
4758

4859
// Can send events to the global UI event system. These will be processed in LateUpdate which allows the
4960
// game to paused.
50-
GlobalUIEventSystem.SendEvent(new EvExampleEvent(-1));
61+
GlobalEventSystem.SendEventUI(new EvExampleEvent(-1));
5162

5263
// Similarly can send to a specific GameObject to be processed in LateUpdate
5364
gameObject.SendEventUI(new EvExampleEvent(999999));
@@ -58,15 +69,4 @@ private void OnExampleEvent(EvExampleEvent ev)
5869
Debug.Log("Event received! Value: " + ev.exampleValue);
5970
}
6071
}
61-
62-
// I have to be an unmanaged type! Need references? Use an id and have a lookup database system.
63-
public struct EvExampleEvent
64-
{
65-
public int exampleValue;
66-
67-
public EvExampleEvent(int exampleValue)
68-
{
69-
this.exampleValue = exampleValue;
70-
}
71-
}
7272
}

Assets/UnityEvents/Examples/ExampleSimpleJob.cs Assets/UnityEvents/Examples/Simple/ExampleSimpleJob.cs

+11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ namespace UnityEvents.Example
88
/// </summary>
99
public class ExampleSimpleJob : MonoBehaviour
1010
{
11+
// I have to be an unmanaged type! Need references? Use an id and have a lookup database system.
12+
private struct EvExampleEvent
13+
{
14+
public int exampleValue;
15+
16+
public EvExampleEvent(int exampleValue)
17+
{
18+
this.exampleValue = exampleValue;
19+
}
20+
}
21+
1122
private struct ExampleJob : IJobForEvent<EvExampleEvent>
1223
{
1324
// This result is stored across jobs, wipe it out at the beginning of each job if this isn't wanted!

Assets/UnityEvents/Scripts/UnityEventJobSystem.cs Assets/UnityEvents/Scripts/EventHandlerJob.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace UnityEvents
1414
/// </summary>
1515
/// <typeparam name="T_Job">The job the system is responsible for.</typeparam>
1616
/// <typeparam name="T_Event">The event the system is responsible for.</typeparam>
17-
public class UnityEventJobSystem<T_Job, T_Event> :
17+
public class EventHandlerJob<T_Job, T_Event> :
1818
IJobEventSystem<T_Event>,
1919
IDisposable
2020
where T_Job : struct, IJobForEvent<T_Event>
@@ -59,7 +59,7 @@ public Subscription(EventEntity entity, T_Job job)
5959
}
6060
}
6161

62-
public UnityEventJobSystem() : this(
62+
public EventHandlerJob() : this(
6363
DEFAULT_SUBSCRIBER_CAPACITY,
6464
DEFAULT_EVENTS_TO_PROCESS_CAPACITY,
6565
DEFAULT_PARALLEL_BATCH_COUNT)
@@ -72,7 +72,7 @@ public UnityEventJobSystem() : this(
7272
/// <param name="subscriberStartingCapacity">The starting capacity of subscriber containers.</param>
7373
/// <param name="queuedEventsStartingCapacity">The starting capacity of queued events containers.</param>
7474
/// <param name="parallelBatchCount">The batch count allowed per thread for parallel processing.</param>
75-
public UnityEventJobSystem(
75+
public EventHandlerJob(
7676
int subscriberStartingCapacity,
7777
int queuedEventsStartingCapacity,
7878
int parallelBatchCount)

Assets/UnityEvents/Scripts/UnityEventStandardSystem.cs Assets/UnityEvents/Scripts/EventHandlerStandard.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace UnityEvents
1212
/// Event system that processes regular events.
1313
/// </summary>
1414
/// <typeparam name="T_Event">The event the system is responsible for.</typeparam>
15-
public class UnityEventStandardSystem<T_Event> : IEventSystem, IDisposable where T_Event : unmanaged
15+
public class EventHandlerStandard<T_Event> : IEventSystem, IDisposable where T_Event : unmanaged
1616
{
1717
private NativeList<QueuedEvent<T_Event>> _queuedEvents;
1818
private NativeList<EventEntity> _subscribers;
@@ -28,7 +28,7 @@ public class UnityEventStandardSystem<T_Event> : IEventSystem, IDisposable where
2828
private const int DEFAULT_SUBSCRIBER_CAPACITY = 100;
2929
private const int DEFAULT_PARALLEL_BATCH_COUNT = 32;
3030

31-
public UnityEventStandardSystem() : this(
31+
public EventHandlerStandard() : this(
3232
DEFAULT_SUBSCRIBER_CAPACITY,
3333
DEFAULT_EVENTS_TO_PROCESS_CAPACITY,
3434
DEFAULT_PARALLEL_BATCH_COUNT)
@@ -41,7 +41,7 @@ public UnityEventStandardSystem() : this(
4141
/// <param name="subscriberStartingCapacity">The starting capacity of subscriber containers.</param>
4242
/// <param name="queuedEventsStartingCapacity">The starting capacity of queued events containers.</param>
4343
/// <param name="parallelBatchCount">The batch count allowed per thread for parallel processing.</param>
44-
public UnityEventStandardSystem(
44+
public EventHandlerStandard(
4545
int subscriberStartingCapacity,
4646
int queuedEventsStartingCapacity,
4747
int parallelBatchCount)

0 commit comments

Comments
 (0)