Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Utility] Master Material Assigner script

Discussion in 'Works In Progress - Archive' started by AndrewGrayGames, Jan 20, 2013.

  1. AndrewGrayGames

    AndrewGrayGames

    Joined:
    Nov 19, 2009
    Posts:
    3,821
    I've been working on making scenes based on a set of scene pieces that, with the help of the Mesh Combiner script on the Wiki, get optimized to use a single mesh and a single material, for extremely good performance.

    One thing though - I originally messed up, and used my texturing .blend file for the actual pieces! After resolving the mistake, I was still left with prefabs where pieces had multiple textures assigned to any given piece.

    I got tired of hand-jamming fixes to having multiple textures. So, I came up with this little script. This one's on me; use as you see fit. I honestly don't even mind if you attribute me or not, this is just something to be generally helpful - I know others either A) have, or B) totally will run into the same issue. :)

    Code (csharp):
    1. using UnityEngine;
    2. using System;
    3.  
    4. public class MasterMaterialAssigner : MonoBehaviour
    5. {
    6.     public Material masterMaterial;
    7.    
    8.     public void Start()
    9.     {
    10.         NormalizeMaterial();
    11.     }
    12.    
    13.     [ContextMenu("Set child object materials to Master Material")]
    14.     public void NormalizeMaterial()
    15.     {
    16.         if(masterMaterial == null)
    17.             throw new Exception("Must specify a master material to use!");
    18.        
    19.         // Set up the array of renderer references...
    20.         MeshRenderer[] renderers = GetComponentsInChildren<MeshRenderer>();
    21.            
    22.         // Replace the materials array on each renderer with an array
    23.         // consisting of only the master material.
    24.         Material[] replacement = new Material[1] { masterMaterial };
    25.         foreach(MeshRenderer current in renderers)
    26.         {
    27.             Debug.Log ("Current renderer: " + current);
    28.             current.materials = replacement;
    29.         }
    30.        
    31.         Debug.Log("Assignment successful.");
    32.     }
    33. }
    Instructions for use:
    Step 1: Attach to a Game Object with a bunch of child meshes that are all using multiple materials that share a common UV space for a given master texture
    Step 2: Assign the material that uses the Master Texture to the variable on the script
    Step 3: Right click the script title and click the option, Set child object materials to Master Material. If all goes well, a message, Assignment successful will appear in the console.
    Step 4: Immediately save your prefab so you don't have to do all this again!

    I hope this helps :)