Merge pull request #1635 from ousttrue/fix/vrm_import_using_dispose

Fixed dispose on vrm import call from VRMImporterMenu
This commit is contained in:
ousttrue 2022-05-09 15:30:03 +09:00 committed by GitHub
commit a62044bfa3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 68 deletions

View File

@ -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));
}
}
/// <summary>
/// load into scene
/// </summary>
/// <param name="path">vrm path</param>
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<IEnumerable<UnityPath>> onCompleted = texturePaths =>
{
//
// after textures imported
//
var map = texturePaths
.Select(x => x.LoadAsset<Texture2D>())
.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);
}
}
}
}

View File

@ -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;
}
/// <summary>
/// これは 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
}