From e5142dff17dc7fef4893c48f2d57116c94f41f62 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 18 May 2022 15:33:54 +0900 Subject: [PATCH] prefab search for preview --- Assets/VRM/Editor/BlendShape/PreviewEditor.cs | 36 +----------- .../VRM/Runtime/BlendShape/BlendShapeClip.cs | 56 +++++++++++++++---- 2 files changed, 45 insertions(+), 47 deletions(-) diff --git a/Assets/VRM/Editor/BlendShape/PreviewEditor.cs b/Assets/VRM/Editor/BlendShape/PreviewEditor.cs index 179f2fe32..6a27374ff 100644 --- a/Assets/VRM/Editor/BlendShape/PreviewEditor.cs +++ b/Assets/VRM/Editor/BlendShape/PreviewEditor.cs @@ -89,41 +89,7 @@ namespace VRM protected virtual GameObject GetPrefab() { - var assetPath = AssetDatabase.GetAssetPath(target); - if (string.IsNullOrEmpty(assetPath)) - { - return null; - } - - var prefab = AssetDatabase.LoadAssetAtPath(assetPath); - // search prefab if nothing - if (prefab == null && 0 < (target as BlendShapeAvatar).Clips.Count) - { - prefab = (target as BlendShapeAvatar).Clips[0].Prefab; - } - // once more, with string-based method - if (prefab == null) - { - var parent = UniGLTF.UnityPath.FromUnityPath(assetPath).Parent; - var prefabPath = parent.Parent.Child(parent.FileNameWithoutExtension + ".prefab"); - prefab = UnityEditor.AssetDatabase.LoadAssetAtPath(prefabPath.Value); - } - // once more, with string-based method. search same folder *.prefab - if (prefab == null) - { - var parent = UniGLTF.UnityPath.FromUnityPath(assetPath).Parent; - foreach (var file in Directory.EnumerateFiles(parent.FullPath)) - { - var ext = Path.GetExtension(file).ToLower(); - if (ext == ".prefab") - { - var prefabPath = UniGLTF.UnityPath.FromFullpath(file); - prefab = UnityEditor.AssetDatabase.LoadAssetAtPath(prefabPath.Value); - break; - } - } - } - return prefab; + return BlendShapeClip.VrmPrefabSearch(target); } protected virtual void OnEnable() diff --git a/Assets/VRM/Runtime/BlendShape/BlendShapeClip.cs b/Assets/VRM/Runtime/BlendShape/BlendShapeClip.cs index 038f16c9e..a2ffc30b3 100644 --- a/Assets/VRM/Runtime/BlendShape/BlendShapeClip.cs +++ b/Assets/VRM/Runtime/BlendShape/BlendShapeClip.cs @@ -1,6 +1,10 @@ using System; +using System.IO; using System.Linq; using UnityEngine; +#if UNITY_EDITOR +using UnityEditor; +#endif namespace VRM @@ -76,6 +80,45 @@ namespace VRM public class BlendShapeClip : ScriptableObject { #if UNITY_EDITOR + /// + /// Inspector preview 用の prefab をがんばってサーチする + /// + /// + /// + public static GameObject VrmPrefabSearch(UnityEngine.Object target) + { + var assetPath = AssetDatabase.GetAssetPath(target); + if (string.IsNullOrEmpty(assetPath)) + { + return null; + } + + var prefab = AssetDatabase.LoadAssetAtPath(assetPath); + // once more, with string-based method + if (prefab == null) + { + var parent = UniGLTF.UnityPath.FromUnityPath(assetPath).Parent; + var prefabPath = parent.Parent.Child(parent.FileNameWithoutExtension + ".prefab"); + prefab = UnityEditor.AssetDatabase.LoadAssetAtPath(prefabPath.Value); + } + // once more, with string-based method. search same folder *.prefab + if (prefab == null) + { + var parent = UniGLTF.UnityPath.FromUnityPath(assetPath).Parent; + foreach (var file in Directory.EnumerateFiles(parent.FullPath)) + { + var ext = Path.GetExtension(file).ToLower(); + if (ext == ".prefab") + { + var prefabPath = UniGLTF.UnityPath.FromFullpath(file); + prefab = UnityEditor.AssetDatabase.LoadAssetAtPath(prefabPath.Value); + break; + } + } + } + return prefab; + } + /// /// Preview 用のObject参照 /// @@ -88,18 +131,7 @@ namespace VRM { if (m_prefab == null) { - var assetPath = UnityEditor.AssetDatabase.GetAssetPath(this); - if (!string.IsNullOrEmpty(assetPath)) - { - // if asset is subasset of prefab - m_prefab = UnityEditor.AssetDatabase.LoadAssetAtPath(assetPath); - if (m_prefab == null) - { - var parent = UniGLTF.UnityPath.FromAsset(this).Parent; - var prefabPath = parent.Parent.Child(parent.FileNameWithoutExtension + ".prefab"); - m_prefab = UnityEditor.AssetDatabase.LoadAssetAtPath(prefabPath.Value); - } - } + m_prefab = VrmPrefabSearch(this); } return m_prefab; }