管理中心
我的
API参考游戏性能调优花瓣游戏数据驱动(PGD)C#EntityEntityFastAccess

EntityFastAccess

展开

Class/Struct Info

public readonly ref struct EntityFastAccess

EntityFastAccess定义为只读引用结构体,保证了高性能的组件访问优化,同时确保内存安全。

Property Summary

展开

Name

Type

Description

Id

int

只读属性,实体标识。

Tags

ITags

实体具有的标签集合。

IsDeleted

bool

数据删除标识:

  • true:表示实体数据已被删除。
  • false:表示实体数据未被删除。

Archetype

Archetype

实体所属原型。

Sample Code
var world = new IECSWorld();
var entity = world.CreateEntity(new PgdPosition());
var fastAccess = entity.EntityFastAccess;
// EntityFastAccess的属性与其实体相同
fastAccess.Id; // 等于entity.Id
fastAccess.Tags; // 等于entity.Tags
fastAccess.IsDeleted; // 等于entity.IsDeleted()
fastAccess.Archetype; // 等于entity.Archetype

Method Summary

展开

Qualifier and Type

Method Name and Description

bool

Has<T>()

判断实体是否拥有指定泛型组件,最多支持1个泛型。

ref T

Get<T>()

获取指定泛型组件,最多支持1个泛型。

bool

TryGet<T>(out T value)

获取指定泛型组件,有安全性检验,最多支持1个泛型。

string

ToString()

将实体标识以字符串形式返回。

Methods

Has<T>

展开

Method

bool Has<T>()

判断实体是否拥有指定泛型组件,最多支持1个泛型。

T为IComponent。

Return

展开

Type

Description

bool

  • true:表示实体拥有指定泛型组件。
  • false:表示实体没有指定泛型组件。

Sample Code

var world = new IECSWorld();
var entity = world.CreateEntity(new PgdPosition());
var fastAccess = entity.EntityFastAccess;
// 判断实体是否拥有指定组件,仅支持一个组件
fastAccess.Has<PgdPosition>(); // true
fastAccess.Has<PgdRotation>(); // false

Get<T>

展开

Method

ref T Get<T>()

获取指定泛型组件,最多支持1个泛型。

T为IComponent。

Return

展开

Type

Description

ref T

返回指定泛型组件的引用。

Sample Code

var world = new IECSWorld();
var entity = world.CreateEntity(new PgdPosition()); // 此时PgdPosition组件值为(0,0,0)
var fastAccess = entity.EntityFastAccess;
// 获取组件引用,可直接修改组件值
fastAccess.Get<PgdPosition>().x = 1; // 此时PgdPosition组件值为(1,0,0)

TryGet<T>

展开

Method

bool TryGet<T>(out T value)

获取指定泛型组件,有安全性检验,最多支持1个泛型。

T为IComponent。

Parameters

展开

Name

Type

Description

value

T

接收返回的组件。

Return

展开

Type

Description

bool

  • true:表示成功获取指定组件。
  • false:表示实体没有该组件。

Sample Code

var world = new IECSWorld();
var entity = world.CreateEntity(new PgdPosition());
var fastAccess = entity.EntityFastAccess;
// 安全获取组件值,成功获取返回true,没有对应组件则返回false
if(fastAccess.TryGet<PgdPosition>(out var pos))
{
    /*获取成功处理*/
}
else
{
    /*没有组件处理*/
}

ToString

展开

Method

string ToString()

将实体标识以字符串形式返回。

Return

展开

Type

Description

string

实体标识。

Sample Code

var world = new IECSWorld();
var entity = world.CreateEntity(1);
var fastAccess = entity.EntityFastAccess;
// 打印实体Id
fastAccess.ToString(); // "EntityId: 1"