Created
December 24, 2019 11:52
-
-
Save Kellojo/7c6fe27ae799d8792f39fab746aaf470 to your computer and use it in GitHub Desktop.
Allows easy temporary swapping of materials for highlighting i.e. different status of the GameObject (think building systems, obstructed red material, valid green material, placed restore default materials).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class MaterialChanger { | |
private GameObject gameObject; | |
private Dictionary<MeshRenderer, Material> defaultMaterials = new Dictionary<MeshRenderer, Material>(); | |
public MaterialChanger(GameObject gameObject) { | |
this.gameObject = gameObject; | |
StoreDefaultMaterials(); | |
} | |
private void StoreDefaultMaterials() { | |
defaultMaterials.Clear(); | |
MeshRenderer[] meshRenderers = gameObject.GetComponentsInChildren<MeshRenderer>(); | |
foreach (MeshRenderer mr in meshRenderers) { | |
defaultMaterials.Add(mr, mr.material); | |
} | |
} | |
public void SetMaterialOnAllMeshRenderers(Material material) { | |
foreach (MeshRenderer mr in defaultMaterials.Keys) { | |
mr.material = material; | |
} | |
} | |
public void RestoreDefaultMaterials() { | |
foreach (MeshRenderer mr in defaultMaterials.Keys) { | |
mr.material = defaultMaterials[mr]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment