Hi,
For example i have scene with a lot of cubes and different materials applied to them.
Mat01, Mat02, Mat03…
I’m inserted the Button witch takes an another Material from Resources folder and applied to these cubes.
Material newMat = Resources.Load(“Material”, typeof(Material)) as Material;
Cube[1…X].GetComponent ().material = newMat;
Question is : How can i get back to default assigned materials, that every cube has his own Mat01… again. ?
I’m tryed to save every scene object cube in new Array[ ] to save material property, but problem is when i press the Button materials change in new Array[ ] as well. How to save every scene object and his material property for restore in late.
Anyone know ?
It sounds like you’re saving references to the cubes (perhaps as GameObject?). But what you’re changing is the material property of the renderer on the cube. So, keeping just the cube references wouldn’t help.
You need to instead store the Material. I would approach this by making a script that goes on each cube, that saves the material (via GetComponent().material) in a property. Then this script can have a public Restore method, that applies the saved material back to the renderer. You can call the Restore method on all cubes whenever that’s wanted.
Thanks JoeStrout for reply, but i didn’t get it, where to save materials and assignments of all GameObjects ? new Array ?
And how can it looks like public Restore method in C# ? Maybe can you show a little example ?
Sure, this is off the top of my head, but should be approximately right…
public class MaterialSaver : MonoBehaviour {
Material originalMat;
void Start() {
originalMat = GetComponent<Renderer>().material;
}
public void Restore() {
GetComponent<Renderer>().material = originalMat;
}
}
Put this on each of your cubes. Now just call GetComponent().Restore() on all of your cubes when you want to restore the original materials.
6 Likes
Finally i did it ! Thanks JoeStrout for your example it was handy.
1 Like