- Home /
Is there a way to mass assign materials?
this should quite basic. is it possible to do it in editor ?
Thanks
Built-in? No I don't believe so, though I will say that would be a handy feature. $$anonymous$$ight I suggest writing an editor script that does it?
Answer by Stelimar · Dec 07, 2009 at 10:51 PM
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.
Answer by VJ Anomolee · Jun 29, 2010 at 09:50 PM
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?
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.
Answer by Xin · Dec 08, 2009 at 12:23 AM
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.
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 ;-)
Hi Jashan - true, but users can't comment before they have 50 points of reputation.
oh, good, i can add comments now , it seems , I wont need to "answer" my own questions, good good.
Xin, It would be good if you can mark your question as answered. $$anonymous$$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
Your answer
Follow this Question
Related Questions
EditorWindow texture effected by Playmode Color Tint 1 Answer
"add selected" editor script 1 Answer
Change material.color in code and revert on Quit 2 Answers
Draw specific Object Inspector into Rect 1 Answer
Editor Window Views 0 Answers