UniVRM/Assets/VRM/Editor/Format/VRMImporterMenu.cs
ousttrue a5be5e1383 GltfParserの分離と、ImporterContextのインターフェース整理
使い方
* var parser = new GltfParser();
* parser.ParsePath(filePath); // ParseJson, ParseGlb 等パース系の関数
* context = new ImporterContext(parser); // parse から context を作る
* context.Load(); // もしくは LoadAsync で、Unity化する
2021-02-22 15:52:32 +09:00

71 lines
2.2 KiB
C#

using System.IO;
using UnityEditor;
using UnityEngine;
using UniGLTF;
namespace VRM
{
public static class VRMImporterMenu
{
[MenuItem(VRMVersion.MENU + "/Import", priority = 1)]
static void ImportMenu()
{
var path = EditorUtility.OpenFilePanel("open vrm", "", "vrm");
if (string.IsNullOrEmpty(path))
{
return;
}
if (Application.isPlaying)
{
// load into scene
var parser = new GltfParser();
parser.ParsePath(path);
var context = new VRMImporterContext(parser);
context.Load();
context.ShowMeshes();
context.EnableUpdateWhenOffscreen();
Selection.activeGameObject = context.Root;
}
else
{
if (path.StartsWithUnityAssetPath())
{
Debug.LogWarningFormat("disallow import from folder under the Assets");
return;
}
var assetPath = EditorUtility.SaveFilePanel("save prefab", "Assets", Path.GetFileNameWithoutExtension(path), "prefab");
if (string.IsNullOrEmpty(path))
{
return;
}
if (!assetPath.StartsWithUnityAssetPath())
{
Debug.LogWarningFormat("out of asset path: {0}", assetPath);
return;
}
// import as asset
var prefabPath = UnityPath.FromUnityPath(assetPath);
var parser = new GltfParser();
parser.ParseGlb(File.ReadAllBytes(path));
var context = new VRMImporterContext(parser);
context.ExtractImages(prefabPath);
EditorApplication.delayCall += () =>
{
//
// after textures imported
//
context.Load();
context.SaveAsAsset(prefabPath);
context.EditorDestroyRoot();
};
}
}
}
}