diff --git a/Scripts/Format/Editor/vrmAssetPostprocessor.cs b/Scripts/Format/Editor/vrmAssetPostprocessor.cs index b7c5a5f06..6f4e07eea 100644 --- a/Scripts/Format/Editor/vrmAssetPostprocessor.cs +++ b/Scripts/Format/Editor/vrmAssetPostprocessor.cs @@ -1,7 +1,7 @@ -using System; -using System.IO; +using System.IO; +using System.Linq; +using UniGLTF; using UnityEditor; -using UnityEngine; namespace VRM @@ -16,32 +16,45 @@ namespace VRM if (ext == ".vrm") { var context = new VRMImporterContext(path); - try + + context.ParseVrm(File.ReadAllBytes(context.Path)); + + // + // https://answers.unity.com/questions/647615/how-to-update-import-settings-for-newly-created-as.html + // + for (int i = 0; i < context.GLTF.textures.Count; ++i) { - VRMImporter.LoadFromPath(context); - - /* - var prefabPath = String.Format("{0}/{1}.prefab", - Path.GetDirectoryName(path), - Path.GetFileNameWithoutExtension(path)); - - VRMAssetWriter.SaveAsPrefab(context.Root, prefabPath); - - var prefab = AssetDatabase.LoadAssetAtPath(prefabPath.ToUnityRelativePath()); - Selection.activeObject = prefab; - */ - - context.SaveAsAsset(); - context.Destroy(false); - } - catch(Exception ex) - { - Debug.LogError(ex); - if (context != null) + var x = context.GLTF.textures[i]; + var image = context.GLTF.images[x.source]; + if (string.IsNullOrEmpty(image.uri)) { - context.Destroy(true); + // glb buffer + var folder = context.GetAssetFolder(".Textures").AssetPathToFullPath(); + if (!Directory.Exists(folder)) + { + UnityEditor.AssetDatabase.CreateFolder(context.GLTF.baseDir, Path.GetFileNameWithoutExtension(context.Path) + ".Textures"); + //Directory.CreateDirectory(folder); + } + + var textureName = string.IsNullOrEmpty(image.extra.name) ? string.Format("buffer#{0:00}", i) : image.extra.name; + var png = Path.Combine(folder, textureName + ".png"); + var byteSegment = context.GLTF.GetViewBytes(image.bufferView); + File.WriteAllBytes(png, byteSegment.ToArray()); + var assetPath = png.ToUnityRelativePath(); + //Debug.LogFormat("import asset {0}", assetPath); + UnityEditor.AssetDatabase.ImportAsset(assetPath, UnityEditor.ImportAssetOptions.ForceUpdate); + UnityEditor.AssetDatabase.Refresh(); + image.uri = assetPath.Substring(context.GLTF.baseDir.Length + 1); } } + + EditorApplication.delayCall += () => + { + // delay and can import png texture + VRMImporter.LoadFromBytes(context); + context.SaveAsAsset(); + context.Destroy(false); + }; } } }