From fcbea0295483f34966839a67d21c6e6e75226dee Mon Sep 17 00:00:00 2001 From: ousttrue Date: Sat, 26 Jan 2019 17:10:08 +0900 Subject: [PATCH 1/2] Load Material when importer avoid overwrite #142 --- .../VRM/UniGLTF/Scripts/IO/ImporterContext.cs | 48 ++++++++++++++++--- .../UniGLTF/Scripts/IO/MeshWithMaterials.cs | 2 + 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/Assets/VRM/UniGLTF/Scripts/IO/ImporterContext.cs b/Assets/VRM/UniGLTF/Scripts/IO/ImporterContext.cs index 7c9d2da87..f2054f465 100644 --- a/Assets/VRM/UniGLTF/Scripts/IO/ImporterContext.cs +++ b/Assets/VRM/UniGLTF/Scripts/IO/ImporterContext.cs @@ -804,14 +804,47 @@ namespace UniGLTF public virtual bool IsOverwrite(UnityEngine.Object o) { - if(o is Material) - { - return false; - } return true; } + public virtual bool LoadAsset(UnityPath assetPath, UnityEngine.Object o) + { + if (o is Material) + { + var loaded = assetPath.LoadAsset(); + + // replace member + for(int i=0; i< m_materials.Count; ++i) + { + if (m_materials[i] == o) + { + m_materials[i] = loaded; + break; + } + } + + // replace renderers.material + foreach(var mesh in Meshes) + { + foreach(var r in mesh.Renderers) + { + for(int i=0; i x == o ? loaded : x).ToArray(); + } + } + } + } + + return true; + } + + return false; + } + public void SaveAsAsset(UnityPath prefabPath) { ShowMeshes(); @@ -841,13 +874,14 @@ namespace UniGLTF { if (assetPath.IsFileExists) { - if (!IsOverwrite(o)) + if (LoadAsset(assetPath, o)) { - // 上書きしない - Debug.LogWarningFormat("already exists. skip {0}", assetPath); + // 上書きせずに既存のアセットからロードして置き換えた continue; } } + + // アセットとして書き込む assetPath.Parent.EnsureFolder(); assetPath.CreateAsset(o); paths.Add(assetPath); diff --git a/Assets/VRM/UniGLTF/Scripts/IO/MeshWithMaterials.cs b/Assets/VRM/UniGLTF/Scripts/IO/MeshWithMaterials.cs index 56838167b..42c2e11d4 100644 --- a/Assets/VRM/UniGLTF/Scripts/IO/MeshWithMaterials.cs +++ b/Assets/VRM/UniGLTF/Scripts/IO/MeshWithMaterials.cs @@ -8,6 +8,8 @@ namespace UniGLTF { public Mesh Mesh; public Material[] Materials; + + // 複数のノードから参照されうる public List Renderers=new List(); // SkinnedMeshRenderer or MeshRenderer } } From 7a09b5930712e839465bc8c86c161946407d7fa4 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Sat, 26 Jan 2019 17:18:36 +0900 Subject: [PATCH 2/2] Load and assign VRMBlendShapeProxy.BlendShapeAvatar when importer avoid overwrite #142 --- .../VRM/UniGLTF/Scripts/IO/ImporterContext.cs | 22 +++---------------- .../Scripts/Format/VRMImporterContext.cs | 19 ++++++++++------ 2 files changed, 15 insertions(+), 26 deletions(-) diff --git a/Assets/VRM/UniGLTF/Scripts/IO/ImporterContext.cs b/Assets/VRM/UniGLTF/Scripts/IO/ImporterContext.cs index f2054f465..82c78e51e 100644 --- a/Assets/VRM/UniGLTF/Scripts/IO/ImporterContext.cs +++ b/Assets/VRM/UniGLTF/Scripts/IO/ImporterContext.cs @@ -802,29 +802,13 @@ namespace UniGLTF } } - public virtual bool IsOverwrite(UnityEngine.Object o) - { - - return true; - } - - public virtual bool LoadAsset(UnityPath assetPath, UnityEngine.Object o) + public virtual bool AvoidOverwriteAndLoad(UnityPath assetPath, UnityEngine.Object o) { if (o is Material) { var loaded = assetPath.LoadAsset(); - // replace member - for(int i=0; i< m_materials.Count; ++i) - { - if (m_materials[i] == o) - { - m_materials[i] = loaded; - break; - } - } - - // replace renderers.material + // replace component reference foreach(var mesh in Meshes) { foreach(var r in mesh.Renderers) @@ -874,7 +858,7 @@ namespace UniGLTF { if (assetPath.IsFileExists) { - if (LoadAsset(assetPath, o)) + if (AvoidOverwriteAndLoad(assetPath, o)) { // 上書きせずに既存のアセットからロードして置き換えた continue; diff --git a/Assets/VRM/UniVRM/Scripts/Format/VRMImporterContext.cs b/Assets/VRM/UniVRM/Scripts/Format/VRMImporterContext.cs index df4ffe155..e3b8d372d 100644 --- a/Assets/VRM/UniVRM/Scripts/Format/VRMImporterContext.cs +++ b/Assets/VRM/UniVRM/Scripts/Format/VRMImporterContext.cs @@ -341,18 +341,23 @@ namespace VRM } #if UNITY_EDITOR - public override bool IsOverwrite(UnityEngine.Object o) + public override bool AvoidOverwriteAndLoad(UnityPath assetPath, UnityEngine.Object o) { if (o is BlendShapeAvatar) { - return false; - } - if (o is BlendShapeClip) - { - return false; + var loaded = assetPath.LoadAsset(); + var proxy = Root.GetComponent(); + proxy.BlendShapeAvatar = loaded; + + return true; } - return base.IsOverwrite(o); + if (o is BlendShapeClip) + { + return true; + } + + return base.AvoidOverwriteAndLoad(assetPath, o); } protected override UnityPath GetAssetPath(UnityPath prefabPath, UnityEngine.Object o)