Skip to content

Instantly share code, notes, and snippets.

@Jura-Z
Last active October 30, 2019 06:03
Show Gist options
  • Save Jura-Z/f2a4358b1fdc5dc3a04cf36b494d3dc6 to your computer and use it in GitHub Desktop.
Save Jura-Z/f2a4358b1fdc5dc3a04cf36b494d3dc6 to your computer and use it in GitHub Desktop.
//IndexOutOfRangeException: Index 2 is out of range of '2' Length.
//Unity.Collections.NativeArray`1[T].FailOutOfRangeError (System.Int32 index) (at /Users/builduser/buildslave/unity/build/Runtime/Export/NativeArray/NativeArray.cs:255)
//Unity.Collections.NativeArray`1[T].CheckElementReadAccess (System.Int32 index) (at /Users/builduser/buildslave/unity/build/Runtime/Export/NativeArray/NativeArray.cs:115)
//Unity.Collections.NativeArray`1[T].get_Item (System.Int32 index) (at /Users/builduser/buildslave/unity/build/Runtime/Export/NativeArray/NativeArray.cs:140)
//Unity.Transforms.TRSToLocalToWorldSystem+TRSToLocalToWorld.Execute (Unity.Entities.ArchetypeChunk chunk, System.Int32 chunkIndex, System.Int32 entityOffset) (at Packages/com.unity.entities/Unity.Transforms/TRSToLocalToWorldSystem.cs:95)
//Unity.Entities.JobChunkExtensions+JobChunk_Process`1[T].ExecuteInternal (Unity.Entities.JobChunkExtensions+JobChunkData`1[T]& jobData, Unity.Jobs.LowLevel.Unsafe.JobRanges& ranges, System.Int32 jobIndex) (at Packages/com.unity.entities/Unity.Entities/IJobChunk.cs:208)
//Unity.Entities.JobChunkExtensions+JobChunk_Process`1[T].Execute (Unity.Entities.JobChunkExtensions+JobChunkData`1[T]& jobData, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, Unity.Jobs.LowLevel.Unsafe.JobRanges& ranges, System.Int32 jobIndex) (at Packages/com.unity.entities/Unity.Entities/IJobChunk.cs:194)
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Transforms;
[UpdateInGroup(typeof(InitializationSystemGroup))]
public class SphereSpawner : JobComponentSystem
{
struct ExecuteOnceTag : IComponentData {}
[BurstCompile]
struct Spawn : IJobParallelFor
{
public EntityCommandBuffer.Concurrent ec;
public Entity prefab;
public void Execute(int index)
{
ec.Instantiate(index, prefab);
}
}
private Entity m_spherePrefabEntity;
private Entity m_initEntity;
protected override void OnCreate()
{
if (SphereSpawnerUnity.instance != null)
{
m_initEntity = EntityManager.CreateEntity();
EntityManager.AddComponent<ExecuteOnceTag>(m_initEntity);
var spherePrefabArchetype = EntityManager.CreateArchetype(typeof(Translation), typeof(LocalToWorld));
m_spherePrefabEntity = EntityManager.CreateEntity(spherePrefabArchetype);
}
var query = GetEntityQuery(ComponentType.ReadOnly<ExecuteOnceTag>());
RequireForUpdate(query);
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
EntityManager.DestroyEntity(m_initEntity);
m_initEntity = Entity.Null;
EntityCommandBuffer ecb;
EntityCommandBufferSystem ecbSystem;
ecbSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
// ecbSystem = World.GetOrCreateSystem<EndInitializationEntityCommandBufferSystem>(); // this will fix the bug
ecb = ecbSystem.CreateCommandBuffer();
var sp = new Spawn
{
ec = ecb.ToConcurrent(),
prefab = m_spherePrefabEntity
};
var handle = inputDeps;
handle = sp.Schedule(1024, 128, inputDeps);
ecbSystem.AddJobHandleForProducer(handle);
return handle;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment