Last active
April 8, 2020 04:38
-
-
Save tsubaki/e446b52471ab9fa339d3026bfa30ccdd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Author
tsubaki
commented
Oct 1, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment