diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GltfScriptedImporter.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GltfScriptedImporter.cs index d61ccc604..7c3f9862d 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GltfScriptedImporter.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GltfScriptedImporter.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System.IO; +using System.Linq; using UnityEditor; using UnityEditor.Experimental.AssetImporters; using UnityEngine; @@ -12,7 +13,6 @@ namespace UniGLTF [SerializeField] Axises m_reverseAxis = default; - const string TextureDirName = "Textures"; const string MaterialDirName = "Materials"; public override void OnImportAsset(AssetImportContext ctx) @@ -83,11 +83,23 @@ namespace UniGLTF public void ExtractMaterialsAndTextures() { - TextureExtractor.ExtractTextures(this, TextureDirName, () => + if (string.IsNullOrEmpty(assetPath)) { - this.ExtractAssets(MaterialDirName, ".mat"); - AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate); - }); + return; + } + + TextureExtractor.ExtractTextures(assetPath, + this.GetSubAssets(this.assetPath).ToArray(), + externalObject => + { + this.AddRemap(new AssetImporter.SourceAssetIdentifier(typeof(UnityEngine.Texture2D), externalObject.name), externalObject); + }, + () => + { + AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate); + this.ExtractAssets(MaterialDirName, ".mat"); + AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate); + }); } public void SetExternalUnityObject(UnityEditor.AssetImporter.SourceAssetIdentifier sourceAssetIdentifier, T obj) where T : UnityEngine.Object diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterExtension.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterExtension.cs index e5c7cc53b..7eeef7bfa 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterExtension.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterExtension.cs @@ -75,22 +75,12 @@ namespace UniGLTF dirName ); - var info = importer.SafeCreateDirectory(path); + var info = TextureExtractor.SafeCreateDirectory(path); foreach (var asset in subAssets) { ExtractFromAsset(asset, string.Format("{0}/{1}{2}", path, asset.name, extension), false); } } - - - public static DirectoryInfo SafeCreateDirectory(this ScriptedImporter importer, string path) - { - if (Directory.Exists(path)) - { - return null; - } - return Directory.CreateDirectory(path); - } } } diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs index 77ce3fa01..853ea557f 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs @@ -1,16 +1,16 @@ using System; using System.Collections.Generic; using System.IO; -using System.Text.RegularExpressions; using UnityEngine; using UnityEditor; -using UnityEditor.Experimental.AssetImporters; using System.Linq; namespace UniGLTF { class TextureExtractor { + const string TextureDirName = "Textures"; + GltfParser m_parser; public GltfParser Parser => m_parser; @@ -21,17 +21,27 @@ namespace UniGLTF UnityEngine.Texture2D[] m_subAssets; string m_path; - public TextureExtractor(ScriptedImporter importer) + public TextureExtractor(string assetPath, UnityEngine.Texture2D[] subAssets) { // parse GLTF m_parser = new GltfParser(); - m_parser.ParsePath(importer.assetPath); + m_parser.ParsePath(assetPath); - m_path = $"{Path.GetDirectoryName(importer.assetPath)}/{Path.GetFileNameWithoutExtension(importer.assetPath)}.Textures"; - m_subAssets = importer.GetSubAssets(importer.assetPath).ToArray(); + m_path = $"{Path.GetDirectoryName(assetPath)}/{Path.GetFileNameWithoutExtension(assetPath)}.Textures"; + SafeCreateDirectory(m_path); + + m_subAssets = subAssets; } - static Regex s_mimeTypeReg = new Regex("image/(?.*)$"); + static string GetExt(string mime) + { + switch (mime) + { + case "image/png": return ".png"; + case "image/jpeg": return ".jpg"; + } + throw new NotImplementedException(); + } public void Extract(GetTextureParam param) { @@ -44,11 +54,7 @@ namespace UniGLTF case GetTextureParam.OCCLUSION_PROP: { // write converted texture - targetPath = string.Format("{0}/{1}{2}", - m_path, - param.Name, - ".png" - ); + targetPath = $"{m_path}/{param.Name}.png"; File.WriteAllBytes(targetPath, subAsset.EncodeToPNG().ToArray()); break; } @@ -56,12 +62,10 @@ namespace UniGLTF default: { // write original bytes - targetPath = string.Format("{0}/{1}{2}", - m_path, - param.Name, - ".png" - ); var gltfTexture = GLTF.textures[param.Index0.Value]; + var gltfImage = GLTF.images[gltfTexture.source]; + var ext = GetExt(gltfImage.mimeType); + targetPath = $"{m_path}/{param.Name}{ext}"; File.WriteAllBytes(targetPath, GLTF.GetImageBytes(Storage, gltfTexture.source).ToArray()); break; } @@ -70,6 +74,15 @@ namespace UniGLTF Textures.Add(targetPath, param); } + public static DirectoryInfo SafeCreateDirectory(string path) + { + if (Directory.Exists(path)) + { + return null; + } + return Directory.CreateDirectory(path); + } + /// /// /// * Texture(.png etc...)をディスクに書き出す @@ -80,23 +93,9 @@ namespace UniGLTF /// /// /// - public static void ExtractTextures(ScriptedImporter importer, string dirName, Action onCompleted = null) + public static void ExtractTextures(string assetPath, Texture2D[] subAssets, Action addRemap, Action onCompleted = null) { - if (string.IsNullOrEmpty(importer.assetPath)) - { - return; - } - - var path = string.Format("{0}/{1}.{2}", - Path.GetDirectoryName(importer.assetPath), - Path.GetFileNameWithoutExtension(importer.assetPath), - dirName - ); - importer.SafeCreateDirectory(path); - - // Reload Model - var extractor = new TextureExtractor(importer); - + var extractor = new TextureExtractor(assetPath, subAssets); foreach (var material in extractor.GLTF.materials) { foreach (var x in extractor.Parser.EnumerateTextures(material)) @@ -131,11 +130,9 @@ namespace UniGLTF // remap var externalObject = AssetDatabase.LoadAssetAtPath(targetPath); - importer.AddRemap(new AssetImporter.SourceAssetIdentifier(typeof(UnityEngine.Texture2D), externalObject.name), externalObject); + addRemap(externalObject); } - AssetDatabase.ImportAsset(importer.assetPath, ImportAssetOptions.ForceUpdate); - if (onCompleted != null) { onCompleted();