public static List <MeshUtility.MeshIntegrationResult> Integrate(GameObject prefab) { Undo.RecordObject(prefab, "Mesh Integration"); var instance = SkinnedMeshUtility.InstantiatePrefab(prefab); var clips = new List <BlendShapeClip>(); var proxy = instance.GetComponent <VRMBlendShapeProxy>(); if (proxy != null && proxy.BlendShapeAvatar != null) { clips = proxy.BlendShapeAvatar.Clips; } foreach (var clip in clips) { Undo.RecordObject(clip, "Mesh Integration"); } // Backup Exists BackupVrmPrefab(prefab); // Execute var results = VRMMeshIntegratorUtility.Integrate(instance, clips); foreach (var res in results) { if (res.IntegratedRenderer == null) { continue; } SaveMeshAsset(res.IntegratedRenderer.sharedMesh, instance, res.IntegratedRenderer.gameObject.name); Undo.RegisterCreatedObjectUndo(res.IntegratedRenderer.gameObject, "Integrate Renderers"); } // destroy source renderers foreach (var res in results) { foreach (var renderer in res.SourceSkinnedMeshRenderers) { Undo.RecordObject(renderer.gameObject, "Deactivate old renderer"); renderer.gameObject.SetActive(false); } foreach (var renderer in res.SourceMeshRenderers) { Undo.RecordObject(renderer.gameObject, "Deactivate old renderer"); renderer.gameObject.SetActive(false); } } // Apply to Prefab SkinnedMeshUtility.ApplyChangesToPrefab(instance); Object.DestroyImmediate(instance); return(results); }
private static void BackupVrmPrefab(GameObject rootPrefab) { var proxy = rootPrefab.GetComponent <VRMBlendShapeProxy>(); var srcAvatar = proxy.BlendShapeAvatar; var dstAvatar = (BlendShapeAvatar)BackupAsset(srcAvatar, rootPrefab); var clipMapper = srcAvatar.Clips.ToDictionary(x => x, x => (BlendShapeClip)BackupAsset(x, rootPrefab)); dstAvatar.Clips = clipMapper.Values.ToList(); var dstPrefab = BackupAsset(rootPrefab, rootPrefab); var dstInstance = SkinnedMeshUtility.InstantiatePrefab(dstPrefab); dstInstance.GetComponent <VRMBlendShapeProxy>().BlendShapeAvatar = dstAvatar; SkinnedMeshUtility.ApplyChangesToPrefab(dstInstance); Object.DestroyImmediate(dstInstance); }