Representing object positions in arrays

Hi, I’m thinking about the best way to represent positioning of game objects/terrain in a large 2D game world.

One idea I had is to have arrays representing positions of game objects, which would then map to the actual game coordinates with a scale factor. Like in this example image, I have an array of 144 elements (12 by 12) which holds float values, which in turn can represent objects/terrain etc. (for example the mushroom in the top left grid is positioned four cells to the left and four cells above the center cell of the array, which would represent the center of the game world. In world coordinates this object could for instance be placed with a factor of 100, at (-400, 400).

How would I nicely implement this so that when I move to the position of the object, I can draw and place the object accordingly in my viewport and when the viewport leaves the position I don’t draw it anymore?

At least in theory, this feels like a quite effective and easy to understand way of representing a 2d game world; The arrays could be defined with higher resolution in order to place objects closer to each other, and I can adjust the scale of the world with a larger scale factor between array and world coordinates. And objects are only created/drawn when they are visible.

I started experimenting with a script that, in each update, tries to find its current position in the grid/array and identify any objects that are visible in the viewport to render, but I can’t quite figure it out. Maybe there is some easier way of doing this in Unity.

By far the easiest way to do this is to have two cameras, one looking from far, one looking from close.

If that doesn’t work for you (f/ex you want that smaller cam to show not actual game objects, but smaller icons), you can call basically have each object tell the minimap where to draw it’s icon. Can look something like this (assuming your main camera is looking at your viewport):

public class SpaceObject : MonoBehaviour {
	public float x { get { return gameObject.transform.position.x } } // easier read access to x and y of object's transform
	public float y { get { return gameObject.transform.position.y } } // ... or z, if you are using x-z axis for some reason
	private ObjectMinimapIcon icon; // reference to object's minimap sprite
	private Minimap minimap; // reference to minimap handling script

	public void Update {
		if (Mathf.abs(x) < 100  Mathf.abs(y) < 100) // if within viewport range of 100
			renderer.enabled = true;
		else renderer.enabled = false;
		if (Mathf.abs(x) < 600  Mathf.abs(y) < 600) // if within minimap range of 600
			minimap.Draw(icon, x/100, y/100); // tell minimap to draw icon at minimap X and minimap Y
	}
}

Actual implementation of ObjectMinimapIcon and Minimap classes is up to you.