Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Created April 2, 2018 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki/acccd2d1b1e22607d3c0c3135e82c936 to your computer and use it in GitHub Desktop.
Save tsubaki/acccd2d1b1e22607d3c0c3135e82c936 to your computer and use it in GitHub Desktop.
using Unity.Entities;
// 実体
public struct CountData : IComponentData
{
public int count;
}
[System.Serializable]
public struct RangeData : ISharedComponentData
{
public int min, max;
}
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);
}
}
}
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