The main idea is to disable the enemy outside the camera.
On the stage I created 2 objects:
1) Enemy - a “container” for the enemy.
The EnemyController script is connected to it. It has Box Collider 2D isTrigger.
2) EnemyCharacter - enemy.
This object is nested within the Enemy object.
This is an object of type GameObject->Spine->SkeletonAnimation
The EnemyCharacterController script is connected to it.
My camera has Box Collider 2D isTrigger with Rigidbody 2D Kinematics.
When the camera collider intersects the Enemy collider (the "container" for the enemy), the SetActiveSkeletonCharacter method is called. It activates the EnemyCharacter object.
At this moment there is a freeze and a drop in FPS.
Based on the profiler, the freeze is the fault of Spine.
Why does activating a Spine object cause a drop in FPS?
public class EnemyController : MonoBehaviour
{
// Spine->Skeleton Animation Object (Character).
[SerializeField] GameObject skeletonCharacter;
private void Awake()
{
// Initially disable the character.
SetActiveSkeletonCharacter(false);
}
// Enable or disable a character.
private void SetActiveSkeletonCharacter(bool enableSkeletonCharacter)
{
skeletonCharacter.SetActive(enableSkeletonCharacter);
}
// Enable the character object.
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("CameraView"))
SetActiveSkeletonCharacter(true);
}
}