Hello,
I need to have health bars above enemies.
There is a LOT of enemies in the map and I need the most optimized solution.
I want to enable the OnGUI only when the camera is near enough to those objects.
What I did is I attached separate Script to each object, which has only OnGUI with drawing of the healthBar.
What I am doing now is enabling / disabling this component when the object is around like this:
(This function is called only once half a second to optimize it even more)
void updateGUI()
{
GUI_SpaceShip[] script = GameObject.FindObjectsOfType();
foreach(Component ship in script)
{
Vector2 distance = ship.gameObject.transform.position - cameraTransform.position;
if (distance.magnitude < 300)
ship.GetComponent().enabled = true;
else
ship.GetComponent().enabled = false;
}
}
But ofc the thing is there could be even 500+ objects in the scene.
Do you think it could be done better way?
↧