Exemplo n.º 1
0
 private void RemapMaterials()
 {
     if (ValidateSelection())
     {
         foreach (GameObject obj in Selection.GetFiltered(typeof(GameObject), SelectionMode.Assets))
         {
             string        path          = AssetDatabase.GetAssetPath(obj);
             ModelImporter modelImporter = AssetImporter.GetAtPath(path) as ModelImporter;
             modelImporter.materialImportMode = ModelImporterMaterialImportMode.ImportViaMaterialDescription;
             modelImporter.materialLocation   = ModelImporterMaterialLocation.InPrefab;
             modelImporter.materialName       = ModelImporterMaterialName.BasedOnModelNameAndMaterialName;
             modelImporter.materialSearch     = ModelImporterMaterialSearch.Local;
             var sourceMaterials = typeof(ModelImporter).GetProperty("sourceMaterials", BindingFlags.NonPublic | BindingFlags.Instance)?.GetValue(modelImporter) as AssetImporter.SourceAssetIdentifier[];
             foreach (var identifier in sourceMaterials ?? Enumerable.Empty <AssetImporter.SourceAssetIdentifier>())
             {
                 if (materialToAssign.TryGetValue(identifier.name, out Material result))
                 {
                     modelImporter.AddRemap(identifier, result);
                 }
                 else
                 {
                     string newPath = path.Replace(obj.name + ".fbx", "");
                     newPath += "Materials/" + obj.name + "-" + identifier.name + ".mat";
                     modelImporter.materialLocation = ModelImporterMaterialLocation.External;
                     SaveReimport();
                     modelImporter.materialLocation = ModelImporterMaterialLocation.InPrefab;
                     Material extractedMat = (Material)AssetDatabase.LoadAssetAtPath(newPath, typeof(Material));
                     modelImporter.AddRemap(identifier, extractedMat);
                     Debug.Log("Some Materials Were Not Assigned. Extracting Material from FBX at " + newPath);
                 }
             }
             SaveReimport();
         }
     }
 }
        public static void ExportMeshFilterToObjFile(MeshFilter meshFilter, string fullObjFilePath)
        {
            var outputFolder           = Path.GetDirectoryName(fullObjFilePath);
            var objFileName            = Path.GetFileName(fullObjFilePath);
            var objFileNameNoExtension = Path.GetFileNameWithoutExtension(fullObjFilePath);
            var objAssetPath           = GetAssetPath(fullObjFilePath);

            // Write out the Obj file
            File.WriteAllText(fullObjFilePath, MeshFilterToString(meshFilter, objFileNameNoExtension));

            // Write out the materials file
            var materialNames = meshFilter.GetComponent <MeshRenderer>().sharedMaterials.Select(x => x.name).ToList();

            File.WriteAllText(Path.Combine(outputFolder, objFileNameNoExtension) + ".mtl", string.Concat(materialNames.Select(x => $"newmtl {x}\n")));

            // Import the Obj file
            AssetDatabase.Refresh();
            AssetDatabase.ImportAsset(objAssetPath);

            // Updating the Obj import settings and reimporting
            ModelImporter importer = (ModelImporter)ModelImporter.GetAtPath(objAssetPath);

            if (importer)
            {
                importer.importNormals        = ModelImporterNormals.Calculate;
                importer.optimizeMeshPolygons = true;
                importer.optimizeMeshVertices = true;

                foreach (var material in meshFilter.GetComponent <MeshRenderer>().sharedMaterials)
                {
                    importer.AddRemap(new AssetImporter.SourceAssetIdentifier(material), material);
                }

                EditorUtility.SetDirty(importer);
                importer.SaveAndReimport();
            }
        }