Skip to content

Instantly share code, notes, and snippets.

@tsubaki tsubaki/RemoveSystem.cs
Last active Apr 8, 2019

Embed
What would you like to do?
using Unity.Entities;
using Unity.Jobs;
using Unity.Transforms;
using UnityEngine;
using Unity.Mathematics;
using Unity.Collections;
class RangeCheckSystem : JobComponentSystem
{
EntityCommandBufferSystem barrier; // EntityCommandBufferSystemを用意
EntityQuery query;
protected override void OnCreate()
{
base.OnCreate();
barrier = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
query = GetEntityQuery(ComponentType.ReadOnly<Translation>());
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
inputDeps = new RangeCheckJob
{
mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition + Vector3.forward * 10),
commandBuffer = barrier.CreateCommandBuffer().ToConcurrent(), // ToConcurrentを使用
}.Schedule(this, inputDeps);
barrier.AddJobHandleForProducer(inputDeps); // 終了時にはAddJobHandleForProducer
return inputDeps;
}
[Unity.Burst.BurstCompile]
struct RangeCheckJob : IJobForEachWithEntity<Translation>
{
public Vector3 mousePosition;
[ReadOnly]
public EntityCommandBuffer.Concurrent commandBuffer; // 利用する側もConcurrent。何故かReadOnlyが必要
public void Execute(Entity entity, int index, ref Translation position)
{
if( math.distance(position.Value, mousePosition) < 1)
{
commandBuffer.DestroyEntity(index, entity);
}
}
}
}
using UnityEngine;
using Unity.Entities;
using Unity.Transforms;
using Unity.Collections;
using Unity.Mathematics;
using Random = Unity.Mathematics.Random;
public class GameController : MonoBehaviour
{
[SerializeField] GameObject prefab;
NativeArray<Entity> entities;
const int count = 4000;
void Awake()
{
entities = new NativeArray<Entity>(count, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
var entity = GameObjectConversionUtility.ConvertGameObjectHierarchy(prefab, World.Active);
var entityManager = World.Active.EntityManager;
entityManager.Instantiate(entity, entities);
Random random = new Random(128);
for (int i = 0; i < count; i++)
{
var pos = new float3(random.NextFloat(-10, 10), random.NextFloat(-10, 10), 0);
entityManager.SetComponentData(entities[i], new Translation() { Value = pos });
}
World.Active.EntityManager.DestroyEntity(entity);
entities.Dispose();
}
}
@tsubaki

This comment has been minimized.

Copy link
Owner Author

commented Oct 1, 2018

118

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.