x


Is there a way to mass assign materials?

this should quite basic. is it possible to do it in editor ?

Thanks

more ▼

asked Dec 07 '09 at 06:30 PM

Xin gravatar image

Xin
304 17 21 27

Built-in? No I don't believe so, though I will say that would be a handy feature. Might I suggest writing an editor script that does it?

Dec 07 '09 at 08:35 PM equalsequals
(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

There's no built-in script to do it, but if you're familiar with scripting at all, it should be fairly easy to make a script to automate it.

You can use the @MenuItem attribute to create a menu item at the top of the editor which calls a certain function. Then you can use Selection.gameObjects to get a list of selected GameObjects, loop through them, and set all of their materials to whatever you want.

NOTE: Make sure the script is in the Assets/Editor folder, otherwise you can't use the Editor classes (MenuItem and Selection in this case), and will get errors.

EDIT: Here's a quick script I made to do this:

@MenuItem("Scripts/Mass Set Materials")
static function MassSetMaterials() {
    Undo.RegisterSceneUndo("Mass Set Materials");

    var mats : Material[] = Selection.activeGameObject.renderer.sharedMaterials;

    for (var obj : GameObject in Selection.gameObjects) {
    	obj.renderer.sharedMaterials = mats;
    }
}

To "install" it, just create an empty JavaScript in your project, call it "MassSetMaterials" or something similar, and put it inside the "Editor" folder (which you'll also have to create if it doesn't already exist".

Basically, what it does is to set the materials of all currently selected objects to be the same as the materials of the currently active object (the one shown in the Inspector). So to use it, just change the materials of one of the objects to whatever you want, the hold Control and select the other objects you want to change (the object you changed manually should still be shown in the inspector on the right), and click Scripts > Mass Set Materials. All the selected objects should then have the same materials.

A couple things to note:

First, I haven't tested this script beyond just making sure it works. However, I have made the script so that everything it does can be reversed by hitting Undo.

Second, the script uses RegisterSceneUndo for the undo functionality. While this makes it so that everything that the script does can be undone, it can also take up a lot of memory if you use it many times in a large scene (I'm not sure if Unity limits the size of the Undo cache).

If you have any problems with slow downs after using the script repeatedly, you may want to comment out the RegisterSceneUndo line. Note, however, that if you do this, you will NOT be able to reverse the effects of the script by hitting Undo.

I tried to get it working by using the RegisterUndo function instead, which only stores undo data for the specified objects (thus saving on memory), but I couldn't get it to work for some reason.

more ▼

answered Dec 07 '09 at 10:51 PM

Stelimar gravatar image

Stelimar
3k 14 16 50

(comments are locked)
10|3000 characters needed characters left

Oop I posted in the wrong place cant delete..sorry (

more ▼

answered Oct 23 '10 at 07:53 PM

Grimmy gravatar image

Grimmy
533 58 64 69

(comments are locked)
10|3000 characters needed characters left

Works Great, Neat! I guess that these is a reason that they don't add things like this built in, cos they have the users like you guys?

Anyway, Thanks very much. Btw, I am the one who asked the question, just got a proper login.

more ▼

answered Dec 08 '09 at 12:23 AM

Xin gravatar image

Xin
304 17 21 27

Just a little comment: Your answer (this one here that I'm commenting on) would best be posted as "comment" to the answer you are referring to. And it would certainly be nice if you could mark Stelimar's answer as "answer" (click on the greyed out check-sign below the "score" which is left of the actual answer text).

I think you were the only one who asked the question, and I think it's a very good question (I also appreciate the answer because this is the kind of snippet that can save a lot of time). Welcome to Unity Answers!!! :-) ... and yeah: I guess that is why they don't build it in ;-)

Dec 08 '09 at 10:57 AM jashan

Hi Jashan - true, but users can't comment before they have 50 points of reputation.

Dec 08 '09 at 08:30 PM Ricardo

oh, good, i can add comments now , it seems , I wont need to "answer" my own questions, good good.

Dec 09 '09 at 12:12 AM Xin

Xin, It would be good if you can mark your question as answered. E.g. if you are satisfied with Stelimar's answer, you should give him the points that he deserves by accepting his answer. Also, any questions with an accepted answer will not be listed in the "Unanswered" section of this site, making it easier to use and thereby more powerful. Good question, by the way, I'm sure it will help a lot of Unity's users understand the power of editor scripting

Dec 13 '09 at 10:39 PM Nicolaj Schweitz ♦♦
(comments are locked)
10|3000 characters needed characters left

Doesn't seem to work for me.. I followed instructions correctly but Im getting this error in Console: " NullReferenceException Mass Set Materials.MassSetMaterials () (at Assets/Editor/Mass Set Materials.js:7) "

and here is the exact copy of the code active in my project:

@MenuItem("Scripts/Mass Set Materials")
static function MassSetMaterials() {
    Undo.RegisterSceneUndo("Mass Set Materials");

    var mats : Material[] = Selection.activeGameObject.renderer.sharedMaterials;

    for (var obj : GameObject in Selection.gameObjects) {
        obj.renderer.sharedMaterials = mats;
    }
}

Any Ideas?

more ▼

answered Jun 29 '10 at 09:50 PM

VJ Anomolee gravatar image

VJ Anomolee
2

Answering someone else's question is not the way to ask for support, this isn't like a forum. Post your own question if you have a problem.

Jun 29 '10 at 10:15 PM qJake
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1679
x820
x349

asked: Dec 07 '09 at 06:30 PM

Seen: 3030 times

Last Updated: Dec 09 '09 at 03:16 PM