This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
/
CleanTemporaryComponentsSystem.cs
58 lines (51 loc) · 1.95 KB
/
CleanTemporaryComponentsSystem.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
using System.Collections.Generic;
using Unity.Collections;
using Unity.Entities;
using UnityEngine.Profiling;
namespace Improbable.Gdk.Core
{
/// <summary>
/// Removes components with attribute RemoveAtEndOfTick from all entities
/// </summary>
[DisableAutoCreation]
[UpdateInGroup(typeof(InternalSpatialOSCleanGroup))]
public class CleanTemporaryComponentsSystem : ComponentSystem
{
private readonly List<(EntityQuery, ComponentType)> componentGroupsToRemove =
new List<(EntityQuery, ComponentType)>();
protected override void OnCreate()
{
base.OnCreate();
// Find all components with the RemoveAtEndOfTick attribute
foreach (var type in ReflectionUtility.GetNonAbstractTypes(typeof(IComponentData),
typeof(RemoveAtEndOfTickAttribute)))
{
componentGroupsToRemove.Add((GetEntityQuery(ComponentType.ReadOnly(type)), type));
}
foreach (var type in ReflectionUtility.GetNonAbstractTypes(typeof(ISharedComponentData),
typeof(RemoveAtEndOfTickAttribute)))
{
componentGroupsToRemove.Add((GetEntityQuery(ComponentType.ReadOnly(type)), type));
}
}
protected override void OnUpdate()
{
Profiler.BeginSample("RemoveRemoveAtEndOfTick");
foreach (var (componentGroup, componentType) in componentGroupsToRemove)
{
if (componentGroup.IsEmptyIgnoreFilter)
{
continue;
}
using (var entityArray = componentGroup.ToEntityArray(Allocator.TempJob))
{
foreach (var entity in entityArray)
{
PostUpdateCommands.RemoveComponent(entity, componentType);
}
}
}
Profiler.EndSample();
}
}
}