diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/EditorMaterial.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/EditorMaterial.cs index 300e675bf..294b875a7 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/EditorMaterial.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/EditorMaterial.cs @@ -96,8 +96,6 @@ namespace UniGLTF AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate); } - const string MaterialDirName = "Materials"; - static void ExtractMaterialsAndTextures(ScriptedImporter self) { if (string.IsNullOrEmpty(self.assetPath)) @@ -112,7 +110,7 @@ namespace UniGLTF Action> onCompleted = _ => { AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate); - self.ExtractSubAssets(MaterialDirName, ".mat"); + self.ExtractMaterials(); AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate); }; @@ -123,24 +121,18 @@ namespace UniGLTF ); } - public static void ExtractSubAssets(this ScriptedImporter importer, string dirName, string extension) where T : UnityEngine.Object + public static void ExtractMaterials(this ScriptedImporter importer) { if (string.IsNullOrEmpty(importer.assetPath)) + { return; - - var subAssets = importer.GetSubAssets(importer.assetPath); - - var path = string.Format("{0}/{1}.{2}", - Path.GetDirectoryName(importer.assetPath), - Path.GetFileNameWithoutExtension(importer.assetPath), - dirName - ); - + } + var path = $"{Path.GetDirectoryName(importer.assetPath)}/{Path.GetFileNameWithoutExtension(importer.assetPath)}.Materials"; var info = TextureExtractor.SafeCreateDirectory(path); - foreach (var asset in subAssets) + foreach (var asset in importer.GetSubAssets(importer.assetPath)) { - ExtractSubAsset(asset, string.Format("{0}/{1}{2}", path, asset.name, extension), false); + ExtractSubAsset(asset, $"{path}/{asset.name}.mat", false); } } @@ -152,7 +144,7 @@ namespace UniGLTF AssetDatabase.CreateAsset(clone, destinationPath); var assetImporter = AssetImporter.GetAtPath(assetPath); - assetImporter.AddRemap(new AssetImporter.SourceAssetIdentifier(subAsset), clone); + assetImporter.AddRemap(new AssetImporter.SourceAssetIdentifier(clone), clone); if (isForceUpdate) { diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/PBRMaterialItem.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/PBRMaterialItem.cs index 8d53c14c0..4ea932bb5 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/PBRMaterialItem.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/PBRMaterialItem.cs @@ -73,10 +73,11 @@ namespace UniGLTF } // PBR material - var material = MaterialFactory.CreateMaterial(i, null, ShaderName); + var material = default(Material); if (i >= 0 && i < gltf.materials.Count) { var src = gltf.materials[i]; + material = MaterialFactory.CreateMaterial(i, src, ShaderName); if (src.pbrMetallicRoughness != null) { if (src.pbrMetallicRoughness.baseColorFactor != null && src.pbrMetallicRoughness.baseColorFactor.Length == 4) @@ -213,6 +214,10 @@ namespace UniGLTF material.SetFloat("_Mode", (float)blendMode); } + else + { + material = MaterialFactory.CreateMaterial(i, null, ShaderName); + } return material; }