Search Unity

assign a material to multiple object?

Discussion in 'Editor & General Support' started by unforgiven8419, Nov 25, 2009.

  1. unforgiven8419

    unforgiven8419

    Joined:
    Jun 27, 2009
    Posts:
    9
    Hi,
    i try to select multiple object and try to assign a material to that but the last object get that material.
    so how can i assign a material to multiple object?
     
  2. h.istvan

    h.istvan

    Joined:
    Aug 4, 2009
    Posts:
    77
    What do you mean by selecting multiple obect?
    Maybe you should make an array of GameObjects, and assign material to elements of that array!
     
  3. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    You can't apply a material to several selected objects at once, they have to be set individually.
     
  4. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
    It's possible with an Editor Script. Save this as AssignMaterial.js and put in a folder named Editor.

    Code (csharp):
    1. class AssignMaterial extends ScriptableWizard
    2. {
    3.     var theMaterial : Material;
    4.    
    5.     function OnWizardUpdate()
    6.     {
    7.         helpString = "Select Game Obects";
    8.         isValid = (theMaterial != null);
    9.     }
    10.    
    11.     function OnWizardCreate()
    12.     {
    13.        
    14.         var gos = Selection.gameObjects;
    15.    
    16.         for (var go in gos)
    17.         {
    18.             go.renderer.material = theMaterial;
    19.         }
    20.     }
    21.    
    22.     @MenuItem("Custom/Assign Material", false, 4)
    23.     static function assignMaterial()
    24.     {
    25.         ScriptableWizard.DisplayWizard(
    26.             "Assign Material", AssignMaterial, "Assign");
    27.     }
    28. }
     
  5. PrvtHudson

    PrvtHudson

    Joined:
    Apr 10, 2009
    Posts:
    236
    thanx ! sluurp ! :D
     
  6. CavemanLog

    CavemanLog

    Joined:
    Mar 25, 2009
    Posts:
    12
    This is an old post, but I wanted to thank you for the script. Seriously cut down on material assignment.
     
  7. Tysoe

    Tysoe

    Joined:
    Jul 6, 2009
    Posts:
    577
    This is handy. To be honest I wish unity did more things to multiple selections much like 3dsmax does.

    In 3ds max if you select multiple objects and go to the equivalent of the inspector, you can change settings for all selected objects globally.

    If you have a selection where a checkbox or whatever is not the same on all objects then the current setting is greyed out but you can overwrite these settings globally by changing it and it will no longer be greyed out.

    Perhaps it's worth suggesting this behaviour as a wish list. It really speeds things up in the editor if you can do global settings on a multi selection of entities. Having to do the same task multiple times on seperate individual items is slow and cumbersome and takes an awful long time.
     
  8. wannabeartist

    wannabeartist

    Joined:
    Jun 20, 2009
    Posts:
    272
    Umh,

    Sorry to bring back this old thread, but is there a way to get this to work with 3.0? It won't compile...
     
  9. unseenthings

    unseenthings

    Joined:
    Nov 12, 2009
    Posts:
    32
    Works fine in 3.0 here -- make sure that you specify it's a javascript, though.
     
  10. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    865
    thanks for this, it is very useful
     
  11. athanazio

    athanazio

    Joined:
    Nov 10, 2009
    Posts:
    26
    thanks for the script !! based on your idea I made to others ... :p
    one to change a prefab from the selected objects, and other to rename with sequential numbers

    Code (csharp):
    1.  
    2. class AssignPreFab extends ScriptableWizard
    3. {
    4.     var thePrefab : GameObject;
    5.    
    6.     function OnWizardUpdate()
    7.     {
    8.         helpString = "Select Game Obects";
    9.         isValid = (thePrefab != null);
    10.     }
    11.    
    12.     function OnWizardCreate()
    13.     {
    14.        
    15.         var gos = Selection.gameObjects;
    16.    
    17.         for (var go in gos)
    18.         {
    19.             go.GetComponent(zombiegen).otherUserType = thePrefab;
    20.         }
    21.     }
    22.    
    23.     @MenuItem("Custom/Assign Prefab", false, 4)
    24.     static function assignMaterial()
    25.     {
    26.         ScriptableWizard.DisplayWizard("Assign Material", AssignPreFab, "Assign");
    27.     }
    28. }
    29.  
    the other that renames
    Code (csharp):
    1.  
    2. class RenameWithNumber extends ScriptableWizard
    3. {
    4.     var namePreffix: String;
    5.        
    6.     function OnWizardUpdate()
    7.     {
    8.         helpString = "Rename with numbers";
    9.         isValid = (namePreffix != null);
    10.     }
    11.    
    12.     function OnWizardCreate()
    13.     {
    14.        
    15.         var gos = Selection.gameObjects;
    16.         var counter :int = 1;
    17.        
    18.         for (var go in gos)
    19.         {
    20.             go.name = namePreffix + counter++;
    21.         }
    22.     }
    23.    
    24.     @MenuItem("Custom/Rename with numbers", false, 5)
    25.     static function rename()
    26.     {
    27.         ScriptableWizard.DisplayWizard("Rename with numbers", RenameWithNumber, "rename");
    28.     }
    29. }
    30.  
     
  12. franktrog

    franktrog

    Joined:
    May 31, 2011
    Posts:
    10
    How do I use this script?
     
    Last edited: Sep 1, 2011
  13. franktrog

    franktrog

    Joined:
    May 31, 2011
    Posts:
    10
    Some help on how to run this would be awesome!
     
  14. xindexer

    xindexer

    Joined:
    Dec 29, 2011
    Posts:
    9
    For those of you who can't make this work:

    -Create a javascript script
    -Call it "AssignMaterial"
    -Create an "Editor" folder in the Assets folder if it isn't there already
    -Place script in that folder
    -This will create a new menu along the top "Custom"
    -Click Custom/Assign Material
    -A pop up box will come up with a place to drop the material
    -Select the objects
    -Click Assign

    Hope this helps out
     
  15. loopyllama

    loopyllama

    Joined:
    Apr 6, 2009
    Posts:
    71
    Here is the c sharp version:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4. using UnityEditor;
    5.  
    6. public class AssignMaterial : ScriptableWizard
    7. {
    8.  
    9.     public Material theMaterial;
    10.     String strHelp = "Select Game Objects";
    11.     GameObject[] gos;
    12.    
    13.     void OnWizardUpdate ()
    14.     {
    15.         helpString = strHelp;
    16.         isValid = (theMaterial != null);
    17.     }
    18.    
    19.     void OnWizardCreate ()
    20.     {
    21.         gos = Selection.gameObjects;
    22.         foreach (GameObject go in gos)
    23.         {
    24.             go.renderer.material = theMaterial;
    25.         }
    26.     }
    27.    
    28.     [MenuItem ("Custom/Assign Material", false, 4)]
    29.     static void assignMaterial()
    30.     {
    31.         ScriptableWizard.DisplayWizard ("Assign Material", typeof(AssignMaterial), "Assign");
    32.     }
    33. }
     
  16. camentos

    camentos

    Joined:
    Jul 20, 2010
    Posts:
    1
    If I don't get the question wrong, I think what you are looking for is simply by selecting all the objects from the Hierarchy window, and drag the material that you want to apply to the Inspector instead, NOT the Hierarchy.

    Also take note that you can't apply directly to parented objects.

    Hope this helps.
     
  17. LordKnyv

    LordKnyv

    Joined:
    Dec 12, 2012
    Posts:
    36
    the assign material script just crashes unity 4.0
    did somoen manage to makeit work with 4 ?
     
  18. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
    This script is obsolete as you can now edit multiple selections at the same time.
     
  19. dproederSGD

    dproederSGD

    Joined:
    Aug 29, 2012
    Posts:
    13
    Yes you can select multiple game objects and change values in the Inspector, you still can't drag and drop a material on multiple selected objects in the inspector.

    and I just used this script in 4.3.1 with no issue what so ever.

    Let me re-iterate the steps listed above that I JUST used successfully.




    For those of you who can't make this work:

    -Create a javascript script
    -Call it "AssignMaterial"
    -Create an "Editor" folder in the Assets folder if it isn't there already
    -Place script in that folder
    -This will create a new menu along the top "Custom"
    -Click Custom/Assign Material
    -A pop up box will come up with a place to drop the material
    -Select the objects
    -Click Assign


    Biggest pitfall I think people are having is they are making a C# script.
     
  20. madguru

    madguru

    Joined:
    Nov 1, 2013
    Posts:
    7
    Thanks for the great scripts! I wondered if there is an easy way to select all objects within a prefab, in order to run the AssignMaterial.js script in one go. A character hierarchy can be pretty complex with lots of nodes on a character that is a jointed model.

    Is there a method or script by which I can select the top node of a prefab, click a button or run a script and then have all geometry objects below it selected so that I can run AssignMaterial.js on them?

    So far, the best way I have found is to filter in the hierarchy using
    Code (csharp):
    1. t:objects
    . This at least narrows down the number of objects to sift through.
     
    Last edited: Jan 23, 2014
  21. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86
    Just thought I should add the unity 5 version of the code:
    Code (CSharp):
    1. class AssignMaterial extends ScriptableWizard
    2. {
    3.     var theMaterial : Material;
    4.  
    5.     function OnWizardUpdate()
    6.     {
    7.         helpString = "Select Game Obects";
    8.         isValid = (theMaterial != null);
    9.     }
    10.  
    11.     function OnWizardCreate()
    12.     {
    13.      
    14.         var gos = Selection.gameObjects;
    15.  
    16.         for (var go in gos)
    17.         {
    18.             go.GetComponent.<Renderer>().material = theMaterial;
    19.         }
    20.     }
    21.  
    22.     @MenuItem("Custom/Assign Material", false, 4)
    23.     static function assignMaterial()
    24.     {
    25.         ScriptableWizard.DisplayWizard(
    26.             "Assign Material", AssignMaterial, "Assign");
    27.     }
    28. }
     
  22. Wiechering

    Wiechering

    Joined:
    Jun 22, 2015
    Posts:
    9
    Made the script recursive and added a check if the given objects have a renderer or not. You can swap the recursive thingy by selecting a bool.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System;
    5. using UnityEditor;
    6.  
    7. public class AssignMaterial : ScriptableWizard {
    8.     public bool runRecursively =  true;
    9.     public Material theMaterial;
    10.     String strHelp = "Select Game Objects";
    11.     GameObject[] gos;
    12.    
    13.     void OnWizardUpdate (){
    14.         helpString = strHelp;
    15.         isValid = (theMaterial != null);
    16.     }
    17.    
    18.     void OnWizardCreate (){
    19.         gos = Selection.gameObjects;
    20.         foreach (GameObject go in gos){
    21.             changeMaterial(go);
    22.         }
    23.     }
    24.  
    25.     void changeMaterial(GameObject go){
    26.         if (go.GetComponent<Renderer> ()) {
    27.             go.GetComponent<Renderer> ().material = theMaterial;
    28.         }
    29.         if (runRecursively == true) {
    30.             for(int i = 0; i < go.transform.GetChildCount(); i++){
    31.                 changeMaterial(go.transform.GetChild(i).gameObject);
    32.             }
    33.         }
    34.     }
    35.  
    36.     [MenuItem ("Custom/Assign Material", false, 4)]
    37.     static void assignMaterial(){
    38.         ScriptableWizard.DisplayWizard ("Assign Material", typeof(AssignMaterial), "Assign");
    39.     }
    40. }
    41.  
    Find a unity package here http://www.consus.de/files/unity/EditorMultiAssignMaterial.unitypackage
     
  23. MACHIN3

    MACHIN3

    Joined:
    May 25, 2016
    Posts:
    61
    Super useful, thanks everyone and thanks @Wiechering for the recursive option!
     
    Neikke and Wiechering like this.
  24. Todd-Wahoske

    Todd-Wahoske

    Joined:
    Oct 23, 2012
    Posts:
    4
    Thank the heavens for Wiechering's script! Using this with Mesh Baker for creating texture atlases for 3d meshes. Would be nice if Unity was able to do it all by default. :) *cough*
     
    Neikke likes this.
  25. dilmerval

    dilmerval

    Joined:
    Jun 15, 2013
    Posts:
    232
    Awesome guys thank you I really needed this.
     
  26. ekergraphics

    ekergraphics

    Joined:
    Feb 22, 2017
    Posts:
    257

    This was great, thank you!

    In Unity 2017.3 I'm having a few issues with it, though:

    1. It doesn't show up in the menu, despite placing it in an Assets/Editor folder until after I add [ExecuteInEditMode] which is weird, since I didn't have to do that in 5.6.
    2. The wizard help string doesn't seem to appear anywhere.
    3. It doesn't replace all materials (note the black materials in the selection after clicking below):
     
  27. jaycoxcp

    jaycoxcp

    Joined:
    Oct 7, 2017
    Posts:
    8
    This was super helpful. You rule!
     
  28. daaavid

    daaavid

    Joined:
    May 23, 2019
    Posts:
    1
    Very helpful, thank you all! :)
     
  29. arunosborn

    arunosborn

    Joined:
    Sep 23, 2018
    Posts:
    1
    Does this work in 2019.3 ? I don't seem to get the "custom" tab.
     
  30. rolando75

    rolando75

    Joined:
    Oct 30, 2017
    Posts:
    3
    Works great in Unity 2019.3. Thanks!
     
  31. Zanamb

    Zanamb

    Joined:
    Jul 8, 2018
    Posts:
    1
    The easiest way to do this: drag material to mesh renderer -> materials
     
  32. coolamigo

    coolamigo

    Joined:
    Mar 28, 2020
    Posts:
    25
    Very very late but to help anyone, you can. Read from camentos's reply below
     
    Brennan_Sullivan likes this.
  33. Jamz8000

    Jamz8000

    Joined:
    Jun 24, 2022
    Posts:
    1
    THE REAL ANSWER.. thank you sir
     
  34. Cave2137

    Cave2137

    Joined:
    Jan 23, 2018
    Posts:
    14
    After selecting your Game Objects the Matieral should be applied to the Mesh Renderer. This is recommended as the Materials component might not be visible when multiple materials are in use across various objects, whereas the Mesh Renderer is always visible.
     
  35. theriser777

    theriser777

    Joined:
    Feb 2, 2020
    Posts:
    12
    I will be relying on scripts in the future to automate this task but for testing purposes this was exactly what I needed, thanks.