Reputation: 1
Good day! I have a problem with optimization in the project, I use the library in the public domain. https://github.com/Ideefixze/DTerrain with its help, dynamic destruction can be done. It works well, but there is one problem. During destruction, colliders are disabled and the unity physics engine calls Physics2D.Collider2D.DestroyShapes, because colliders can be disabled a lot at a time, drawdowns may occur. Is there a way to optimize this moment somehow?
Screenshot from profiler:
public void UpdateColliders(List<Column> pixelData, ITextureSource textureSource)
{
PPU = textureSource.PPU;
Vector2Int textureSize = new Vector2Int(textureSource.Texture.width, textureSource.Texture.height);
if (rects != null) rects.Clear();
List<BoxCollider2D> colls = new List<BoxCollider2D>(gameObject.GetComponents<BoxCollider2D>());
rects = new List<Rect>();
QuadTreeToRect(pixelData, 0, 0, textureSize.x, textureSize.y);
foreach (BoxCollider2D b in colls)
{
b.enabled = false;
}
foreach (Rect r in rects)
{
Vector2 rColliderOffset = new Vector2(r.x + r.size.x / 2, r.y + r.size.y / 2f);
BoxCollider2D boxC = colls.Find(coll => coll.offset == rColliderOffset && coll.size == r.size);
if (!boxC)
{
AddComponentAsync(rColliderOffset,r.size);
}
else
{
boxC.enabled = true;
}
}
foreach (BoxCollider2D b in colls)
{
if (b.enabled == false)
{
CounterTime.Time += Time.deltaTime * 100;
Destroy(b, CounterTime.Time);
}
}
}
I commented out the line where the collider is finally destroyed, leaving only enabled=false. Physics2D.Collider2D.Destroy shape still appears. Through the profiler I found that it is called instantly
Upvotes: 0
Views: 43