how to change multiple textures in one click?
Hi I am just learning Unity and get stuck with this.
I am building a city simulator that is build up of simple 3d blocks with a texture. To show different scenarios I have multiple textures for each building, IE img_1 is poor and img_2 is rich : (in which img is the imagename)
What I want is on the canvas I have a drop down box: Setting 2 options : Rich and Poor, so once I select one option all the textures will change.
So 2 things - how to change material using C# and How to call that with a standarddrop down menu (unity5)
I watched lots of videos but still cannot figure it out... Can someone give me directions?
Thanks so much, all help is highly appreciated!
Answer by Dibbie · Jun 22, 2016 at 07:34 AM
Take a look at Unitys Dropdown menu guide, it may give you a good understanding of how it works.
To achieve what you want, you need an array of GameObjects.
Your array would store all of the Game Objects, in your case your cubed buildings you want to update on the button press.
Now its best to create this script attached to your camera or some object thats easily accessible and is never destroyed. Then creating 2 public void functions, one for the "poor switch" and one for the "rich switch", which would be attached to your Dropdown's OnClick event in the Inspector.
Once that is done, you need your "poor" and "rich" textures... Depending on if it is 1 universal/generic "poor' and "rich" then creating 2 public Textures will work... But if every building has a different "poor" and "rich" texture, then you need to make a second script, that has public Textures for both that buildings specific "poor" and "rich".
So if its generic, your script to actually update every building would look something like:
public void SwitchBuildingsTextures(Texture img){
//Get every single building from your array
for(int i = 0; i < yourArrayOfGameObjects.Length - 1; i++){
//access each buildings Materal, so you can affect its Texture
yourArrayOfGameObjects[i].GetComponent<Renderer>().Material.Texture = img;
}
}
And your 2 public "MakeBuildingPoor" and "MakeBuildingRich" functions that your dropdown is looking at, is what would call "SwitchBuildingsTextures", and pass in the public reference to your generic rich/poor texture.
But if its not generic, then you would have to change that "SwitchBuildingsTexures" to something like:
public void SwitchBuildingsTextures(bool poor){
//Create a temp variable for the texture
Texture img;
//Get every single building from your array
for(int i = 0; i < yourArrayOfGameObjects.Length - 1; i++){
//Set the texture
if(poor == true){
img = yourArrayOfGameObjects[i].GetComponent<YourSecondScript>().poorTexture;}
else{ img = yourArrayOfGameObjects[i].GetComponent<YourSecondScript>().richTexture;}
//access each buildings Materal, so you can affect its Texture
yourArrayOfGameObjects[i].GetComponent<Renderer>().Material.Texture = img;
}
}
This would probably be the easiest and quickest solution I can think of right now.
Your answer
Follow this Question
Related Questions
Purple textures problem 0 Answers
Inexplicable loss of frame rate 3 Answers
2 variables holding Renderer.materials change at the same time... 0 Answers