forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVerifyTriggerEvents.cs
More file actions
122 lines (101 loc) · 4.05 KB
/
Copy pathVerifyTriggerEvents.cs
File metadata and controls
122 lines (101 loc) · 4.05 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Physics.Systems;
using UnityEngine;
using UnityEngine.Assertions;
namespace Unity.Physics.Tests
{
public struct VerifyTriggerEventsData : IComponentData
{
public int ExpectedValue;
}
[Serializable]
public class VerifyTriggerEvents : MonoBehaviour, IConvertGameObjectToEntity
{
public int ExpectedValue = 4;
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
{
dstManager.AddComponentData(entity, new VerifyTriggerEventsData() { ExpectedValue = ExpectedValue });
#if HAVOK_PHYSICS_EXISTS
Havok.Physics.HavokConfiguration config = Havok.Physics.HavokConfiguration.Default;
config.EnableSleeping = 0;
dstManager.AddComponentData(entity, config);
#endif
}
}
[UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
[UpdateBefore(typeof(StepPhysicsWorld))]
public class VerifyTriggerEventsSystem : SystemBase
{
EntityQuery m_VerificationGroup;
StepPhysicsWorld m_StepPhysicsWorld;
public NativeReference<int> NumEvents;
protected override void OnCreate()
{
m_StepPhysicsWorld = World.GetOrCreateSystem<StepPhysicsWorld>();
m_VerificationGroup = GetEntityQuery(new EntityQueryDesc
{
All = new ComponentType[] { typeof(VerifyTriggerEventsData) }
});
NumEvents = new NativeReference<int>(Allocator.Persistent);
}
protected override void OnDestroy()
{
NumEvents.Dispose();
}
struct VerifyTriggerEventsJob : ITriggerEventsJob
{
private bool m_Initialized;
public NativeReference<int> NumEvents;
[ReadOnly]
public ComponentDataFromEntity<VerifyTriggerEventsData> VerificationData;
public void Execute(TriggerEvent triggerEvent)
{
if (!m_Initialized)
{
m_Initialized = true;
NumEvents.Value = 0;
}
NumEvents.Value++;
}
}
struct VerifyTriggerEventsPostJob : IJob
{
public NativeReference<int> NumEvents;
public SimulationType SimulationType;
[ReadOnly]
public ComponentDataFromEntity<VerifyTriggerEventsData> VerificationData;
[DeallocateOnJobCompletion]
public NativeArray<Entity> Entities;
public void Execute()
{
Assert.AreEqual(NumEvents.Value, VerificationData[Entities[0]].ExpectedValue);
}
}
protected override void OnUpdate()
{
SimulationCallbacks.Callback testTriggerEventsCallback = (ref ISimulation simulation, ref PhysicsWorld world, JobHandle inDeps) =>
{
return new VerifyTriggerEventsJob
{
NumEvents = NumEvents,
VerificationData = GetComponentDataFromEntity<VerifyTriggerEventsData>(true)
}.Schedule(simulation, ref world, inDeps);
};
SimulationCallbacks.Callback testTriggerEventsPostCallback = (ref ISimulation simulation, ref PhysicsWorld world, JobHandle inDeps) =>
{
return new VerifyTriggerEventsPostJob
{
NumEvents = NumEvents,
SimulationType = simulation.Type,
VerificationData = GetComponentDataFromEntity<VerifyTriggerEventsData>(true),
Entities = m_VerificationGroup.ToEntityArray(Allocator.TempJob)
}.Schedule(inDeps);
};
m_StepPhysicsWorld.EnqueueCallback(SimulationCallbacks.Phase.PostSolveJacobians, testTriggerEventsCallback, Dependency);
m_StepPhysicsWorld.EnqueueCallback(SimulationCallbacks.Phase.PostSolveJacobians, testTriggerEventsPostCallback, Dependency);
}
}
}