void replaceMaterials(Renderer r) { string[] matNames = new string[r.materials.Length]; for (int i = 0; i < matNames.Length; i++) { matNames[i] = "wheel"; } List <MaterialPropertyExport>[] matProperties = new List <MaterialPropertyExport> [r.materials.Length]; for (int i = 0; i < matProperties.Length; i++) { matProperties[i] = new List <MaterialPropertyExport>(); } fillMaterialInfos(r, matNames, matProperties); var materials = r.materials.ToArray(); for (int i = 0; i < r.materials.Length; i++) { MaterialInfos matInfo = null; if (!m_infos.materials.TryGetValue(matNames[i], out matInfo)) { ErrorList.add("Can't find the material " + matNames[i] + " on " + r.gameObject.FullName()); continue; } if (matInfo == null || matInfo.material == null) { continue; } Material mat = UnityEngine.Object.Instantiate(matInfo.material); if (matInfo.diffuseIndex >= 0) { mat.SetTexture(matInfo.diffuseIndex, r.materials[i].GetTexture("_MainTex")); } if (matInfo.normalIndex >= 0) { mat.SetTexture(matInfo.normalIndex, r.materials[i].GetTexture("_BumpMap")); } if (matInfo.emitIndex >= 0) { mat.SetTexture(matInfo.emitIndex, r.materials[i].GetTexture("_EmissionMap")); } foreach (var p in matProperties[i]) { copyMaterialProperty(r.materials[i], mat, p); } materials[i] = mat; } r.materials = materials; }
void getMaterials() { List <MaterialPropertyInfo> materialsNames = new List <MaterialPropertyInfo> { new MaterialPropertyInfo("Custom/LaserCut/CarPaint", "carpaint", 5, -1, -1), new MaterialPropertyInfo("Custom/LaserCut/CarWindow", "carwindow", -1, 218, 219), new MaterialPropertyInfo("Custom/Reflective/Bump Glow LaserCut", "wheel", 5, 218, 255), new MaterialPropertyInfo("Custom/LaserCut/CarPaintBump", "carpaintbump", 5, 218, -1), new MaterialPropertyInfo("Custom/Reflective/Bump Glow Interceptor Special", "interceptor", 5, 218, 255), new MaterialPropertyInfo("Custom/LaserCut/CarWindowTrans2Sided", "transparentglow", -1, 218, 219) }; foreach (var c in G.Sys.ProfileManager_.carInfos_) { var prefab = c.prefabs_.carPrefab_; foreach (var renderer in prefab.GetComponentsInChildren <Renderer>()) { foreach (var mat in renderer.materials) { foreach (var key in materialsNames) { if (materials.ContainsKey(key.name)) { continue; } if (mat.shader.name == key.shaderName) { var m = new MaterialInfos(); m.material = mat; m.diffuseIndex = key.diffuseIndex; m.normalIndex = key.normalIndex; m.emitIndex = key.emitIndex; materials.Add(key.name, m); } } } } } foreach (var mat in materialsNames) { if (!materials.ContainsKey(mat.name)) { ErrorList.add("Can't find the material: " + mat.name + " - shader: " + mat.shaderName); } } materials.Add("donotreplace", new MaterialInfos()); }