I have a problem similar to http://answers.unity3d.com/questions/36659/setting-sharedmaterial-only-setting-for-one-object but the proposed solution does not satisfy my needs. I have a lot of prefabs imported into my project with multiple materials per mesh.
When I import an FBX into my project, UNity creates a set of materials for it. The thing is, there are a lot of materials on different objects that should be the same. The good example is glass material on buildings: I have a lot of different buildings with different wallsand roof materials, etc. but I want them all to use the same glass material, so I could set it up once, and every time I change it, all the windows would change.
Thing is there are tons of cross used materials (around 100) like that and a lot of prefabs (like 200 with 5-10 materials per prefab), so setting materials list manually would be a huge task.
I wrought an editor script to search for materials with certain names in objects and replace them with materials form a preset. It's working perfectly with an object, but never changes a prefab. I cannot use CopyPropertiesFromMaterial because it won't make different objects use the same material, just copy settings from my preset materials to another. Here's the code I use:
Material[] tempList = recipient.renderer.sharedMaterials;
int changesDone = 0;
for(int i = 0; i < tempList.Length; i++)
{
string replacingName = ParseMaterialName(tempList*.name);*
*if(replacingName == "")*
*{*
*.....*
*continue;*
*}*
*if(nativeMaterials.ContainsKey(replacingName))*
*{*
_tempList *= nativeMaterials[replacingName];*_
_*changesDone++;*_
_*......*_
_*continue;*_
_*}*_
_*if(SharedMaterials.ContainsKey(replacingName))*_
_*{*_
<em>_tempList *= SharedMaterials[replacingName];*_</em>
<em>_*changesDone++;*_</em>
<em>_*.....*_</em>
<em>_*continue;*_</em>
<em>_*}*_</em>
<em>_*.....*_</em>
<em>_*}*_</em>
<em>_*.....*_</em>
<em>_*if(changesDone > 0)*_</em>
<em>_*{*_</em>
<em>_*recipient.renderer.sharedMaterials = tempList;*_</em>
<em>_*return true;*_</em>
<em>_*}*_</em>
<em>_*......*_</em>
<em>_*EditorUtility.SetDirty(recipient);*_</em>
<em>_*AssetDatabase.SaveAssets();*_</em>
<em>_*```*_</em>
<em>_*<p>What am I doing wrong? Is it possible to somehow change the whole materials list of a prefab and save the changes to asset database?</p>*_</em>