-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathExampleSimpleJob.cs
61 lines (53 loc) · 1.73 KB
/
ExampleSimpleJob.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
using UnityEngine;
using UnityEvents.Internal;
namespace UnityEvents.Example
{
/// <summary>
/// Simple example of using jobs with the event system
/// </summary>
public class ExampleSimpleJob : MonoBehaviour
{
// I have to be an unmanaged type! Need references? Use an id and have a lookup database system.
private struct EvExampleEvent
{
public int exampleValue;
public EvExampleEvent(int exampleValue)
{
this.exampleValue = exampleValue;
}
}
private struct ExampleJob : IJobForEvent<EvExampleEvent>
{
// This result is stored across jobs, wipe it out at the beginning of each job if this isn't wanted!
public int result;
public void ExecuteEvent(EvExampleEvent ev)
{
result += ev.exampleValue;
}
}
private void OnEnable()
{
// Jobs work with the global simulation and global UI event systems as well as the GameObject system. This
// will just show examples with the global simulation system.
//
// When an event is fired jobs will processed in parallel using the burst compiler. Can make otherwise
// long tasks very short. Afterwards the callback functions are invoked so the listener can use the results
// of the job.
GlobalEventSystem.SubscribeWithJob<ExampleJob, EvExampleEvent>(new ExampleJob(), OnJobFinished);
}
private void OnDisable()
{
GlobalEventSystem.UnsubscribeWithJob<ExampleJob, EvExampleEvent>(OnJobFinished);
}
public void SendEvents()
{
// Job listeners trigger on events like anything else. You can have job listeners and regular listeners to
// a single event.
GlobalEventSystem.SendEvent(new EvExampleEvent(10));
}
private void OnJobFinished(ExampleJob ev)
{
Debug.Log("Job finished! Value: " + ev.result);
}
}
}