/// <summary> /// Store a map of renderers and their materials into a dictionary /// </summary> /// <param name="targetObject">Game object to scan</param> /// <param name="materialsBackup">Dictionary used to map renderers to their materials</param> public static void BackupMaterials(GameObject targetObject, MaterialMap materialsBackup) { materialsBackup.Clear(); foreach (Renderer mr in targetObject.GetComponentsInChildren <Renderer>(true)) { materialsBackup.Add(mr, mr.sharedMaterial); } }
/// <summary> /// Replace the materials in a game object with a copy of each one of them. /// Both the original materials and the cloned materials are stored in the given dictionaries. /// </summary> /// <param name="targetObject">Game object to scan</param> /// <param name="originalMaterials">Dictionary that maps renderers to their original materials</param> /// <param name="newMaterials">Dictionary that maps renderers to their cloned materials</param> /// <remarks>The given dictionaries are cleaned before they are filled.</remarks> public static void CloneMaterials(GameObject targetObject, MaterialMap originalMaterials, MaterialMap newMaterials) { originalMaterials.Clear(); newMaterials.Clear(); foreach (Renderer mr in targetObject.GetComponentsInChildren <Renderer>(true)) { originalMaterials.Add(mr, mr.sharedMaterial); mr.sharedMaterial = Material.Instantiate(mr.sharedMaterial); newMaterials.Add(mr, mr.sharedMaterial); } }