Instantiating large planes to surround camera.

I have a main camera view looking down on a 3 x 3 grid of plane prefabs(2d representation). The planes themselves are used to spawn procedural generated objects onto. The camera uses the floating origin script off the wiki therefore it is not held at the origin but never strays too far from the origin to prevent imprecision I found when I went above 5000 from the origin.

Therefore, the view is like this:

NW Plane - N Plane - NE Plane
W Plane — C Plane - E Plane
SW Plane - S Plane - SE Plane

My aim is to keep planes always spawned around the player. Therefore, if the camera moves far to the east I need to destroy the west planes (some/all) and then replace them with more planes in the east. By doing this the player will always have a the 3 x 3 plane grid in view.

I can spawn the objects on the plane, and destroy the prefab them depending on the distance from the camera but I am having a real ‘writer’s block’ on how to actually implement the loading of the planes. Any ideas are gratefully received - I am well aware I am missing the obvious here but sometimes you just reach something you can’t see!

First, if each texture is used only once in your 25 x 25 configuration, then instead of Instantiating() a prefab, simply create all 25 game object at startup and then turn the renderer off for ones that are not currently in use:

gameObject.renderer.enabled = false;

If your textures are used multiple times and/or in a random configuration, you can swap out the textures rather than Destroy/Create game objects. With 25 512x512 tiles you can place them in an public array
something like:

public Texture[] artex;

Then you can drag and drop the textures into the array in the inspector. Instead of creating and destroying the tiles, you can do something like:

goTile.renderer.material.texture = artex[11]; 

If you want some better resolution tiles and therefore don’t want have all the tiles loaded into memory you can load the textures dynamically with Resources.Load(). Something like:

Texture tex = (Texture)ResourcesLoad("Tile_25");
if (tex != null)
        goTile.renderer.material.texture = tex;

If you are targeting this app for a phone or tablet, you may want to consider building an atlas for your textures. I use tools that build atlases, but I’ve never directly waded through using the classes that Unity provides to build an atlas.


If you need a really big plane (but not an infinite one), and if the 25x25 textures are tiled into the world, you can instead create a single 2k x 2k or 4k x 4x uber-tile that contains all 25 textures. Next create one really big plane. Then in the material for the plane, just up the tile values. It will automatically tile your texture over the plane. It’s not infinite, but you could make one that takes minutes to cross at 5 seconds a tile.


Lastly you could create a plane mesh with a fixed number of squares (16 or 25 for this use). You’ll find an editor script here that does that job here:

http://wiki.unity3d.com/index.php?title=CreatePlane

Next you combine all 25 textures into an either 2k x 2k or 4k x 4k texture. Then to move tiles, you shift the plane and redefine the uv coordinates array for the texture. If you really wanted to attempt this, I can post a bit of source, but be prepared to struggle a bit through the details.