Created
October 6, 2020 07:35
-
-
Save sarkahn/2393212856728e0fe3d26325af35f410 to your computer and use it in GitHub Desktop.
Simple test showing how to use ExclusiveEntityTransaction in Unity ECS
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 NUnit.Framework; | |
| using Unity.Collections; | |
| using Unity.Entities; | |
| using Unity.Jobs; | |
| [TestFixture] | |
| public class EntityTransactionTests | |
| { | |
| struct SomeComponent : IComponentData | |
| { } | |
| struct TransactionJob : IJob | |
| { | |
| public ExclusiveEntityTransaction Transaction; | |
| public EntityArchetype Archetype; | |
| public void Execute() | |
| { | |
| var arr = new NativeArray<Entity>(10000, Allocator.Temp); | |
| Transaction.CreateEntity(Archetype, arr); | |
| } | |
| } | |
| // A Test behaves as an ordinary method | |
| [Test] | |
| public void SimpleExample() | |
| { | |
| var world1 = new World("World 1"); | |
| var world2 = new World("World 2"); | |
| var em1 = world1.EntityManager; | |
| var em2 = world2.EntityManager; | |
| var q1 = em1.CreateEntityQuery(typeof(SomeComponent)); | |
| var q2 = em2.CreateEntityQuery(typeof(SomeComponent)); | |
| // Note if you create the archetype from em1, it would | |
| // cause the transaction to happen in World1, even though | |
| // you call BeginTransaction/EndTransaction from em2. | |
| var archetype = em2.CreateArchetype(typeof(SomeComponent)); | |
| // Prevents access to em2 until EndTransaction is called | |
| var transaction = em2.BeginExclusiveEntityTransaction(); | |
| var job = new TransactionJob | |
| { | |
| Archetype = archetype, | |
| Transaction = transaction | |
| }.Schedule(); | |
| Assert.AreEqual(0, q1.CalculateEntityCount()); | |
| Assert.AreEqual(0, q2.CalculateEntityCount()); | |
| job.Complete(); | |
| em2.EndExclusiveEntityTransaction(); | |
| Assert.AreEqual(0, q1.CalculateEntityCount()); | |
| Assert.AreEqual(10000, q2.CalculateEntityCount()); | |
| em1.MoveEntitiesFrom(em2); | |
| Assert.AreEqual(10000, q1.CalculateEntityCount()); | |
| Assert.AreEqual(0, q2.CalculateEntityCount()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment