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