Hello guys,
For the past week I have been working with DOTS. Its amazing I recommend it.
My problem is that I create a new native list at OnEnable inside a blob wrapper. And I need to be persistent through out the game.
private void OnEnable()
{
localLstAttractor = new List<Transform>();
totalSpawnedBoids = 0;
entitymanager = World.DefaultGameObjectInjectionWorld.EntityManager;
currentPosition = this.transform.position;
ea = entitymanager.CreateArchetype(
typeof(Translation),
typeof(Rotation),
typeof(LocalToWorld),
typeof(RenderMesh),
typeof(RenderBounds),
typeof(Boid_ComponentData)
);
debugEntities = new List<Entity>();
BlobBuilder bb = new BlobBuilder(Unity.Collections.Allocator.Temp);
boidManagerReference = new BlobAssetReference<BoidManagerBLOB>();
ref BoidManagerBLOB bmb = ref bb.ConstructRoot<BoidManagerBLOB>();
BlobBuilderArray<Boid_Manager> blobManagerArray = bb.Allocate(ref bmb.blobManagerArray, 9);
blobManagerArray[0] = new Boid_Manager
{
cohesionBias = cohesionBias,
separationBias = separationBias,
alignmentBias = alignmentBias,
targetBias = targetBias,
perceptionRadius = perceptionRadius,
step = step,
cellSize = cellSize,
fieldOfView = fieldOfView,
maxPercived = maxPercived,
maxObstacleDistance = maxObstacleDistace,
maxDistanceAttractor = maxDistanceFromAttractor,
maxDistanceGate = maxDistanceFromGate,
GatePos = new NativeList<float3>(Allocator.Persistent)
};
boidManagerReference = bb.CreateBlobAssetReference<BoidManagerBLOB>(Allocator.Persistent);
bb.Dispose();
if (colliderMesh != null)
{
col = CreateSphereCollider(colliderMesh);
}
}
And in OnDestroy I am disposing the native list and the boidManagerReference
private void OnDestroy()
{
col.Dispose();
boidManagerReference.Value.blobManagerArray[0].GatePos.Clear();
boidManagerReference.Value.blobManagerArray[0].GatePos.Dispose();
boidManagerReference.Dispose();
}
Even with this things I still get an error from unity saying
A Native Collection has not been disposed, resulting in a memory leak. Allocated from:
Boid_Spawner:OnEnable() (at Assets/Collider Cast/Scripts/Boid_Spawner.cs:82)
And I looked over the activity monitor and I can see that the memory its increasing at each PLAY
Could Anyone explain me a bit what’s happening here ??
As well after I tried it on a different computer. Realised that the fps drops by half if I play multiple times
PS: some parts of the code are from a tutorial on boids and ecs