跳到主要内容

Unity ECS Samples - HelloCube

· 阅读需 9 分钟
Arce
独立游戏开发者

官方项目地址:Unity-Technologies/EntityComponentSystemSamples (github.com)

Main Thread Sample

MainThreadeffect

IJobEntity Samples

MainThreadeffect

Aspects sample

MainThreadeffect

Prefabs sample

Prefabseffect

创建大量cube prefab在随机位置,并让它们逐渐下落,当位置y小于0时销毁,当所有的cube都被销毁那么再重新创建它们

在Spawner 下挂载脚本 SpawnerAuthoring 创建spawner的entity

// An authoring component is just a normal MonoBehavior that has a Baker<T> class.
// authoring 组件只是具有 Baker 类的普通 MonoBehavior<T>。
public class SpawnerAuthoring : MonoBehaviour
{
public GameObject Prefab;

// In baking, this Baker will run once for every SpawnerAuthoring instance in a subscene.
// 在烘焙中,此 Baker 将为子场景中的每个 SpawnerAuthoring 实例运行一次。
// (Note that nesting an authoring component's Baker class inside the authoring MonoBehaviour class
// is simply an optional matter of style.)
// (请注意,将创作组件的 Baker 类嵌套在创作 MonoBehaviour 类中只是一个可选的样式问题。
// Baker<T> 将 T Mono class bake 成 entity
class Baker : Baker<SpawnerAuthoring>
{
public override void Bake(SpawnerAuthoring authoring)
{
var entity = GetEntity(TransformUsageFlags.None);
AddComponent(entity, new Spawner
{
Prefab = GetEntity(authoring.Prefab, TransformUsageFlags.Dynamic)
});
}

}
}

IJobChunk

MainThreadeffect

IJobChunk代替IJobEntity来处理 cube 的旋转

IJobEntity 和 IJobChunk 的区别:

IJobEntity 为与查询匹配的每个实体调用一次

IJobChunk 为与查询匹配的每个区块调用一次

[BurstCompile]
struct RotationJob : IJobChunk
{
// 比较 IJobEntity, 这里的变量是ComponentTypeHandle<T> ; 而IJobEntity可以在Execute使用ref T
public ComponentTypeHandle<LocalTransform> TransformTypeHandle;
[ReadOnly] public ComponentTypeHandle<RotationSpeed> RotationSpeedTypeHandle;
public float DeltaTime;

// v128 表示 128 位 SIMD 值
public void Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask,
in v128 chunkEnabledMask)
{
// The useEnableMask parameter is true when one or more entities in
// the chunk have components of the query that are disabled.
// If none of the query component types implement IEnableableComponent,
// we can assume that useEnabledMask will always be false.
// However, it's good practice to add this guard check just in case
// someone later changes the query or component types.
// 当区块中的一个或多个实体具有 disable 的查询组件时,useEnableMask 参数为 true。如果查询组件类型均未实现 IEnableComponent,
// 则可以假定 useEnabledMask 将始终为 false。但是,最好添加此保护检查,以防以后有人更改查询或组件类型。
Assert.IsFalse(useEnabledMask);

var transforms = chunk.GetNativeArray(ref TransformTypeHandle);
var rotationSpeeds = chunk.GetNativeArray(ref RotationSpeedTypeHandle);
for (int i = 0, chunkEntityCount = chunk.Count; i < chunkEntityCount; i++)
{
transforms[i] = transforms[i].RotateY(rotationSpeeds[i].RadiansPerSecond * DeltaTime);
}
}
}

Reparenting sample

Reparentingeffect

让子Cube周期性的挂载和卸载在父Cube上,挂载时跟随旋转

EnableableComponents Sample

Enableableeffect

GameObjectSync

MainThreadeffect

CrossQuery

CrossQueryeffect

RandomSpawn

RandomSpawneffect

FirstPersonController

FirstPersonControllereffect

FixedTimestep

FixedTimestepeffect

实例化每个系统更新的一个实体。每个显示的帧更新一次。(取决于帧速率)

实例化每个系统更新的一个实体。更新速率由下面的滑块控制。(与帧速率无关)

CustomTransforms

CustomTransformseffect

StateChange

ClosestTarget

ClosestTargetseffect