diff --git a/Assets/UniGLTF/Editor/MeshUtility/MeshUtilityDialog.cs b/Assets/UniGLTF/Editor/MeshUtility/MeshUtilityDialog.cs index a2ae3207a..3a35821f2 100644 --- a/Assets/UniGLTF/Editor/MeshUtility/MeshUtilityDialog.cs +++ b/Assets/UniGLTF/Editor/MeshUtility/MeshUtilityDialog.cs @@ -199,7 +199,11 @@ namespace UniGLTF.MeshUtility { try { - var (results, created) = MeshUtility.Process(_exportTarget, context.Instance); + // prefab が instantiate されていた場合に + // Mesh統合設定を instantiate に置き換える + var groupCopy = MeshUtility.CopyInstantiate(_exportTarget, context.Instance); + + var (results, created) = MeshUtility.Process(context.Instance, groupCopy); WriteAssets(context.Instance, context.AssetFolder, results); } @@ -216,7 +220,7 @@ namespace UniGLTF.MeshUtility { using (var context = new UndoContext("MeshUtility", _exportTarget)) { - var (results, created) = MeshUtility.Process(_exportTarget, null); + var (results, created) = MeshUtility.Process(_exportTarget, MeshUtility.MeshIntegrationGroups); foreach (var go in created) { diff --git a/Assets/UniGLTF/Runtime/MeshUtility/GltfMeshUtility.cs b/Assets/UniGLTF/Runtime/MeshUtility/GltfMeshUtility.cs index adad8582c..bffca8e6d 100644 --- a/Assets/UniGLTF/Runtime/MeshUtility/GltfMeshUtility.cs +++ b/Assets/UniGLTF/Runtime/MeshUtility/GltfMeshUtility.cs @@ -99,40 +99,6 @@ namespace UniGLTF.MeshUtility }); } - void RemoveComponent(T c) where T : Component - { - if (c == null) - { - return; - } - var t = c.transform; - if (Application.isPlaying) - { - GameObject.Destroy(c); - } - else - { - GameObject.DestroyImmediate(c); - } - - if (t.childCount == 0) - { - var list = t.GetComponents(); - // Debug.Log($"{list[0].GetType().Name}"); - if (list.Length == 1 && list[0] == t) - { - if (Application.isPlaying) - { - GameObject.Destroy(t.gameObject); - } - else - { - GameObject.DestroyImmediate(t.gameObject); - } - } - } - } - static GameObject GetOrCreateEmpty(GameObject go, string name) { foreach (var child in go.transform.GetChildren()) @@ -156,7 +122,7 @@ namespace UniGLTF.MeshUtility /// MeshIntegrationGroup を作ったとき root /// go が prefab だった場合に instance されたもの /// - protected virtual IEnumerable CopyInstantiate(GameObject go, GameObject instance) + public virtual IEnumerable CopyInstantiate(GameObject go, GameObject instance) { if (instance == null) { @@ -174,9 +140,9 @@ namespace UniGLTF.MeshUtility } } - public virtual (List, List) Process(GameObject _go, GameObject _instance) + public virtual (List, List) Process( + GameObject target, IEnumerable groupCopy) { - var target = _instance ?? _go; if (FreezeBlendShape || FreezeRotation || FreezeScaling) { // MeshをBakeする @@ -188,10 +154,6 @@ namespace UniGLTF.MeshUtility BoneNormalizer.Replace(target, newMesh, FreezeRotation, FreezeScaling); } - // prefab が instantiate されていた場合に - // Mesh統合設定を instantiate に置き換える - var groupCopy = CopyInstantiate(_go, _instance); - var newList = new List(); var empty = GetOrCreateEmpty(target, "mesh"); @@ -212,24 +174,29 @@ namespace UniGLTF.MeshUtility { foreach (var r in result.SourceMeshRenderers) { - RemoveComponent(r); + if (Application.isPlaying) + { + GameObject.Destroy(r.gameObject.GetComponent()); + GameObject.Destroy(r); + } + else + { + GameObject.DestroyImmediate(r.gameObject.GetComponent()); + GameObject.DestroyImmediate(r); + } } foreach (var r in result.SourceSkinnedMeshRenderers) { - RemoveComponent(r); + if (Application.isPlaying) + { + GameObject.Destroy(r); + } + else + { + GameObject.DestroyImmediate(r); + } } } - // foreach (var result in results) - // { - // foreach (var renderer in result.SourceMeshRenderers) - // { - // GameObject.DestroyImmediate(renderer); - // } - // foreach (var renderer in result.SourceSkinnedMeshRenderers) - // { - // GameObject.DestroyImmediate(renderer); - // } - // } MeshIntegrationGroups.Clear(); diff --git a/Assets/VRM/Runtime/SkinnedMeshUtility/VrmMeshUtility.cs b/Assets/VRM/Runtime/SkinnedMeshUtility/VrmMeshUtility.cs index 161f0fddd..37a9abdf8 100644 --- a/Assets/VRM/Runtime/SkinnedMeshUtility/VrmMeshUtility.cs +++ b/Assets/VRM/Runtime/SkinnedMeshUtility/VrmMeshUtility.cs @@ -10,7 +10,7 @@ namespace VRM public class VrmMeshUtility : UniGLTF.MeshUtility.GltfMeshUtility { bool _generateFirstPerson = false; - protected override IEnumerable CopyInstantiate(GameObject go, GameObject instance) + public override IEnumerable CopyInstantiate(GameObject go, GameObject instance) { var copy = new List(); _generateFirstPerson = false; @@ -89,9 +89,10 @@ namespace VRM /// /// glTF に比べて Humanoid や FirstPerson の処理が追加される /// - public override (List, List) Process(GameObject go, GameObject instance) + public override (List, List) Process( + GameObject target, IEnumerable copyGroup) { - _vrmInstance = go.GetComponent(); + _vrmInstance = target.GetComponent(); if (_vrmInstance == null) { throw new ArgumentException(); @@ -102,7 +103,7 @@ namespace VRM throw new NotImplementedException(); // 必用? - var animator = go.GetComponent(); + var animator = target.GetComponent(); var newAvatar = AvatarDescription.RecreateAvatar(animator); animator.avatar = newAvatar; } @@ -110,11 +111,11 @@ namespace VRM // TODO: update: spring // TODO: update: constraint // TODO: update: firstPerson offset - var (list, newList) = base.Process(go, instance); + var (list, newList) = base.Process(target, copyGroup); if (FreezeBlendShape || FreezeRotation || FreezeScaling) { - var animator = go.GetComponent(); + var animator = target.GetComponent(); var newAvatar = AvatarDescription.RecreateAvatar(animator); animator.avatar = newAvatar; } diff --git a/Assets/VRM10/Runtime/MeshUtility/Vrm10MeshUtility.cs b/Assets/VRM10/Runtime/MeshUtility/Vrm10MeshUtility.cs index e9caabcef..c106b6c28 100644 --- a/Assets/VRM10/Runtime/MeshUtility/Vrm10MeshUtility.cs +++ b/Assets/VRM10/Runtime/MeshUtility/Vrm10MeshUtility.cs @@ -10,7 +10,7 @@ namespace UniVRM10 public class Vrm10MeshUtility : UniGLTF.MeshUtility.GltfMeshUtility { bool _generateFirstPerson = false; - protected override IEnumerable CopyInstantiate(GameObject go, GameObject instance) + public override IEnumerable CopyInstantiate(GameObject go, GameObject instance) { var copy = new List(); _generateFirstPerson = false; @@ -93,9 +93,10 @@ namespace UniVRM10 /// /// glTF に比べて Humanoid や FirstPerson の処理が追加される /// - public override (List, List) Process(GameObject go, GameObject instance) + public override (List, List) Process( + GameObject target, IEnumerable groupCopy) { - _vrmInstance = go.GetComponent(); + _vrmInstance = target.GetComponent(); if (_vrmInstance == null) { throw new ArgumentException(); @@ -106,7 +107,7 @@ namespace UniVRM10 throw new NotImplementedException(); // 必用? - var animator = go.GetComponent(); + var animator = target.GetComponent(); var newAvatar = AvatarDescription.RecreateAvatar(animator); animator.avatar = newAvatar; } @@ -114,11 +115,11 @@ namespace UniVRM10 // TODO: update: spring // TODO: update: constraint // TODO: update: firstPerson offset - var (list, newList) = base.Process(go, instance); + var (list, newList) = base.Process(target, groupCopy); if (FreezeBlendShape || FreezeRotation || FreezeScaling) { - var animator = go.GetComponent(); + var animator = target.GetComponent(); var newAvatar = AvatarDescription.RecreateAvatar(animator); animator.avatar = newAvatar; }