[BurstCompile]
public partial struct ProcessDestroyBulletJob : IJobEntity
{
public EntityCommandBuffer.ParallelWriter ecb;
private void Execute([ChunkIndexInQuery] int chunkIndex, Entity entity, in BulletComponent bullet, in LocalTransform transform)
{
if (bullet.isDestroyed == true)
{
if (bullet.HitEffect != Entity.Null)
{
var hitEffectEntity = ecb.Instantiate(chunkIndex, bullet.HitEffect);
ecb.SetComponent(chunkIndex, hitEffectEntity, transform);
}
ecb.DestroyEntity(chunkIndex, entity);
}
}
}
간단하게 히트되면 파티클 오브젝트가 생성되도록 짰습니다.
파티클이 어떻게 잘 되지? 하고 봤더니 Managed Component로 되어있더라고요.
내부적으로는 Unity시스템의 Particle System 컴포넌트를 생성해서 Value로 참조하고 있었습니다.
https://docs.unity3d.com/Packages/com.unity.entities@1.0/manual/components-type.html
Component types | Entities | 1.0.0-pre.15
docs.unity3d.com
public partial struct ParticleDestroySystem : ISystem
{
public void OnUpdate(ref SystemState state)
{
var ecbSingleton = SystemAPI.GetSingleton<BeginSimulationEntityCommandBufferSystem.Singleton>();
var ecb = ecbSingleton.CreateCommandBuffer(state.WorldUnmanaged);
foreach ((var particle, var particleDestroy, var entity) in SystemAPI.Query<SystemAPI.ManagedAPI.UnityEngineComponent<UnityEngine.ParticleSystem>, RefRW<ParticleDestroyComponent>>().WithEntityAccess())
{
bool willDestroy = false;
if (willDestroy == false &&
particleDestroy.ValueRO.DestroyTimer > 0f)
{
particleDestroy.ValueRW.timer += SystemAPI.Time.DeltaTime;
if (particleDestroy.ValueRO.timer >= particleDestroy.ValueRO.DestroyTimer)
willDestroy = true;
}
if (willDestroy == false &&
particleDestroy.ValueRO.DestroyOnPlayEnd == true &&
particle.Value.isPlaying == false)
willDestroy = true;
if (willDestroy == true)
{
ecb.DestroyEntity(entity);
}
}
}
}
덕분에 조금 특이하게도 쿼리 할 때 조금 다르게 참조하게 됩니다.
SystemAPI.ManagedAPI.UnityEngineComponent<UnityEngine.ParticleSystem>
이런 식으로 Entity가 가지고 있는 Component Type으로 쿼리를 해야 합니다.
매뉴얼에는 아래 아주 짧게 적어뒀네요..;;
https://docs.unity3d.com/Packages/com.unity.entities@1.0/manual/systems-systemapi.html
SystemAPI | Entities | 1.0.0-pre.15
SystemAPI SystemAPI is a class that provides caching and utility methods for accessing data in an entity's world. It works in non-static methods in SystemBase and non-static methods in ISystem that take ref SystemState as a parameter. You can use it to per
docs.unity3d.com
아주 잘 됩니다.
https://the-paper.notion.site/Unity-ECS-1c1b02e774c049c2a8452c1cc7baee4b
Unity ECS 공부해보자
Entities 1.0 업데이트 이후 API가 많이 바뀌었습니다. 몇개 없긴 하지만 1.0 이전에 쓰여진 글이나 영상을 보실 때는 감안하시고 보시면 됩니다.
the-paper.notion.site
간단하게 프로젝트 만들어서 부딪쳐보는건 잠시 멈춰둘까 합니다. (아니면 이대로 마무리?)
간단하게 사용하는 방법 정도는 이해한 것 같고, 샘플 프로젝트들을 더 파보거나 좀 더 그럴싸한 프로젝트 기획해서 부딧쳐 볼 예정입니다.
지금까지 참고가 됐던 링크들은 노션에 남겨두겠습니다.
https://portal.productboard.com/wkjyyxmtns7dipwofp19pj8r/tabs/104-dots
DOTS 로드맵을 한번 찾아봤는데, 다행히도 ECS-based Animation이 로드맵에 In Progress로 있어서 다행이네요.
Animation이라도 돼야 게임다운 게임의 모습이 나오지 않을까 합니다.
'Study Log' 카테고리의 다른 글
Unity ECS Study Log - 7 (0) | 2023.01.21 |
---|---|
Unity ECS Study Log - 6 (0) | 2023.01.15 |
Unity ECS Study Log - 5 (0) | 2023.01.14 |
Unity ECS Study Log - 4 (0) | 2023.01.09 |
Unity ECS Study Log - 3 (1) | 2023.01.07 |