diff --git a/Assets/VRM/Editor/Format/VRMImporterMenu.cs b/Assets/VRM/Editor/Format/VRMImporterMenu.cs index a2fb45009..12071d70b 100644 --- a/Assets/VRM/Editor/Format/VRMImporterMenu.cs +++ b/Assets/VRM/Editor/Format/VRMImporterMenu.cs @@ -2,11 +2,6 @@ using UnityEditor; using UnityEngine; using UniGLTF; -using System; -using System.Collections.Generic; -using System.Linq; -using VRMShaders; -using Object = UnityEngine.Object; namespace VRM { @@ -22,32 +17,36 @@ namespace VRM if (Application.isPlaying) { + // import vrm to scene without asset creation ImportRuntime(path); - return; } - - if (path.StartsWithUnityAssetPath()) + else { - Debug.LogWarningFormat("disallow import from folder under the Assets"); - return; - } + // import vrm to asset + if (path.StartsWithUnityAssetPath()) + { + Debug.LogWarningFormat("disallow import from folder under the Assets"); + return; + } - var prefabPath = EditorUtility.SaveFilePanel("save prefab", "Assets", Path.GetFileNameWithoutExtension(path), "prefab"); - if (string.IsNullOrEmpty(path)) - { - return; - } + var prefabPath = EditorUtility.SaveFilePanel("save prefab", "Assets", Path.GetFileNameWithoutExtension(path), "prefab"); + if (string.IsNullOrEmpty(path)) + { + return; + } - ImportAsset(path, UnityPath.FromFullpath(prefabPath)); + vrmAssetPostprocessor.ImportVrmAndCreatePrefab(path, UnityPath.FromFullpath(prefabPath)); + } } + /// + /// load into scene + /// + /// vrm path static void ImportRuntime(string path) { - // load into scene - var data = new GlbFileParser(path).Parse(); - // VRM extension を parse します - var vrm = new VRMData(data); - using (var context = new VRMImporterContext(vrm)) + using (var data = new GlbFileParser(path).Parse()) + using (var context = new VRMImporterContext(new VRMData(data))) { var loaded = context.Load(); loaded.EnableUpdateWhenOffscreen(); @@ -55,46 +54,5 @@ namespace VRM Selection.activeGameObject = loaded.gameObject; } } - - static void ImportAsset(string path, UnityPath prefabPath) - { - if (!prefabPath.IsUnderAssetsFolder) - { - Debug.LogWarningFormat("out of asset path: {0}", prefabPath); - return; - } - - // import as asset - var data = new GlbFileParser(path).Parse(); - var vrm = new VRMData(data); - - Action> onCompleted = texturePaths => - { - // - // after textures imported - // - var map = texturePaths - .Select(x => x.LoadAsset()) - .Where(x => x != null) - .ToDictionary(x => new SubAssetKey(x), x => x as Object); - - using (var context = new VRMImporterContext(vrm, externalObjectMap: map)) - { - var editor = new VRMEditorImporterContext(context, prefabPath); - foreach (var textureInfo in editor.TextureDescriptorGenerator.Get().GetEnumerable()) - { - VRMShaders.TextureImporterConfigurator.Configure(textureInfo, context.TextureFactory.ExternalTextures); - } - var loaded = context.Load(); - editor.SaveAsAsset(loaded); - } - }; - - using (var context = new VRMImporterContext(vrm)) - { - var editor = new VRMEditorImporterContext(context, prefabPath); - editor.ConvertAndExtractImages(onCompleted); - } - } } } diff --git a/Assets/VRM/Editor/Format/vrmAssetPostprocessor.cs b/Assets/VRM/Editor/Format/vrmAssetPostprocessor.cs index 0ae4e0f24..fa328e9c9 100644 --- a/Assets/VRM/Editor/Format/vrmAssetPostprocessor.cs +++ b/Assets/VRM/Editor/Format/vrmAssetPostprocessor.cs @@ -10,9 +10,9 @@ using VRMShaders; namespace VRM { -#if !VRM_STOP_ASSETPOSTPROCESSOR public class vrmAssetPostprocessor : AssetPostprocessor { +#if !VRM_STOP_ASSETPOSTPROCESSOR static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) { foreach (string path in importedAssets) @@ -37,6 +37,7 @@ namespace VRM } } } +#endif static void ImportVrm(UnityPath vrmPath) { @@ -47,6 +48,17 @@ namespace VRM var prefabPath = vrmPath.Parent.Child(vrmPath.FileNameWithoutExtension + ".prefab"); + ImportVrmAndCreatePrefab(vrmPath.FullPath, prefabPath); + } + + public static void ImportVrmAndCreatePrefab(string vrmPath, UnityPath prefabPath) + { + if (!prefabPath.IsUnderAssetsFolder) + { + Debug.LogWarningFormat("out of asset path: {0}", prefabPath); + return; + } + /// /// これは EditorApplication.delayCall により呼び出される。 /// @@ -62,7 +74,7 @@ namespace VRM .ToDictionary(x => new SubAssetKey(x), x => x as UnityEngine.Object); // 確実に Dispose するために敢えて再パースしている - using (var data = new GlbFileParser(vrmPath.FullPath).Parse()) + using (var data = new GlbFileParser(vrmPath).Parse()) using (var context = new VRMImporterContext(new VRMData(data), externalObjectMap: map)) { var editor = new VRMEditorImporterContext(context, prefabPath); @@ -73,16 +85,16 @@ namespace VRM var loaded = context.Load(); editor.SaveAsAsset(loaded); } + }; - // extract texture images - using (var data = new GlbFileParser(vrmPath.FullPath).Parse()) + using (var data = new GlbFileParser(vrmPath).Parse()) using (var context = new VRMImporterContext(new VRMData(data))) { var editor = new VRMEditorImporterContext(context, prefabPath); + // extract texture images editor.ConvertAndExtractImages(onCompleted); } } } -#endif }