- Home /
Collada Import and shared materails
I'm importing a whole bunch of models that are collada. A lot of them share textures. I set the importer to look in the entire project to find materials. Nonetheless at run time it seems like all models are using instances of the materails rather then the materials themselves and that this is preventing batching.
How can I get Unity to share materials across these imported models??
I should add that there are hundreds of them. Individually setting them in the editor is otu of the question. I did try to make prefabs from them via a script-- and it crashed Unity which seemed unable to handle converting such a large number to prefabs.
Is it posible for you to convert those models to FBX files, because I have tried to use collada before and it just doesn't work well. So try that and let me know how it goes:)
Answer by jeffpk · May 01, 2012 at 11:25 PM
I'm not. They are existing data I am ripping and recoding. Im doing the collada trans-coding myself with a program I wrote.
I can modify the collada pretty freely if that would help.
Answer by jeffpk · May 02, 2012 at 07:01 PM
I ended up having to force the textures to be shared at runtime. As i instance each object I submit it to this class which forces like named materials to be shared. Now dynamic batching works.
using UnityEngine;
using System.Collections.Generic;
public static class MaterialsCache {
static private Dictionary<string,Material> matDict = new Dictionary<string, Material>();
public static void UsedSharedMaterials(GameObject obj){
Renderer[] renderers = obj.GetComponentsInChildren<Renderer>();
foreach(Renderer rend in renderers){
Material mat = rend.material;
if (matDict.ContainsKey(mat.name)){
mat = matDict[mat.name];
} else {
matDict[mat.name] = mat;
}
rend.material=mat;
}
}
}