I have a big 2D sci-fi strategy game where the gameboard is an enormous grid of Tiles. The Tiles may have GameObject units (spaceships, planets, UFOs, etc.) within them, or they may be empty. Now, when the units are rendered, I imagine that they would be rendered with a unit icon, a nametag, and maybe some simple, color-coded HUD display to the side, like this:
But when the player zooms out the main camera past some distance, the game would no longer render the nametags, just to keep the game visually easy on the eye:
And if the player zooms out even further, the HUD display should vanish next:
So depending on the distance between the Main Camera and the gameboard, the game should render (or not render) the various components of the game’s units.
I think I could figure out a solution on my own. For instance, I have some C# code where a Tile object can compute its distance to the camera:
public class TileSquare : MonoBehaviour
{
public int computeDistToMainCamera()
{
int layer = gameObject.layer;
Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position); // Here, "transform" is the Transform component of the GameObject
return (int)Math.Floor(screenPos.z);
}
}
And in the TileSquare Update() method, I could use…
Renderer renderer = this.GetComponent<Renderer>();
renderer.enabled = false; // Disable rendering
renderer.enabled = true; // Enable rendering
…to switch the unit’s nametag / HUD / icon on and off.
But my question is, should I take this approach? I’ve been reading up on Frustum Culling with distance and Occlusion Culling with distance (like here), and I don’t think those concepts really address what I want to do.
I’m also concerned about performance issues, as my gameboard may have hundreds of Tiles and GameObjects, and if the CPU is forever measuring the distance between every object and the Main Camera, couldn’t that bog down processing?
Any advice is appreciated! Thanks.