Skip to content

Instantly share code, notes, and snippets.

@sarkahn
Created March 21, 2020 17:43
Show Gist options
  • Select an option

  • Save sarkahn/7a7295e1661287204147b571a79cd881 to your computer and use it in GitHub Desktop.

Select an option

Save sarkahn/7a7295e1661287204147b571a79cd881 to your computer and use it in GitHub Desktop.
using NUnit.Framework;
using System.Collections;
using System.Collections.Generic;
using Unity.Entities;
using UnityEngine;
namespace ECBEntityRefTests
{
struct CompA : IComponentData
{
}
class CreateRef : SystemBase
{
EndSimulationEntityCommandBufferSystem _barrier;
protected override void OnCreate()
{
base.OnCreate();
_barrier = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
}
protected override void OnUpdate()
{
var ecb = _barrier.CreateCommandBuffer().ToConcurrent();
var arch = EntityManager.CreateArchetype(typeof(CompA));
Entities
.ForEach((int entityInQueryIndex, Entity e, ref DynamicBuffer<LinkedEntityGroup> group) =>
{
var newA = ecb.CreateEntity(entityInQueryIndex, arch);
group.Add(newA);
}).ScheduleParallel();
_barrier.AddJobHandleForProducer(Dependency);
}
}
[TestFixture]
public class ECBEntityReferenceTests
{
[Test]
public void TestRef()
{
DefaultWorldInitialization.Initialize("DefaultWorld", true);
var world = World.DefaultGameObjectInjectionWorld;
var em = world.EntityManager;
var refSystem = world.GetOrCreateSystem<CreateRef>();
var barrier = world.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
var groupEntity = em.CreateEntity(typeof(LinkedEntityGroup));
refSystem.Update();
barrier.Update();
em.CompleteAllJobs();
var buffer = em.GetBuffer<LinkedEntityGroup>(groupEntity);
var aQuery = em.CreateEntityQuery(typeof(CompA));
Assert.AreEqual(aQuery.GetSingletonEntity(), buffer[0].Value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment