-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursiveMaterialReplace.cs
More file actions
39 lines (36 loc) · 1002 Bytes
/
RecursiveMaterialReplace.cs
File metadata and controls
39 lines (36 loc) · 1002 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using UnityEngine;
using System.Collections;
public class RecursiveMaterialReplace : MonoBehaviour
{
/*this class will start with a gameobject
that has multiple children with possibly multiple grandchildren
on start it will detect any material on any renderer
and change it to a user defined material*/
public Material newMaterial;
// Use this for initialization
void Start ()
{
ScanChildren (transform, newMaterial);
}
private static void RecursiveReplace (Transform t, Material m)
{
ScanChildren (t, m);
}
private static void ScanChildren (Transform t, Material m)
{
if (t.childCount != 0) {
int c = t.childCount;
for (int i = 0; i < c; i++) {
ReplaceMaterial (t, i, m);
ScanChildren (t.GetChild (i), m);
}
}
}
private static void ReplaceMaterial (Transform t, int i, Material m)
{
Renderer r = t.GetChild (i).GetComponent<Renderer> () as Renderer;
if (r != null) {
r.material = m;
}
}
}