Advertisement
Guest User

ChangeMaterialChildren

a guest
May 31st, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.04 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine;
  4.  
  5. //This attribute is used so the component is working in also Edit Mode  (not in play)
  6. [ExecuteInEditMode]
  7. public class ChangeMaterialChildren : MonoBehaviour
  8. {
  9.     //Class to semplify the visualization on inspector window
  10.     [System.Serializable]
  11.     class MeshMaterial
  12.     {
  13.         public MeshRenderer targetMeshRenderer;        //Target meshRender to apply targetMaterial
  14.         public Material targetMaterial;                //Target maaterial to apply to meshRenderer
  15.         public bool updateMaterial;                    //Used on inspector window to apply targetMaterial on meshRenderer
  16.     }
  17.  
  18.     [SerializeField]
  19.     List<MeshMaterial> meshMaterial;  //List of combination of MeshRenderer - Targetmaterial
  20.  
  21.     [SerializeField]
  22.     private bool updateAll = false;           //Used on inspector to update all meshRenderer in the list meshMaterial
  23.  
  24.  
  25.     void Update()
  26.     {
  27.         //When you check the box (uptadeMaterial) in the inspector of an item in the meshMaterial list
  28.         MeshMaterial itemToUpdate = meshMaterial.Find(m => m.updateMaterial == true);
  29.         if (itemToUpdate != null)
  30.         {
  31.             itemToUpdate.targetMeshRenderer.sharedMaterial = itemToUpdate.targetMaterial;             //Applying material to meshRenderer
  32.             //Setting to false again because we already applied to material
  33.             //other wise this is called every frame (not ideal)
  34.             itemToUpdate.updateMaterial = false;                                                    
  35.         }
  36.  
  37.         //When you check the box (uptadeAll)
  38.         if (updateAll)
  39.         {
  40.             //apply all the materials
  41.             foreach (var m in meshMaterial)
  42.                 m.targetMeshRenderer.sharedMaterial = m.targetMaterial;
  43.                
  44.  
  45.             //Reset updateAll, otherwise this is called every frame (not ideal)
  46.             updateAll = false;
  47.         }
  48.     }
  49.  
  50.     //This is called when you attach this component on a gameObject, so I use it to initialize
  51.     void Reset()
  52.     {
  53.         meshMaterial = new List<MeshMaterial>();
  54.  
  55.         //I assume that your "carpets" are 3D models, so i'll search all children items of this gameobject with MeshRenderer component.
  56.         List<MeshRenderer> mr = GetComponentsInChildren<MeshRenderer>().ToList();
  57.  
  58.         //If i recall correctly GetComponentsInChildren will include the parent (this gameobject) so i remove the parent
  59.         MeshRenderer myMeshRenderer = GetComponent<MeshRenderer>();
  60.         if (myMeshRenderer != null)
  61.             mr.Remove(myMeshRenderer);
  62.  
  63.         //Initializing the list of MeshRenderers affected
  64.         foreach(var r in mr)
  65.         {
  66.             MeshMaterial meshMat = new MeshMaterial();
  67.             meshMat.targetMeshRenderer = r;
  68.             meshMat.targetMaterial = r.sharedMaterial;  //By default the targetMaterial is the current material of the mesh renderer
  69.             meshMat.updateMaterial = false;
  70.  
  71.             meshMaterial.Add(meshMat);
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement