add EditorProgress for GltfExportWindow

This commit is contained in:
ousttrue
2022-01-17 18:47:24 +09:00
parent 95fb2eba1d
commit 40d41cd321
6 changed files with 114 additions and 26 deletions

View File

@@ -0,0 +1,13 @@
using System;
using UnityEditor;
namespace UniGLTF
{
public class EditorProgress : IProgress<ExportProgress>
{
public void Report(ExportProgress value)
{
EditorUtility.DisplayProgressBar(value.Title, value.Message, value.Progress);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 354f802e5f3f61145bafeda8e58307b6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -100,40 +100,51 @@ namespace UniGLTF
default: throw new System.Exception();
}
var data = new ExportingGltfData();
using (var exporter = new gltfExporter(data, Settings))
var progress = 0;
EditorUtility.DisplayProgressBar("export gltf", path, progress);
try
{
exporter.Prepare(State.ExportRoot);
exporter.Export(new EditorTextureSerializer());
}
if (isGlb)
{
var bytes = data.ToGlbBytes();
File.WriteAllBytes(path, bytes);
}
else
{
var (json, buffer0) = data.ToGltf(path);
var data = new ExportingGltfData();
using (var exporter = new gltfExporter(data, Settings, new EditorProgress()))
{
// write JSON without BOM
var encoding = new System.Text.UTF8Encoding(false);
File.WriteAllText(path, json, encoding);
exporter.Prepare(State.ExportRoot);
exporter.Export(new EditorTextureSerializer());
}
if (isGlb)
{
// write to buffer0 local folder
var dir = Path.GetDirectoryName(path);
var bufferPath = Path.Combine(dir, buffer0.uri);
File.WriteAllBytes(bufferPath, data.BinBytes.ToArray());
var bytes = data.ToGlbBytes();
File.WriteAllBytes(path, bytes);
}
}
else
{
var (json, buffer0) = data.ToGltf(path);
if (path.StartsWithUnityAssetPath())
{
// write JSON without BOM
var encoding = new System.Text.UTF8Encoding(false);
File.WriteAllText(path, json, encoding);
}
{
// write to buffer0 local folder
var dir = Path.GetDirectoryName(path);
var bufferPath = Path.Combine(dir, buffer0.uri);
File.WriteAllBytes(bufferPath, data.BinBytes.ToArray());
}
}
if (path.StartsWithUnityAssetPath())
{
AssetDatabase.ImportAsset(path.ToUnityRelativePath());
AssetDatabase.Refresh();
}
}
finally
{
AssetDatabase.ImportAsset(path.ToUnityRelativePath());
AssetDatabase.Refresh();
EditorUtility.ClearProgressBar();
}
}
}

View File

@@ -0,0 +1,25 @@
using System;
namespace UniGLTF
{
public readonly struct ExportProgress
{
public readonly string Title;
public readonly string Message;
public readonly float Progress;
public ExportProgress(string title, string message, float progress)
{
Title = title;
Message = message;
Progress = progress;
}
}
public class NullProgress : IProgress<ExportProgress>
{
public void Report(ExportProgress value)
{
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2235603b34ed9d045b81273d88806b51
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -68,7 +68,18 @@ namespace UniGLTF
GltfExportSettings m_settings;
public gltfExporter(ExportingGltfData data, GltfExportSettings settings)
IProgress<ExportProgress> m_progress;
void ReportProgress(string msg, float progress)
{
if (m_progress == null)
{
return;
}
m_progress.Report(new ExportProgress("gltfExporter", msg, progress));
}
public gltfExporter(ExportingGltfData data, GltfExportSettings settings, IProgress<ExportProgress> progress = null)
{
_data = data;
@@ -113,6 +124,8 @@ namespace UniGLTF
// should throw ?
Debug.LogError("root mesh is not exported");
}
ReportProgress("prepared", 0.1f);
}
public void Dispose()
@@ -233,6 +246,7 @@ namespace UniGLTF
uniqueUnityMeshes.GetInfo(Nodes, m_settings);
#region Materials and Textures
ReportProgress("Materials and Textures", 0.2f);
Materials = uniqueUnityMeshes.GetUniqueMaterials().ToList();
_textureExporter = new TextureExporter(textureSerializer);
@@ -242,6 +256,7 @@ namespace UniGLTF
#endregion
#region Meshes
ReportProgress("Meshes", 0.4f);
MeshBlendShapeIndexMap = new Dictionary<Mesh, Dictionary<int, int>>();
foreach (var unityMesh in uniqueUnityMeshes)
{
@@ -265,6 +280,7 @@ namespace UniGLTF
#endregion
#region Nodes and Skins
ReportProgress("Nodes and Skins", 0.8f);
var skins = uniqueUnityMeshes
.SelectMany(x => x.Renderers)
.Where(x => x.Item1 is SkinnedMeshRenderer && x.UniqueBones != null)
@@ -314,6 +330,7 @@ namespace UniGLTF
#if UNITY_EDITOR
#region Animations
ReportProgress("Animations", 0.9f);
var clips = new List<AnimationClip>();
var animator = Copy.GetComponent<Animator>();