Struct Entity
Identifies an entity.
Namespace: Unity.Entities
Syntax
public struct Entity : IEquatable<Entity>, IComparable<Entity>
Remarks
The entity is a fundamental part of the Entity Component System. Everything in your game that has data or an identity of its own is an entity. However, an entity does not contain either data or behavior itself. Instead, the data is stored in the components and the behavior is provided by the systems that process those components. The entity acts as an identifier or key to the data stored in components.
Entities are managed by the EntityManager class and exist within a World. An Entity struct refers to an entity, but is not a reference. Rather the Entity struct contains an Index used to access entity data and a Version used to check whether the Index is still valid. Note that you generally do not use the Index or Version values directly, but instead pass the Entity struct to the relevant API methods.
Pass an Entity struct to methods of the EntityManager, the EntityCommandBuffer, or the ComponentSystem in order to add or remove components, to access components, or to destroy the entity.
Fields
Index
The ID of an entity.
Declaration
public int Index
Field Value
Type | Description |
---|---|
Int32 | The index into the internal list of entities. |
Remarks
Entity indexes are recycled when an entity is destroyed. When an entity is destroyed, the EntityManager increments the version identifier. To represent the same entity, both the Index and the Version fields of the Entity object must match. If the Index is the same, but the Version is different, then the entity has been recycled.
Version
The generational version of the entity.
Declaration
public int Version
Field Value
Type | Description |
---|---|
Int32 | Used to determine whether this Entity object still identifies an existing entity. |
Remarks
The Version number can, theoretically, overflow and wrap around within the lifetime of an application. For this reason, you cannot assume that an Entity instance with a larger Version is a more recent incarnation of the entity than one with a smaller Version (and the same Index).
Properties
Null
A "blank" Entity object that does not refer to an actual entity.
Declaration
public static readonly Entity Null { get; }
Property Value
Type | Description |
---|---|
Entity |
Methods
CompareTo(Entity)
Compare this entity against a given one
Declaration
public int CompareTo(Entity other)
Parameters
Type | Name | Description |
---|---|---|
Entity | other | The other entity to compare to |
Returns
Type | Description |
---|---|
Int32 | Difference based on the Entity Index value |
Equals(Object)
Entity instances are equal if they refer to the same entity.
Declaration
public override bool Equals(object compare)
Parameters
Type | Name | Description |
---|---|---|
Object | compare | The object to compare to this Entity. |
Returns
Type | Description |
---|---|
Boolean | True, if the compare parameter contains an Entity object having the same Index and Version as this Entity. |
Overrides
Equals(Entity)
Entity instances are equal if they represent the same entity.
Declaration
public bool Equals(Entity entity)
Parameters
Type | Name | Description |
---|---|---|
Entity | entity | The other Entity. |
Returns
Type | Description |
---|---|
Boolean | True, if the Entity instances have the same Index and Version. |
GetHashCode()
A hash used for comparisons.
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
Int32 | A unique hash code. |
Overrides
ToString()
Provides a debugging string.
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
String | A string containing the entity index and generational version. |
Overrides
Operators
Equality(Entity, Entity)
Entity instances are equal if they refer to the same entity.
Declaration
public static bool operator ==(Entity lhs, Entity rhs)
Parameters
Type | Name | Description |
---|---|---|
Entity | lhs | An Entity object. |
Entity | rhs | Another Entity object. |
Returns
Type | Description |
---|---|
Boolean | True, if both Index and Version are identical. |
Inequality(Entity, Entity)
Entity instances are equal if they refer to the same entity.
Declaration
public static bool operator !=(Entity lhs, Entity rhs)
Parameters
Type | Name | Description |
---|---|---|
Entity | lhs | An Entity object. |
Entity | rhs | Another Entity object. |
Returns
Type | Description |
---|---|
Boolean | True, if either Index or Version are different. |