Is there a way to dynamically populate game objects via texture maps?

I am still very new to Unity but really enjoying its interface. I was curious if there was a way to dynamically populate gameObjects using texture maps. For example, we have a plane that has a black/white checkered board texture. At runtime can every black square populate a cube.

If its possible, can you do it with a hidden texture?

So you want to know the color of the texture, you can use

knowing the position of your squares, you can check for color.

Providing you use a power of 2 texture you can calculate the number of squares in the texture and their colours. You then instantiate the cubes for each square which matches the colour.

If you get a row of pixels just one pixel high you can calculate the number of columns by looping over them and checking when the colour changes. A different colour would mean the start of a new column. This only works for a checker board of course.

Once you have the row count you can calculate the width in pxels of each square by dividing the texture width by the column count. Knowing the size of each square will allow you to step through the textures pixels at offsets which matchthe width and height of your squares. You can check the colour of each one and see if it matches the one you want to instantiate stuff.

	public Texture2D texture;

	private int columnCount = 0;
	private Color[] colours = new Color[2];

	private void Start(){

		if(texture){

			// Calculate number of columns and save their individual colours
			Color[] row = texture.GetPixels(0,0,texture.width, 1);
			Color prevPixel = new Color(1000,1000,1000);

			// loop through row and figure out how many columns there are
			for (int i = 0; i < row.Length; i++) {
				
				// Save each colour
				if(columnCount == 0){
					colours[0] = row*;*
  •  		}*
    
  •  		else if(columnCount == 1) {*
    

_ colours[1] = row*;_
_
}*_

* // Check if same colour as previous pixel. inc rowcount if not*
_ if(row != prevPixel){
* columnCount ++;
}*_

* // store previous pixel*
_ prevPixel = row*;
}*_

* // calculate width of column*
* int columnWidth = texture.width / columnCount;*

* // Loop through texture pixels stepping by the column width in X and Y*
* // Check the colour of each square*

* for (int x = 0; x < texture.width; x += columnWidth) {*

* for (int y = 0; y < texture.height; y += columnWidth) {*
* if(texture.GetPixel(x, y) == colours[0]){*

* // prevent divide by 0 error*
* float cubeX = 0;*
* float cubeY = 0;*

* if(x > 0){*
* cubeX = x / columnWidth;*
* }*
* if(y > 0){*
* cubeY = y / columnWidth;*
* }*

* Instantiate(GameObject.CreatePrimitive (PrimitiveType.Cube), new Vector3(cubeX, cubeY, 0), Quaternion.identity);*
* }*
* else if(texture.GetPixel(x, y) == colours[1]){*
* // do something with the other colour if you want*
* }*
* }*
* }*
* }*
* }*