Class EntityQueryManagedComponentExtensions
Namespace: Unity.Entities
Syntax
public static class EntityQueryManagedComponentExtensions
Methods
GetSingleton<T>(EntityQuery)
Gets the value of a singleton component.
Declaration
public static T GetSingleton<T>(this EntityQuery query)
where T : class, IComponentData
Parameters
Type | Name | Description |
---|---|---|
EntityQuery | query |
Returns
Type | Description |
---|---|
T | A copy of the singleton component. |
Type Parameters
Name | Description |
---|---|
T | The component type. |
Remarks
A singleton component is a component of which only one instance exists in the world
and which has been set with
Exceptions
Type | Condition |
---|---|
InvalidOperationException |
SetSingleton<T>(EntityQuery, T)
Sets the value of a singleton component.
Declaration
public static void SetSingleton<T>(this EntityQuery query, T value)
where T : class, IComponentData
Parameters
Type | Name | Description |
---|---|---|
EntityQuery | query | |
T | value | An instance of type T containing the values to set. |
Type Parameters
Name | Description |
---|---|
T | The component type. |
Remarks
For a component to be a singleton, there can be only one instance of that component in a World. The component must be the only component in its archetype and you cannot use the same type of component as a normal component.
To create a singleton, create an entity with the singleton component as its only component,
and then use SetSingleton()
to assign a value.
For example, if you had a component defined as:
public class Singlet : IComponentData{ public int Value; }
You could create a singleton as follows:
var singletonEntity = entityManager.CreateEntity(typeof(Singlet));
var singletonGroup = entityManager.CreateEntityQuery(typeof(Singlet));
singletonGroup.SetSingleton<Singlet>(new Singlet {Value = 1});
You can set and get the singleton value from a EntityQuery or a ComponentSystem.
Exceptions
Type | Condition |
---|---|
InvalidOperationException | Thrown if more than one instance of this component type exists in the world or the component type appears in more than one archetype. |