In our game we spawn many objects that have the outline. Most of them are things like guns/grenades etc. When you kill an enemy in the game, they drop various pieces of gear. We discovered that under certain circumstances killing an enemy would result in a dip in frame rate, and the dip would last longer as the level progressed.
It seems that the OutlineEffect tracks a list of all Outline instances and that every request to enable or disable the outline will result in two things happening:
A call to GameObject.FindObjectsOfType();
An O(n) scan of a list of all outline effects in the scene
My concern with this approach is as follows:
GameObject.FindObjectsOfType() is an expensive call, as it is documented. I don't have access to the source of Unity, but my guess is that it probably scans the whole scene. There typically aren't more than a few cameras in any given scene so it makes little sense to scan the whole scene just to find a few objects. I switched it to use Camera.allCameras and then tested for the presence of the OutlineEffect objects.
The "outlines" member of the Camera is a simple List and the addition/removal of outlines causes an O(n) scan of the list just to ensure there aren't duplicates. I replaced this with a custom LinkedSet, which tracks an index of items as well as a linked list to ensure ensure that the OutlineEffect can cull duplicates as well as iterate quickly over the collection. This effectively makes the call to add or remove an Outline from the OutlineEffect object as close to O(1) as we can get it.
In our game we spawn many objects that have the outline. Most of them are things like guns/grenades etc. When you kill an enemy in the game, they drop various pieces of gear. We discovered that under certain circumstances killing an enemy would result in a dip in frame rate, and the dip would last longer as the level progressed.
It seems that the OutlineEffect tracks a list of all Outline instances and that every request to enable or disable the outline will result in two things happening:
My concern with this approach is as follows: