Created
April 4, 2019 08:36
-
-
Save tsubaki/08385da8d9f1500c2f157d7d64ce5b38 to your computer and use it in GitHub Desktop.
ISystemStateComponentDataのサンプル
This file contains hidden or 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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using Unity.Entities; | |
using Unity.Jobs; | |
using Unity.Mathematics; | |
using Unity.Collections; | |
public class DestroyCheckSystem : JobComponentSystem | |
{ | |
EndSimulationEntityCommandBufferSystem barrier; | |
protected override void OnCreateManager() | |
{ | |
for(int i=0; i<3; i++){ | |
var entity = EntityManager.CreateEntity( ComponentType.ReadWrite<MyData>()); | |
EntityManager.SetComponentData(entity, new MyData{ Value = 30 }); | |
} | |
barrier = World.GetOrCreateManager<EndSimulationEntityCommandBufferSystem>(); | |
} | |
protected override JobHandle OnUpdate(JobHandle inputDeps) | |
{ | |
var commandBuffer = barrier.CreateCommandBuffer(); | |
inputDeps = new AddStateJob{ commandBuffer = commandBuffer }.Schedule(this, inputDeps); | |
inputDeps = new UpdateJob{ | |
isKeyDown = Input.GetKey(KeyCode.Space), | |
commandBuffer = commandBuffer }.Schedule(this, inputDeps); | |
inputDeps = new DestroyEventJob{ commandBuffer = commandBuffer }.Schedule(this, inputDeps); | |
barrier.AddJobHandleForProducer(inputDeps); | |
return inputDeps; | |
} | |
[ExcludeComponent(typeof(MyStateData))] | |
struct AddStateJob : IJobProcessComponentDataWithEntity<MyData> | |
{ | |
[ReadOnly] public EntityCommandBuffer commandBuffer; | |
public void Execute(Entity entity, int index, ref MyData c0) | |
{ | |
commandBuffer.AddComponent(entity, new MyStateData{Value = 15}); | |
} | |
} | |
struct UpdateJob : IJobProcessComponentDataWithEntity<MyData> | |
{ | |
[ReadOnly] public EntityCommandBuffer commandBuffer; | |
public bool isKeyDown; | |
public void Execute(Entity entity, int index, ref MyData data) | |
{ | |
if( isKeyDown) { data.Value -= 1; } | |
if( data.Value < 0 ){ commandBuffer.DestroyEntity(entity); } | |
} | |
} | |
[ExcludeComponent(typeof(MyData))] | |
struct DestroyEventJob : IJobProcessComponentDataWithEntity<MyStateData> | |
{ | |
[ReadOnly] public EntityCommandBuffer commandBuffer; | |
public void Execute(Entity entity, int index, ref MyStateData data) | |
{ | |
Debug.Log($"Oh MyData destroyed!! value:{data.Value}"); | |
commandBuffer.RemoveComponent(entity, ComponentType.ReadWrite<MyStateData>()); | |
} | |
} | |
} | |
[System.Serializable] | |
public struct MyData : IComponentData{ public int Value; } | |
[System.Serializable] | |
public struct MyStateData : ISystemStateComponentData { public int Value; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment