restore UniGLTF/Import(gltf, glb, zip) menu

This commit is contained in:
ousttrue
2021-09-17 19:09:49 +09:00
parent 520f1a6e62
commit 1314561c54
2 changed files with 92 additions and 12 deletions

View File

@@ -1,3 +1,4 @@
using System.IO;
using UnityEditor;
using UnityEngine;
@@ -9,9 +10,87 @@ namespace UniGLTF
public static class Menu
{
#region UniGLTF
const string MENU_KEY = UniGLTFVersion.MENU + "/Export " + UniGLTFVersion.UNIGLTF_VERSION;
[MenuItem(UniGLTFVersion.MENU + "/Import(gltf, glb, zip)", priority = 1)]
public static void ImportMenu()
{
var path = EditorUtility.OpenFilePanel("open glb", "", "gltf,glb,zip");
if (string.IsNullOrEmpty(path))
{
return;
}
[MenuItem(MENU_KEY, false, 0)]
if (Application.isPlaying)
{
//
// load into scene
//
var data = new AutoGltfFileParser(path).Parse();
using (var context = new ImporterContext(data))
{
var loaded = context.Load();
loaded.ShowMeshes();
Selection.activeGameObject = loaded.gameObject;
}
return;
}
//
// save as asset
//
if (path.StartsWithUnityAssetPath())
{
Debug.LogWarningFormat("disallow import from folder under the Assets");
return;
}
var ext = Path.GetExtension(path).ToLower();
var assetPath = EditorUtility.SaveFilePanel("save prefab", "Assets", Path.GetFileNameWithoutExtension(path), ext.Substring(1));
if (string.IsNullOrEmpty(assetPath))
{
return;
}
// copy
var bytes = File.ReadAllBytes(path);
File.WriteAllBytes(assetPath, bytes);
if (ext == ".gltf")
{
// copy associated files
var src_dir = Path.GetDirectoryName(path);
var dst_dir = Path.GetDirectoryName(assetPath);
var data = new GltfFileWithResourceFilesParser(path, bytes).Parse();
foreach (var buffer in data.GLTF.buffers)
{
if (!string.IsNullOrEmpty(buffer.uri))
{
var src_path = Path.Combine(src_dir, buffer.uri);
var src_bytes = File.ReadAllBytes(src_path);
var dst_path = Path.Combine(dst_dir, buffer.uri);
File.WriteAllBytes(dst_path, src_bytes);
UnityPath.FromFullpath(dst_path).ImportAsset();
}
}
foreach (var buffer in data.GLTF.images)
{
if (!string.IsNullOrEmpty(buffer.uri))
{
var src_path = Path.Combine(src_dir, buffer.uri);
var src_bytes = File.ReadAllBytes(src_path);
var dst_path = Path.Combine(dst_dir, buffer.uri);
File.WriteAllBytes(dst_path, src_bytes);
UnityPath.FromFullpath(dst_path).ImportAsset();
}
}
}
// import as asset
var unitypath = UnityPath.FromFullpath(assetPath);
unitypath.ImportAsset();
var asset = unitypath.LoadAsset<GameObject>();
Selection.activeObject = asset;
}
[MenuItem(UniGLTFVersion.MENU + "/Export " + UniGLTFVersion.UNIGLTF_VERSION, false, 0)]
private static void ExportFromMenu()
{
var window = (GltfExportWindow)GltfExportWindow.GetWindow(typeof(GltfExportWindow));

View File

@@ -12,25 +12,26 @@ namespace UniGLTF
{
private readonly string _gltfFilePath;
private readonly string _gltfRootPath;
public GltfFileWithResourceFilesParser(string gltfFilePath)
private readonly byte[] _bytes;
public GltfFileWithResourceFilesParser(string gltfFilePath) : this(gltfFilePath, File.ReadAllBytes(gltfFilePath))
{
}
public GltfFileWithResourceFilesParser(string gltfFilePath, byte[] bytes)
{
if (!File.Exists(gltfFilePath))
{
throw new ArgumentException($"no file: {gltfFilePath}");
}
_gltfFilePath = gltfFilePath;
_gltfRootPath = Path.GetDirectoryName(gltfFilePath);
_bytes = bytes;
}
public GltfData Parse()
{
var binary = File.ReadAllBytes(_gltfFilePath);
return GlbLowLevelParser.ParseGltf(
_gltfFilePath,
Encoding.UTF8.GetString(binary),
Encoding.UTF8.GetString(_bytes),
new List<GlbChunk>(), // .gltf file has no chunks.
new FileSystemStorage(_gltfRootPath), // .gltf file has resource path at file system.
new MigrationFlags()