-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEntityManagerUtility.cs
More file actions
35 lines (33 loc) · 1.43 KB
/
EntityManagerUtility.cs
File metadata and controls
35 lines (33 loc) · 1.43 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
using System.Collections.Generic;
using Unity.Entities;
namespace E7.EcsGadgets
{
public class EntityManagerUtility
{
EntityManager em;
List<int> scdIndices;
public EntityManagerUtility(World world)
{
this.em = world.EntityManager;
scdIndices = new List<int>();
}
/// <summary>
/// Performs a linear search over all existing SCD of a single type to find out the index.
/// The search equality test is by List.IndexOf
/// Remember that the index may become unusable once the SCD order version changed.
/// </summary>
/// <param name="scdValues">In order for this method to not generate garbage, please bring your own
/// managed List of the type you want that you allocate on OnCreate in your system. Just bring it in,
/// this method has a .Clear inside.</param>
/// <returns>-1 when not found. That means this SCD value has not been used anywhere yet or not used anymore.</returns>
public int IndexOfSharedComponentData<T>(T sharedComponentValue, List<T> scdValues)
where T : struct, ISharedComponentData
{
scdValues.Clear();
scdIndices.Clear();
em.GetAllUniqueSharedComponentData(scdValues, scdIndices);
var indexOf = scdValues.IndexOf(sharedComponentValue);
return indexOf == -1 ? -1 : scdIndices[indexOf];
}
}
}