- Home /
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?
Doing it by texture requires some hard work. Everything is possible in Unity btw. I would just do a for loop maybe and add a cube every X units.
Answer by fafase · Jun 02, 2014 at 06:34 AM
So you want to know the color of the texture, you can use
http://docs.unity3d.com/ScriptReference/Texture2D.GetPixel.html
knowing the position of your squares, you can check for color.
Answer by MikeNewall · Jun 02, 2014 at 08:00 AM
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[i];
}
else if(columnCount == 1) {
colours[1] = row[i];
}
// Check if same colour as previous pixel. inc rowcount if not
if(row[i] != prevPixel){
columnCount ++;
}
// store previous pixel
prevPixel = row[i];
}
// 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
}
}
}
}
}
Your answer
Follow this Question
Related Questions
How to assign a gameobject to a script that is added dynamically 1 Answer
Instantiate flexible amount of objects to prevent frame rate drop 0 Answers
Switch to New Shader and Back to Original object's Shader in C# Script 0 Answers
Find a variable using a string 1 Answer
Is there any way I can delete the part of a GameObject that is inside another? 0 Answers