Created
April 2, 2018 13:11
-
-
Save tsubaki/acccd2d1b1e22607d3c0c3135e82c936 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; | |
// 実体 | |
public struct CountData : IComponentData | |
{ | |
public int count; | |
} | |
[System.Serializable] | |
public struct RangeData : ISharedComponentData | |
{ | |
public int min, max; | |
} |
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.Collections; | |
public class ECSMain : MonoBehaviour | |
{ | |
[SerializeField] RangeData range; | |
EntityArchetype sampleArchetype; | |
void Start() | |
{ | |
var entityManager = World.Active.GetOrCreateManager<EntityManager>(); | |
sampleArchetype = entityManager.CreateArchetype(typeof(CountData), typeof(RangeData)); | |
} | |
private void Update() | |
{ | |
if(Input.anyKey) | |
{ | |
var entityManager = World.Active.GetOrCreateManager<EntityManager>(); | |
var entity = entityManager.CreateEntity(sampleArchetype); | |
entityManager.SetSharedComponentData<RangeData>(entity, range); | |
} | |
} | |
} |
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.Collections; | |
using Unity.Jobs; | |
[UpdateAfter(typeof(CountSystem))] | |
public class RangeSystem : ComponentSystem | |
{ | |
struct Group | |
{ | |
public int Length; | |
public EntityArray entities; | |
[ReadOnly] public ComponentDataArray<CountData> countData; | |
[ReadOnly] public SharedComponentDataArray<RangeData> rangeData; | |
} | |
[Inject] Group group; | |
protected override void OnUpdate() | |
{ | |
for (int i=0; i<group.Length; i++) | |
{ | |
var range = group.rangeData[i]; | |
var data = group.countData[i]; | |
if ( data.count > range.max || data.count < range.min ) | |
{ | |
PostUpdateCommands.DestroyEntity(group.entities[i]); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment