Class/Struct Info |
|---|
public readonly struct IEntity : IEquatable<IEntity> 实体的核心结构体,实体的底层实现,继承了IEquatable<IEntity>。 实体创建必须通过IECSWorld或Archetype的创建实体方法创建,不能用关键字new创建。 |
Enum info |
|---|
public enum WorldOwnershipStatus 实体类枚举值,用于描述实体在World中的存在状态。 |
public enum EntityNodeStatus 实体类枚举值,用于描述实体在树结构中的归属状态。 |
Name | Type | Description |
|---|---|---|
EntityName | Name | 实体名字组件(访问器,需手动添加名字组件后才能访问到)。 |
HasName | bool | 实体名字组件标识:
|
Tags | ITags | 只读属性,所属原型的ITags的引用,表示实体拥有的标签种类。 |
Archetype | Archetype | 实体所属原型。 |
World | IECSWorld | 实体所在World。 |
WorldOwnershipStatus | WorldOwnershipStatus | 实体与World关联状态枚举。 |
EntityNodeStatus | EntityNodeStatus | 实体所在树状态枚举。 |
Active | bool | 实体活跃标志。 |
EntityFastAccess | EntityFastAccess | 实体组件数据快速访问结构。 |
Parent | IEntity | 父实体。 |
Children | ChildEntitySet | 子实体集合。 |
ChildrenCount | int | 子实体数量。 |
ChildrenIds | ReadOnlySpan<int> | 子实体标识的只读内存。 |
Id | int | 只读属性,实体唯一标识。 |
Version | short | 只读属性,实体版本号。 |
Qualifier and Type | Method Name and Description |
|---|---|
bool | 判断实体是否已经删除。 |
bool | 判断实体是否拥有指定组件,最多支持1个泛型。 |
ref T | 获取实体指定组件,最多支持1个泛型。 |
bool | TryGetComponent<T>(out T result) 安全获取实体指定组件,最多支持1个泛型。 |
bool | 添加指定泛型组件,组件值为默认值,最多支持1个泛型。 |
bool | AddComponent<T>(in T component) 添加指定泛型组件,组件值为输入组件值,最多支持1个泛型。 |
bool | 移除指定泛型组件,最多支持1个泛型。 |
bool | 判断实体是否拥有指定泛型标签,最多支持1个泛型。 |
bool | 添加指定泛型标签,最多支持1个泛型。 |
bool | AddTags(in ITags iTags) 添加一组标签。 |
bool | 移除指定泛型标签,最多支持1个泛型。 |
bool | RemoveTags(in ITags iTags) 移除一组标签。 |
void | 激活实体及其所有子实体。 |
void | 禁用实体及其所有子实体。 |
int | AddChild(IEntity childEntity) 添加子实体。 |
void | InsertChild(int index, IEntity childEntity) 插入子实体。 |
bool | RemoveChild(IEntity childEntity) 移除子实体。 |
void | 删除实体。 |
int | GetChildEntityIndex(IEntity childEntity) 获取指定子实体的位置索引。 |
void | CopyEntityTo(IEntity target) 将当前实体的所有组件和标签复制到目标实体上。 |
IEntity | 克隆实体。 |
bool | 不等于操作符重载,判断两个实体是否不相等。 |
bool | 等于操作符重载,判断两个实体是否相等。 |
bool | Equals(IEntity other) 判断当前实体与输入的实体是否相等。 |
bool | Equals(object obj) 判断当前实体与输入的对象是否相等。 |
string | ToString() 将实体的详细信息以字符串形式返回。 |
BatchProcessor | 获取一个批量处理器,并将该批量处理器的目标实体设置为当前实体。 |
Method |
|---|
bool IsDeleted() 判断实体是否已经删除。 |
Return
Type | Description |
|---|---|
bool |
|
Sample Code
var world = new IECSWorld(); var entity = world.CreateEntity(); // 实体未删除时,接口返回false entity.IsDeleted(); // false // 实体删除后,接口返回true entity.DeleteEntity(); entity.IsDeleted(); // true
Method |
|---|
bool HasComponent<T>() 判断实体是否拥有指定组件,最多支持1个泛型。 T为IComponent。 |
Return
Type | Description |
|---|---|
bool |
|
Sample Code
var world = new IECSWorld(); var entity = world.CreateEntity(new PgdPosition()); // 创建拥有PgdPosition组件的实体 // 判断实体是否拥有指定组件,仅支持一个组件 entity.HasComponent<PgdRotation>(); // false entity.HasComponent<PgdPosition>(); // true
Method |
|---|
ref T GetComponent<T>() 获取实体指定组件,最多支持1个泛型。 T为IComponent。 |
Return
Type | Description |
|---|---|
ref T | 返回指定组件。 |
Sample Code
var world = new IECSWorld(); var entity = world.CreateEntity(new PgdPosition()); // 创建拥有PgdPosition组件的实体,此时实体的PgdPosition为x=0, y=0, z=0 // 获取组件引用 entity.GetComponent<PgdPosition>() = new PgdPosition(1,1,1); // 此时实体的PgdPosition为x=1, y=1, z=1 // 也支持直接修改组件内容 entity.GetComponent<PgdPosition>().x = 2; // 此时实体的PgdPosition为x=2, y=1, z=1
Method |
|---|
bool TryGetComponent<T>(out T result) 安全获取实体指定组件,最多支持1个泛型。 T为IComponent。 |
Parameters
Name | Type | Description |
|---|---|---|
result | T | 输出参数,将获取结果通过result返回。 |
Return
Type | Description |
|---|---|
bool |
|
Sample Code
var world = new IECSWorld();
var entity = world.CreateEntity();
// 安全获取组件
if (entity.TryGetComponent(out PgdPosition pos))
{
/* 处理代码 */
}
else
{
/* 处理代码 */
} Method |
|---|
bool AddComponent<T>() 添加指定泛型组件,组件值为默认值,最多支持1个泛型。 T为IComponent。 |
Return
Type | Description |
|---|---|
bool |
|
Sample Code
var world = new IECSWorld(); var entity = world.CreateEntity(); // 添加PgdPosition组件 entity.AddComponent<PgdPosition>(); // 返回true // 重复添加PgdPosition,返回false entity.AddComponent<PgdPosition>(); // 返回false
Method |
|---|
bool AddComponent<T>(in T component) 添加指定泛型组件,组件值为输入组件值,最多支持1个泛型。 T为IComponent。 |
Return
Type | Description |
|---|---|
bool |
|
Sample Code
var world = new IECSWorld(); var entity = world.CreateEntity(); // 添加PgdPosition组件 entity.AddComponent(new PgdPosition(1,1,1)); // 返回true,此时实体的PgdPosition为x=1, y=1, z=1 // 重复添加PgdPosition,值覆盖 entity.AddComponent(new PgdPosition(2,1,1)); // 返回false,此时实体的PgdPosition为x=2, y=1, z=1
Method |
|---|
bool RemoveComponent<T>() 移除指定泛型组件,最多支持1个泛型。 T为IComponent。 |
Return
Type | Description |
|---|---|
bool |
|
Sample Code
var world = new IECSWorld(); var entity = world.CreateEntity(new PgdPosition()); // 移除PgdPosition组件 entity.RemoveComponent<PgdPosition>(); // 返回true // 移除实体没有的组件,返回false entity.RemoveComponent<PgdPosition>(); // 返回false
Method |
|---|
bool HasTag<T>() 判断实体是否拥有指定泛型标签,最多支持1个泛型。 T为ITag。 |
Return
Type | Description |
|---|---|
bool |
|
Sample Code
var world = new IECSWorld(); var entity = world.CreateEntity(ITags.Get<Inactive>()); // 创建拥有Inactive标签的实体 // 判断实体是否拥有指定标签,仅支持一个标签 entity.HasTag<TestTag1>(); // false entity.HasTag<Inactive>(); // true
Method |
|---|
bool AddTag<T>() 添加指定泛型标签,最多支持1个泛型。 T为ITag。 |
Return
Type | Description |
|---|---|
bool |
|
Sample Code
var world = new IECSWorld(); var entity = world.CreateEntity(); // 给实体添加标签,仅支持一个标签 entity.AddTag<Inactive>(); // 此时实体拥有Inactive标签 entity.AddTag<Inactive>(); // 重复添加,无效操作,返回false,此时实体还是拥有Inactive标签
Method |
|---|
bool AddTags(in ITags iTags) 添加一组标签。 |
Parameters
Name | Type | Description |
|---|---|---|
iTags | ITags | 标签类型集合。 |
Return
Type | Description |
|---|---|
bool |
|
Sample Code
var world = new IECSWorld(); var entity = world.CreateEntity(); // 添加一组标签,最多支持一次添加10个标签 entity.AddTags(ITags.Get<Inactive, TestTag1, TestTag2>()); // 此时实体拥有Inactive、TestTag1和TestTag2标签 entity.AddTags(ITags.Get<Inactive, TestTag1>()); // 若实体已经拥有所有添加的标签,返回false,无效操作
Method |
|---|
bool RemoveTag<T>() 移除指定泛型标签,最多支持1个泛型。 T为ITag。 |
Return
Type | Description |
|---|---|
bool |
|
Sample Code
var world = new IECSWorld(); var entity = world.CreateEntity(ITags.Get<Inactive>()); // 给实体移除标签,仅支持一个标签 entity.RemoveTag<Inactive>(); // 此时实体没有标签 entity.RemoveTag<Inactive>(); // 重复移除,无效操作,返回false
Method |
|---|
bool RemoveTags(in ITags iTags) 移除一组标签。 |
Parameters
Name | Type | Description |
|---|---|---|
iTags | ITags | 标签类型集合。 |
Return
Type | Description |
|---|---|
bool |
|
Sample Code
var world = new IECSWorld(); var entity = world.CreateEntity(ITags.Get<Inactive, TestTag1, TestTag2>()); // 此时实体拥有Inactive、TestTag1和TestTag2标签 // 移除一组标签,最多支持一次移除10个标签 entity.RemoveTags(ITags.Get<Inactive, TestTag1, TestTag2>()); // 此时实体没有标签 entity.RemoveTags(ITags.Get<Inactive, TestTag1>()); // 若实体没有所有添加的标签,返回false,无效操作
Method |
|---|
void ActivateTree() 激活实体及其所有子实体。 |
Sample Code
var world = new IECSWorld(); var entity = world.CreateEntity(ITags.Get<Inactive>()); var childEntity = world.CreateEntity(ITags.Get<Inactive>()); entity.AddChild(childEntity); // 添加子实体,此时父子实体均是被禁用的 // 激活实体及其所有子实体 entity.ActivateTree(); // 此时父子实体均已激活,即自动移除Inactive标签
Method |
|---|
void InactivateTree() 禁用实体及其所有子实体。 |
Sample Code
var world = new IECSWorld(); var entity = world.CreateEntity(); var childEntity = world.CreateEntity(); entity.AddChild(childEntity); // 添加子实体,此时父子实体均是激活的 // 禁用实体及其所有子实体 entity.InactivateTree(); // 此时父子实体均已禁用,即自动添加Inactive标签
Method |
|---|
int AddChild(IEntity childEntity) 添加子实体。 |
Parameters
Name | Type | Description |
|---|---|---|
childEntity | IEntity | 子实体。 |
Return
Type | Description |
|---|---|
int | 添加的子实体在子实体列表中的位置。 |
Sample Code
var world = new IECSWorld(); var entity = world.CreateEntity(); var childEntity = world.CreateEntity(); // 添加子实体,新增至实体的子实体Group末尾 entity.AddChild(childEntity);
Method |
|---|
void InsertChild(int index, IEntity childEntity) 插入子实体。 |
Parameters
Name | Type | Description |
|---|---|---|
index | int | 插入位置。 |
childEntity | IEntity | 子实体。 |
Sample Code
var world = new IECSWorld(); var entity = world.CreateEntity(); var childEntity = world.CreateEntity(); // 添加子实体,新增至实体的子实体Group指定位置 entity.InsertChild(1, childEntity); // 在位置1处插入子实体
Method |
|---|
bool RemoveChild(IEntity childEntity) 移除子实体。 |
Parameters
Name | Type | Description |
|---|---|---|
childEntity | IEntity | 子实体。 |
Return
Type | Description |
|---|---|
bool |
|
Sample Code
var world = new IECSWorld(); var entity = world.CreateEntity(); var childEntity = world.CreateEntity(); entity.AddChild(childEntity); entity.RemoveChild(childEntity); // 移除子实体 entity.RemoveChild(childEntity); // 移除实体没有的子实体,无效操作,返回false
Method |
|---|
void DeleteEntity() 删除实体。 |
Sample Code
var world = new IECSWorld(); var entity = world.CreateEntity(); // 删除实体 entity.DeleteEntity();
Method |
|---|
int GetChildEntityIndex(IEntity childEntity) 获取指定子实体的位置索引。 |
Parameters
Name | Type | Description |
|---|---|---|
childEntity | IEntity | 子实体。 |
Return
Type | Description |
|---|---|
int | 子实体的位置索引。 |
Sample Code
var world = new IECSWorld(); var entity = world.CreateEntity(); var childEntity = world.CreateEntity(); entity.AddChild(childEntity); // 获取子实体位置 entity.GetChildEntityIndex(childEntity); // 0 entity.GetChildEntityIndex(childEntity2); // 实体没有的子实体,返回-1
Method |
|---|
void CopyEntityTo(IEntity target) 将当前实体的所有组件和标签复制到目标实体上。 |
Parameters
Name | Type | Description |
|---|---|---|
target | IEntity | 目标实体。 |
Sample Code
var world = new IECSWorld(); var entity = world.CreateEntity(new PgdPosition(), ITags.Get<Inactive>()); var target = world.CreateEntity(); // 复制所有组件和标签到目标实体上,若源实体有子实体,子实体不会被复制到目标实体上 entity.CopyEntityTo(target); // 此时target拥有PgdPosition组件和Inactive标签
Method |
|---|
IEntity CloneEntity() 克隆当前实体。 |
Return
Type | Description |
|---|---|
IEntity | 返回克隆创建的实体。 |
Sample Code
var world = new IECSWorld(); var entity = world.CreateEntity(new PgdPosition(), ITags.Get<Inactive>()); // 克隆实体,若源实体有子实体,子实体不会被复制到目标实体上 var clone = entity.CloneEntity(); // 此时clone拥有PgdPosition组件和Inactive标签
Method |
|---|
bool operator !=(IEntity a, IEntity b) 不等于操作符重载,判断两个实体是否不相等。 |
Parameters
Name | Type | Description |
|---|---|---|
a | IEntity | 实体a。 |
b | IEntity | 实体b。 |
Return
Type | Description |
|---|---|
bool |
|
Sample Code
var world = new IECSWorld(); var entity = world.CreateEntity(); var entity2 = world.CreateEntity(); // 比较两个实体是否不相等 entity != entity2; // true
Method |
|---|
bool operator ==(IEntity a, IEntity b) 等于操作符重载,判断两个实体是否相等。 |
Parameters
Name | Type | Description |
|---|---|---|
a | IEntity | 实体a。 |
b | IEntity | 实体b。 |
Return
Type | Description |
|---|---|
bool |
|
Sample Code
var world = new IECSWorld(); var entity = world.CreateEntity(); var entity2 = world.CreateEntity(); // 比较两个实体是否相等 entity == entity2; // false
Method |
|---|
bool Equals(IEntity other) 判断当前实体与输入的实体是否相等。 |
Parameters
Name | Type | Description |
|---|---|---|
other | IEntity | 实体。 |
Return
Type | Description |
|---|---|
bool |
|
Sample Code
var world = new IECSWorld(); var entity = world.CreateEntity(); var entity2 = world.CreateEntity(); // 比较两个实体是否相等 entity.Equals(entity2); // false
Method |
|---|
bool Equals(object obj) 判断当前实体与输入的对象是否相等。 |
Parameters
Name | Type | Description |
|---|---|---|
obj | object | 对象。 |
Return
Type | Description |
|---|---|
bool |
|
Sample Code
var world = new IECSWorld(); var entity = world.CreateEntity(); var entity2 = world.CreateEntity(); // 比较两个实体是否相等,输入为object类型 entity.Equals((object)entity2); // false
Method |
|---|
string ToString() 将实体的详细信息以字符串形式返回,打印的组件和标签名字均会带命名域。 |
Return
Type | Description |
|---|---|
string | 实体的详细信息。 |
Sample Code
var world = new IECSWorld();
var entity = world.CreateEntity(new Name("test"));
// 打印实体信息。预设组件/标签命名域为"Pgd.",自定义组件/标签/共享组件根据实际情况而定
entity.ToString(); // "EntityId: 1, entityName: test, components: [Pgd.Name]" Method |
|---|
BatchProcessor GetBatched() 获取一个批量处理器,并将该批量处理器的目标实体设置为当前实体。 |
Return
Type | Description |
|---|---|
BatchProcessor | 批量处理器。 |
Sample Code
var world = new IECSWorld();
var entity = world.CreateEntity();
// 用批处理器对实体进行操作,可以通过后置调用Process()方法,实现延后处理
entity.GetBatchProcessor()
.AddComponent(new PgdPosition(1, 2, 3))
.AddComponent(new Name("test"))
.RemoveComponent<PgdScale>()
.AddTag<MyTag1>()
.Process();
entity.ToString(); // "EntityId: 1, entityName: test, components: [Name, PgdPosition], tags: [MyTag1]"