diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs index 0127f308d..ac9897aaf 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs @@ -17,15 +17,42 @@ namespace UniGLTF public static void Import(ScriptedImporter scriptedImporter, AssetImportContext context, Axises reverseAxis) { #if VRM_DEVELOP - Debug.Log("OnImportAsset to " + context.assetPath); + Debug.Log("OnImportAsset to " + scriptedImporter.assetPath); #endif - var loaded = Load(context.assetPath, - scriptedImporter.GetExternalObjectMap(), - reverseAxis - ); + // + // Parse(parse glb, parser gltf json) + // + var parser = new GltfParser(); + parser.ParsePath(scriptedImporter.assetPath); - AddSubAssets(context, loaded); + // + // Import(create unity objects) + // + var externalObjectMap = scriptedImporter.GetExternalObjectMap(); + + using (var loaded = new ImporterContext(parser, null, + externalObjectMap.Where(x => x.Value != null).Select(x => (x.Value.name, x.Value)).Concat( + EnumerateExists(externalObjectMap, parser, UnityPath.FromUnityPath(scriptedImporter.assetPath).Parent)))) + { + loaded.InvertAxis = reverseAxis; + loaded.Load(); + loaded.ShowMeshes(); + + loaded.TransferOwnership(o => + { +#if VRM_DEVELOP + Debug.Log($"[{o.GetType().Name}] {o.name} will not destroy"); +#endif + + context.AddObjectToAsset(o.name, o); + if (o is GameObject) + { + // Root GameObject is main object + context.SetMainObject(loaded.Root); + } + }); + } } static IEnumerable<(string, UnityEngine.Object)> EnumerateExists(Dictionary exclude, @@ -58,83 +85,5 @@ namespace UniGLTF } } } - - /// - /// Parse して、UnityObject 化する。 - /// - /// TODO: すべての UnityObject を ImporterContext が所有する。(Disposeで削除できる) - /// - /// glbのパス - /// ScriptedImporter外部に作成済みのAssetへの参照 - /// gltf から unityへの座標変換オプション - /// - static ImporterContext Load(string assetPath, Dictionary externalObjectMap, Axises reverseAxis) - { - // - // Parse(parse glb, parser gltf json) - // - var parser = new GltfParser(); - parser.ParsePath(assetPath); - - // - // Import(create unity objects) - // - var context = new ImporterContext(parser, null, - externalObjectMap.Where(x => x.Value != null).Select(x => (x.Value.name, x.Value)).Concat( - EnumerateExists(externalObjectMap, parser, UnityPath.FromUnityPath(assetPath).Parent))); - context.InvertAxis = reverseAxis; - context.Load(); - context.ShowMeshes(); - - return context; - } - - /// - /// UnityObjectをSubAsset化する。 - /// - /// TODO: SubAsset化されると、ImporterContext から所有権を除去する - /// - /// - /// - static void AddSubAssets(AssetImportContext context, ImporterContext loaded) - { - // Texture - foreach (var info in loaded.TextureFactory.Textures) - { - if (info.IsSubAsset) - { - var texture = info.Texture; - context.AddObjectToAsset(texture.name, texture); - } - } - - // Material - foreach (var info in loaded.MaterialFactory.Materials) - { - if (info.IsSubAsset) - { - var material = info.Asset; - context.AddObjectToAsset(material.name, material); - } - } - - // Mesh - foreach (var mesh in loaded.Meshes.Select(x => x.Mesh)) - { - // all mesh is subasset - context.AddObjectToAsset(mesh.name, mesh); - } - - // Animation - foreach (var clip in loaded.AnimationClips) - { - // all animation is subasset - context.AddObjectToAsset(clip.name, clip); - } - - // Root GameObject is main object - context.AddObjectToAsset(loaded.Root.name, loaded.Root); - context.SetMainObject(loaded.Root); - } } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs index 96427cc08..ddbb1c6d4 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs @@ -235,14 +235,17 @@ namespace UniGLTF destroy(x); } AnimationClips.Clear(); + foreach (var x in Meshes) { destroy(x.Mesh); } Meshes.Clear(); - m_materialFactory.Dispose(); - m_textureFactory.Dispose(); - destroy(Root); + + // // m_materialFactory.Dispose(); + // m_textureFactory.Dispose(); + + // destroy(Root); } /// @@ -256,13 +259,21 @@ namespace UniGLTF add(mesh.Mesh); } Meshes.Clear(); - MaterialFactory.TransferOwnership(add); + TextureFactory.TransferOwnership(add); + MaterialFactory.TransferOwnership(add); + foreach (var animation in AnimationClips) { add(animation); } AnimationClips.Clear(); + + if (Root != null) + { + add(Root); + Root = null; + } } /// diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/MaterialFactory.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/MaterialFactory.cs index d655d20f5..9c683ff02 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/MaterialFactory.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/MaterialFactory.cs @@ -75,19 +75,12 @@ namespace UniGLTF List m_materials = new List(); public IReadOnlyList Materials => m_materials; - public void Dispose() - { - foreach (var x in ObjectsForSubAsset()) - { - UnityEngine.Object.DestroyImmediate(x, true); - } - } - public IEnumerable ObjectsForSubAsset() + public void Dispose() { foreach (var x in m_materials) { - yield return x.Asset; + UnityEngine.Object.DestroyImmediate(x.Asset, false); } } @@ -207,7 +200,10 @@ namespace UniGLTF { foreach (var x in m_materials) { - add(x.Asset); + if (!x.UseExternal) + { + add(x.Asset); + } } m_materials.Clear(); } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs index 341b4e896..7bc4a921d 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs @@ -199,7 +199,7 @@ namespace UniGLTF var keys = new List(); foreach (var x in m_textureCache) { - if (x.Value.IsUsed) + if (x.Value.IsUsed && !x.Value.IsExternal) { keys.Add(x.Key); add(x.Value.Texture);