Randomize Prefab Texture
Hello, I'm still pretty new to coding but I was wondering if there was a way to randomize the texture that is assigned to each prefab clone and how to prevent that texture from being used again if it has already been assigned to a prefab clone. This texture will come from an imported picture. I could manually switch the textures between two prefab clones with the code below but this may take awhile and cause the system to run slow because I want to use a grid size of 25x25, therefore I would have to copy the code 624 more times. If you are able to help me with this it would be greatly appreciated. Thank you.
// create a new material using the defined shader.
Material thisTileMaterial = new Material(PuzzleShader);
// apply the puzzle image to it.
thisTileMaterial.mainTexture = PuzzleImage;
// set the offset and tile values for this material.
thisTileMaterial.mainTextureOffset = new Vector2(1.0f / Width * i, 1.0f / Height * j);
thisTileMaterial.mainTextureScale = new Vector2(1.0f / Width, 1.0f / Height);
// assign the new material to this tile for display.
TileDisplayArray[i, j].GetComponent<Renderer>().material = thisTileMaterial;
}
}
// switch the second and third grid location textures.
Material thisTileMaterial2 = TileDisplayArray[1,3].GetComponent<Renderer>().material;
Material thisTileMaterial3 = TileDisplayArray[2,3].GetComponent<Renderer>().material;
TileDisplayArray[1,3].GetComponent<Renderer>().material = thisTileMaterial3;
TileDisplayArray[2,3].GetComponent<Renderer>().material = thisTileMaterial2;
}
}
Comment