From d6c4a2bea6b15eb9a4238acffff41d7d8aae2270 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 4 Mar 2022 19:45:55 +0900 Subject: [PATCH] =?UTF-8?q?prefab=20=E4=BD=9C=E6=88=90=E5=85=88=E3=82=92?= =?UTF-8?q?=E6=8C=87=E5=AE=9A=E3=81=99=E3=82=8B=20dialog=20=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MeshIntegratorEditor.cs | 63 +++++++------------ .../MeshIntegratorWizard.cs | 17 ++++- Assets/VRM/Editor/VrmTopMenu.cs | 6 -- 3 files changed, 40 insertions(+), 46 deletions(-) diff --git a/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorEditor.cs b/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorEditor.cs index 87a7f1665..5cc142875 100644 --- a/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorEditor.cs +++ b/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorEditor.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -12,7 +11,8 @@ using Object = UnityEngine.Object; namespace VRM { /// - /// 複数のメッシュをまとめて、BlendShapeClipに変更を反映する + /// 複数のメッシュを統合する。 + /// VRM の場合、BlendShapeClip の調整が必用なのでこれも実行する。 /// [DisallowMultipleComponent] public static class MeshIntegratorEditor @@ -26,28 +26,11 @@ namespace VRM SkinnedMeshUtility.IsPrefab(Selection.activeObject); } - public static void Integrate() - { - var go = Selection.activeObject as GameObject; - - Integrate(go); - } - - //[MenuItem("Assets/fooa", false)] - //private static void Foo() - //{ - // var go = Selection.activeObject as GameObject; - - // Debug.Log(SkinnedMeshUtility.IsPrefab(go)); - //} - - - public static List Integrate(GameObject prefab) + public static List Integrate(GameObject prefab, UniGLTF.UnityPath writeAssetPath) { Undo.RecordObject(prefab, "Mesh Integration"); var instance = SkinnedMeshUtility.InstantiatePrefab(prefab); - var clips = new List(); var proxy = instance.GetComponent(); if (proxy != null && proxy.BlendShapeAvatar != null) @@ -65,12 +48,29 @@ namespace VRM // Execute var results = VRMMeshIntegratorUtility.Integrate(instance, clips); - foreach (var res in results) + foreach (var result in results) { - if (res.IntegratedRenderer == null) continue; + if (result.IntegratedRenderer == null) continue; - SaveMeshAsset(res.IntegratedRenderer.sharedMesh, instance, res.IntegratedRenderer.gameObject.name); - Undo.RegisterCreatedObjectUndo(res.IntegratedRenderer.gameObject, "Integrate Renderers"); + var assetPath = writeAssetPath.Parent.Child($"{result.IntegratedRenderer.gameObject.name}{ASSET_SUFFIX}"); + Debug.LogFormat("CreateAsset: {0}", assetPath); + assetPath.CreateAsset(result.IntegratedRenderer.sharedMesh); + Undo.RegisterCreatedObjectUndo(result.IntegratedRenderer.gameObject, "Integrate Renderers"); + } + + // Apply to Prefab + var prefabPath = UniGLTF.UnityPath.FromUnityPath(AssetDatabase.GetAssetPath(prefab)); + if (prefabPath.Equals(writeAssetPath)) + { + SkinnedMeshUtility.ApplyChangesToPrefab(instance); + } + else + { + PrefabUtility.SaveAsPrefabAsset(instance, writeAssetPath.Value, out bool success); + if (!success) + { + throw new System.Exception($"PrefabUtility.SaveAsPrefabAsset: {writeAssetPath}"); + } } // destroy source renderers @@ -88,9 +88,6 @@ namespace VRM renderer.gameObject.SetActive(false); } } - - // Apply to Prefab - SkinnedMeshUtility.ApplyChangesToPrefab(instance); Object.DestroyImmediate(instance); return results; @@ -143,17 +140,5 @@ namespace VRM assetPath = assetPath.Replace(@"\", @"/"); return assetPath; } - - private static void SaveMeshAsset(Mesh mesh, GameObject go, string name) - { - var assetPath = Path.Combine(GetRootPrefabPath(go), string.Format("{0}{1}", - name, - ASSET_SUFFIX - )); - - Debug.LogFormat("CreateAsset: {0}", assetPath); - AssetDatabase.CreateAsset(mesh, assetPath); - } - } } diff --git a/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorWizard.cs b/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorWizard.cs index 672f22332..70b0b4816 100644 --- a/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorWizard.cs +++ b/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorWizard.cs @@ -5,6 +5,7 @@ using System.Linq; using System; using System.Collections.Generic; using UniGLTF.MeshUtility; +using System.IO; namespace VRM { @@ -147,7 +148,21 @@ namespace VRM return; } - integrationResults = MeshIntegratorEditor.Integrate(m_root).ToArray(); + var prefabPath = AssetDatabase.GetAssetPath(m_root); + var path = EditorUtility.SaveFilePanel("save prefab", Path.GetDirectoryName(prefabPath), m_root.name, "prefab"); + if (string.IsNullOrEmpty(path)) + { + return; + } + + var assetPath = UniGLTF.UnityPath.FromFullpath(path); + if (!assetPath.IsUnderAssetsFolder) + { + Debug.LogWarning($"{path} is not asset path"); + return; + } + + integrationResults = MeshIntegratorEditor.Integrate(m_root, assetPath).ToArray(); } void OnWizardCreate() diff --git a/Assets/VRM/Editor/VrmTopMenu.cs b/Assets/VRM/Editor/VrmTopMenu.cs index 0b96ce481..a79dc302a 100644 --- a/Assets/VRM/Editor/VrmTopMenu.cs +++ b/Assets/VRM/Editor/VrmTopMenu.cs @@ -27,12 +27,6 @@ namespace VRM [MenuItem(UserMenuPrefix + "/Freeze T-Pose", priority = 20)] private static void FreezeTPose() => VRMHumanoidNormalizerMenu.Normalize(); - [MenuItem(UserMenuPrefix + "/MeshIntegration", validate = true)] - private static bool MeshIntegrationValidation() => MeshIntegratorEditor.IntegrateValidation(); - - [MenuItem(UserMenuPrefix + "/MeshIntegration", priority = 21)] - private static void MeshIntegration() => MeshIntegratorEditor.Integrate(); - [MenuItem(UserMenuPrefix + "/MeshIntegratorWizard", priority = 21)] private static void OpenMeshIntegratorWizard() => MeshIntegratorWizard.CreateWizard();