- Home /
Question by
piyushiiii · Apr 06, 2016 at 07:13 AM ·
c#meshmaterialcolor
Saving colors of multiple mesheseach having multiple materals
I need a function where I can pass Gameobject and I have a global static variable SavedColor[][] .
I had chosen the SavedColor as a 2D array as my gameobject can have multiple meshes , each mesh has different materials having different colors each .
private static Color[,] SavedColor; private static bool is_saved = false; void Start() { SavedColor = new Color[100,100]; }
void Save_Color_Full(GameObject obj) { // Find all the renderers Renderer[] rs = obj.GetComponentsInChildren(); //Debug.Log(rs.Length);
for (int i = 0; i < rs.Length; i++)
{
int j = 0;
Material[] ms = rs [i].materials;
foreach (Material m in ms)
{
Saved_Colors [i,j] = m.color;
rs [i].materials [j] = m;
//Debug.Log (Saved_Colors[i,j]);
j++;
}
}
save_color = true;
}
void Load_Color_Full(GameObject obj) { save_color = false; Renderer[] rs = obj.GetComponentsInChildren();
for (int i = 0; i < rs.Length; i++)
{
int j = 0;
Material[] ms = rs [i].materials;
foreach (Material m in ms)
{
m.color = Saved_Colors [i,j];
rs [i].materials [j] = m;
j++;
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612074249im_/https://answers.unity.com/themes/thub/images/avi.jpg)