Merge pull request #888 from ousttrue/feature/refactor_export_dialog

ExportDialogBaseなど
This commit is contained in:
PoChang007 2021-04-20 12:21:43 +09:00 committed by GitHub
commit e72bce9c46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 476 additions and 480 deletions

View File

@ -1,6 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using MeshUtility;
using UniGLTF.M17N;
using UnityEditor;
using UnityEngine;

View File

@ -1,4 +1,6 @@

using UniGLTF.M17N;
namespace UniGLTF.EditorSettingsValidator
{
public enum Messages

View File

@ -1,4 +1,5 @@
using UnityEditor;
using UniGLTF.M17N;
namespace UniGLTF.EditorSettingsValidator
{
@ -15,4 +16,4 @@ namespace UniGLTF.EditorSettingsValidator
PlayerSettings.colorSpace = UnityEngine.ColorSpace.Linear;
}
}
}
}

View File

@ -1,4 +1,5 @@
using System;
using UniGLTF.M17N;
using UnityEditor;
using UnityEngine;

View File

@ -5,6 +5,7 @@ using System.Reflection;
using UnityEngine;
using UnityEditor;
using UniGLTF;
using UniGLTF.M17N;
namespace MeshUtility
{
@ -133,7 +134,7 @@ namespace MeshUtility
_scrollPos = EditorGUILayout.BeginScrollView(_scrollPos);
EditorGUIUtility.labelWidth = 150;
// lang
Getter.OnGuiSelectLang();
LanguageGetter.OnGuiSelectLang();
_tab = TabBar.OnGUI(_tab, _tabButtonStyle, _tabButtonSize);

View File

@ -0,0 +1,176 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UniGLTF.M17N;
using UnityEditor;
using UnityEngine;
namespace UniGLTF
{
/// <summary>
/// ヒエラルキーをエクスポートするダイアログ。
///
/// * Root管理(m_state)
/// * Validation管理(Exportボタンを押せるか否か)
///
/// </summary>
public abstract class ExportDialogBase : EditorWindow
{
ExporterDialogState m_state;
protected ExporterDialogState State => m_state;
protected virtual void OnEnable()
{
Undo.willFlushUndoRecord += Repaint;
Selection.selectionChanged += Repaint;
m_state = new ExporterDialogState();
Initialize();
m_state.ExportRootChanged += (root) =>
{
Repaint();
};
m_state.ExportRoot = Selection.activeObject as GameObject;
}
protected abstract void Initialize();
void OnDisable()
{
Clear();
m_state.Dispose();
Selection.selectionChanged -= Repaint;
Undo.willFlushUndoRecord -= Repaint;
}
protected abstract void Clear();
//
// scroll
//
public delegate Vector2 BeginVerticalScrollViewFunc(Vector2 scrollPosition, bool alwaysShowVertical, GUIStyle verticalScrollbar, GUIStyle background, params GUILayoutOption[] options);
static BeginVerticalScrollViewFunc s_func;
static BeginVerticalScrollViewFunc BeginVerticalScrollView
{
get
{
if (s_func == null)
{
var methods = typeof(EditorGUILayout).GetMethods(BindingFlags.Static | BindingFlags.NonPublic).Where(x => x.Name == "BeginVerticalScrollView").ToArray();
var method = methods.First(x => x.GetParameters()[1].ParameterType == typeof(bool));
s_func = (BeginVerticalScrollViewFunc)method.CreateDelegate(typeof(BeginVerticalScrollViewFunc));
}
return s_func;
}
}
private Vector2 m_ScrollPosition;
//
// validation
//
protected abstract IEnumerable<Validator> ValidatorFactory();
void OnGUI()
{
var modified = false;
if (BeginGUI())
{
modified = DoGUI();
}
EndGUI();
if (modified)
{
State.Invalidate();
}
}
protected abstract bool DoGUI();
bool BeginGUI()
{
// ArgumentException: Getting control 1's position in a group with only 1 controls when doing repaint Aborting
// Validation により GUI の表示項目が変わる場合があるので、
// EventType.Layout と EventType.Repaint 間で内容が変わらないようしている。
if (Event.current.type == EventType.Layout)
{
State.Validate(ValidatorFactory());
}
EditorGUIUtility.labelWidth = 150;
// lang
LanguageGetter.OnGuiSelectLang();
EditorGUILayout.LabelField("ExportRoot");
{
State.ExportRoot = (GameObject)EditorGUILayout.ObjectField(State.ExportRoot, typeof(GameObject), true);
}
// Render contents using Generic Inspector GUI
m_ScrollPosition = BeginVerticalScrollView(m_ScrollPosition, false, GUI.skin.verticalScrollbar, "OL Box");
GUIUtility.GetControlID(645789, FocusType.Passive);
// validation
foreach (var v in State.Validations)
{
v.DrawGUI();
if (v.ErrorLevel == ErrorLevels.Critical)
{
// Export UI を表示しない
return false;
}
}
return true;
}
protected abstract string SaveTitle { get; }
protected abstract string SaveName { get; }
protected abstract string[] SaveExtensions { get; }
void EndGUI()
{
EditorGUILayout.EndScrollView();
//
// export button
//
// Create and Other Buttons
{
// errors
GUILayout.BeginVertical();
// GUILayout.FlexibleSpace();
{
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
GUI.enabled = State.Validations.All(x => x.CanExport);
if (GUILayout.Button("Export", GUILayout.MinWidth(100)))
{
var path = SaveFileDialog.GetPath(SaveTitle, SaveName, SaveExtensions);
if (!string.IsNullOrEmpty(path))
{
ExportPath(path);
// close
Close();
GUIUtility.ExitGUI();
}
}
GUI.enabled = true;
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();
}
GUILayout.Space(8);
}
protected abstract void ExportPath(string path);
}
}

View File

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

View File

@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using UniGLTF.Animation;
using UnityEditor;
using UnityEngine;
@ -9,7 +8,7 @@ using VRMShaders;
namespace UniGLTF
{
public class GltfExportWindow : EditorWindow
public class GltfExportWindow : ExportDialogBase
{
const string MENU_KEY = UniGLTFVersion.MENU + "/Export " + UniGLTFVersion.UNIGLTF_VERSION;
@ -22,6 +21,47 @@ namespace UniGLTF
}
private static void Export(GameObject go, string path, MeshExportSettings settings, Axises inverseAxis)
{
}
GltfExportSettings m_settings;
Editor m_settingsInspector;
protected override void Initialize()
{
m_settings = ScriptableObject.CreateInstance<GltfExportSettings>();
m_settings.InverseAxis = UniGLTFPreference.GltfIOAxis;
m_settingsInspector = Editor.CreateEditor(m_settings);
}
protected override void Clear()
{
// m_settingsInspector
UnityEditor.Editor.DestroyImmediate(m_settingsInspector);
m_settingsInspector = null;
}
protected override IEnumerable<Validator> ValidatorFactory()
{
yield return HierarchyValidator.ValidateRoot;
yield return AnimationValidator.Validate;
if (!State.ExportRoot)
{
yield break;
}
}
protected override bool DoGUI()
{
m_settings.Root = State.ExportRoot;
m_settingsInspector.OnInspectorGUI();
return true;
}
protected override string SaveTitle => "Save gltf";
protected override string SaveName => $"{State.ExportRoot.name}.glb";
protected override string[] SaveExtensions => new string[] { "glb", "gltf" };
protected override void ExportPath(string path)
{
var ext = Path.GetExtension(path).ToLower();
var isGlb = false;
@ -33,9 +73,15 @@ namespace UniGLTF
}
var gltf = new glTF();
using (var exporter = new gltfExporter(gltf, inverseAxis))
using (var exporter = new gltfExporter(gltf, m_settings.InverseAxis))
{
exporter.Prepare(go);
exporter.Prepare(State.ExportRoot);
var settings = new MeshExportSettings
{
ExportOnlyBlendShapePosition = m_settings.DropNormal,
UseSparseAccessorForMorphTarget = m_settings.Sparse,
DivideVertexBuffer = m_settings.DivideVertexBuffer,
};
exporter.Export(settings, AssetTextureUtil.IsTextureEditorAsset, AssetTextureUtil.GetTextureBytesWithMime);
}
@ -64,187 +110,7 @@ namespace UniGLTF
AssetDatabase.ImportAsset(path.ToUnityRelativePath());
AssetDatabase.Refresh();
}
}
ExporterDialogState m_state;
GltfExportSettings m_settings;
Editor m_settingsInspector;
void OnEnable()
{
// Debug.Log("OnEnable");
Undo.willFlushUndoRecord += Repaint;
Selection.selectionChanged += Repaint;
m_settings = ScriptableObject.CreateInstance<GltfExportSettings>();
m_settingsInspector = Editor.CreateEditor(m_settings);
m_state = new ExporterDialogState();
m_state.ExportRootChanged += (root) =>
{
Repaint();
};
m_state.ExportRoot = Selection.activeObject as GameObject;
}
void OnDisable()
{
m_state.Dispose();
// Debug.Log("OnDisable");
Selection.selectionChanged -= Repaint;
Undo.willFlushUndoRecord -= Repaint;
// m_settingsInspector
UnityEditor.Editor.DestroyImmediate(m_settingsInspector);
m_settingsInspector = null;
// m_settings
ScriptableObject.DestroyImmediate(m_settings);
m_settings = null;
}
public delegate Vector2 BeginVerticalScrollViewFunc(Vector2 scrollPosition, bool alwaysShowVertical, GUIStyle verticalScrollbar, GUIStyle background, params GUILayoutOption[] options);
static BeginVerticalScrollViewFunc s_func;
static BeginVerticalScrollViewFunc BeginVerticalScrollView
{
get
{
if (s_func == null)
{
var methods = typeof(EditorGUILayout).GetMethods(BindingFlags.Static | BindingFlags.NonPublic).Where(x => x.Name == "BeginVerticalScrollView").ToArray();
var method = methods.First(x => x.GetParameters()[1].ParameterType == typeof(bool));
s_func = (BeginVerticalScrollViewFunc)method.CreateDelegate(typeof(BeginVerticalScrollViewFunc));
}
return s_func;
}
}
private Vector2 m_ScrollPosition;
IEnumerable<Validator> ValidatorFactory()
{
yield return HierarchyValidator.ValidateRoot;
yield return AnimationValidator.Validate;
if (!m_state.ExportRoot)
{
yield break;
}
}
private void OnGUI()
{
// ArgumentException: Getting control 1's position in a group with only 1 controls when doing repaint Aborting
// Validation により GUI の表示項目が変わる場合があるので、
// EventType.Layout と EventType.Repaint 間で内容が変わらないようしている。
if (Event.current.type == EventType.Layout)
{
m_state.Validate(ValidatorFactory());
}
EditorGUIUtility.labelWidth = 150;
// lang
Getter.OnGuiSelectLang();
EditorGUILayout.LabelField("ExportRoot");
{
m_state.ExportRoot = (GameObject)EditorGUILayout.ObjectField(m_state.ExportRoot, typeof(GameObject), true);
}
// Render contents using Generic Inspector GUI
m_ScrollPosition = BeginVerticalScrollView(m_ScrollPosition, false, GUI.skin.verticalScrollbar, "OL Box");
GUIUtility.GetControlID(645789, FocusType.Passive);
bool modified = ScrollArea();
EditorGUILayout.EndScrollView();
// Create and Other Buttons
{
// errors
GUILayout.BeginVertical();
// GUILayout.FlexibleSpace();
{
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
GUI.enabled = m_state.Validations.All(x => x.CanExport);
if (GUILayout.Button("Export", GUILayout.MinWidth(100)))
{
OnExportClicked(m_state.ExportRoot, m_settings);
Close();
GUIUtility.ExitGUI();
}
GUI.enabled = true;
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();
}
GUILayout.Space(8);
if (modified)
{
m_state.Invalidate();
}
}
bool ScrollArea()
{
//
// Validation
//
foreach (var v in m_state.Validations)
{
v.DrawGUI();
if (v.ErrorLevel == ErrorLevels.Critical)
{
// Export UI を表示しない
return false;
}
}
//
// GUI
//
m_settings.Root = m_state.ExportRoot;
m_settingsInspector.OnInspectorGUI();
return true;
}
private static string m_lastExportDir;
static void OnExportClicked(GameObject root, GltfExportSettings settings)
{
string directory;
if (string.IsNullOrEmpty(m_lastExportDir))
{
directory = Directory.GetParent(Application.dataPath).ToString();
}
else
{
directory = m_lastExportDir;
}
// save dialog
var path = EditorUtility.SaveFilePanel(
"Save vrm",
directory,
root.name + ".glb",
"glb,gltf");
if (string.IsNullOrEmpty(path))
{
return;
}
m_lastExportDir = Path.GetDirectoryName(path).Replace("\\", "/");
// export
Export(root, path, new MeshExportSettings
{
ExportOnlyBlendShapePosition = settings.DropNormal,
UseSparseAccessorForMorphTarget = settings.Sparse,
DivideVertexBuffer = settings.DivideVertexBuffer,
}, settings.InverseAxis);
}
}
}

View File

@ -1,9 +1,10 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using UnityEditor;
namespace UniGLTF
namespace UniGLTF.M17N
{
/// <summary>
/// 多言語対応
@ -68,17 +69,41 @@ namespace UniGLTF
return map[key];
}
}
public static class Getter
public static class LanguageGetter
{
const string LANG_KEY = "VRM_LANG";
static Languages? m_lang;
static Dictionary<CultureInfo, Languages> CultureMap = new Dictionary<CultureInfo, Languages>
{
{new CultureInfo("ja-JP",false), Languages.ja},
};
static Languages? s_lang;
public static Languages Lang
{
get
{
return m_lang.GetValueOrDefault();
if (!s_lang.HasValue)
{
var value = EditorPrefs.GetString(LANG_KEY);
if (!string.IsNullOrEmpty(value) && Enum.TryParse<Languages>(value, true, out Languages parsed))
{
s_lang = parsed;
}
else
{
if (CultureMap.TryGetValue(CultureInfo.CurrentCulture, out Languages lang))
{
s_lang = lang;
}
else
{
s_lang = default(Languages);
}
}
}
return s_lang.GetValueOrDefault();
}
}
@ -92,8 +117,8 @@ namespace UniGLTF
var lang = (Languages)EditorGUILayout.EnumPopup("lang", Lang);
if (lang != Lang)
{
m_lang = lang;
EditorPrefs.SetString(LANG_KEY, Getter.Lang.ToString());
s_lang = lang;
EditorPrefs.SetString(LANG_KEY, LanguageGetter.Lang.ToString());
}
}
}

View File

@ -0,0 +1,26 @@
using System.IO;
using UnityEditor;
using UnityEngine;
namespace UniGLTF
{
public static class SaveFileDialog
{
static string m_lastExportDir;
public static string GetPath(string title, string name, params string[] extensions)
{
string directory = m_lastExportDir;
if (string.IsNullOrEmpty(directory))
{
directory = Directory.GetParent(Application.dataPath).ToString();
}
var path = EditorUtility.SaveFilePanel(title, directory, name, string.Join(",", extensions));
if (!string.IsNullOrEmpty(path))
{
m_lastExportDir = Path.GetDirectoryName(path).Replace("\\", "/");
}
return path;
}
}
}

View File

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

View File

@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using UniGLTF.M17N;
using UnityEngine;
namespace UniGLTF

View File

@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using UniGLTF.M17N;
using UnityEditor;
using UnityEngine;

View File

@ -1,5 +1,5 @@
using UnityEngine;
using UnityEditor;
#if UNITY_2020_2_OR_NEWER
using UnityEditor.AssetImporters;
#else
@ -13,10 +13,16 @@ namespace UniGLTF
public class GlbScriptedImporter : ScriptedImporter
{
[SerializeField]
Axises m_reverseAxis = default;
Axises m_reverseAxis;
public override void OnImportAsset(AssetImportContext ctx)
{
var asset = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(ctx.assetPath);
if (asset == null)
{
// first time. set default setting
m_reverseAxis = UniGLTFPreference.GltfIOAxis;
}
ScriptedImporterImpl.Import(this, ctx, m_reverseAxis);
}
}

View File

@ -1,5 +1,5 @@
using UnityEngine;
using UnityEditor;
#if UNITY_2020_2_OR_NEWER
using UnityEditor.AssetImporters;
#else
@ -17,6 +17,12 @@ namespace UniGLTF
public override void OnImportAsset(AssetImportContext ctx)
{
var asset = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(ctx.assetPath);
if (asset == null)
{
// first time. set default setting
m_reverseAxis = UniGLTFPreference.GltfIOAxis;
}
ScriptedImporterImpl.Import(this, ctx, m_reverseAxis);
}
}

View File

@ -0,0 +1,55 @@
using UnityEngine;
using UnityEditor;
using System.Linq;
using System;
namespace UniGLTF
{
public static class UniGLTFPreference
{
[PreferenceItem("UniGLTF")]
private static void OnPreferenceGUI()
{
EditorGUI.BeginChangeCheck();
// language
M17N.LanguageGetter.OnGuiSelectLang();
EditorGUILayout.HelpBox($"Custom editor language setting", MessageType.Info, true);
// default axis
GltfIOAxis = (Axises)EditorGUILayout.EnumPopup("Default Invert axis", GltfIOAxis);
EditorGUILayout.HelpBox($"Default invert axis when glb/gltf import/export", MessageType.Info, true);
if (EditorGUI.EndChangeCheck())
{
}
}
const string AXIS_KEY = "UNIGLTF_IO_AXIS";
static Axises? s_axis;
public static Axises GltfIOAxis
{
set
{
EditorPrefs.SetString(AXIS_KEY, value.ToString());
s_axis = value;
}
get
{
if (!s_axis.HasValue)
{
var value = EditorPrefs.GetString(AXIS_KEY, default(Axises).ToString());
if (Enum.TryParse<Axises>(value, out Axises parsed))
{
s_axis = parsed;
}
else
{
s_axis = default(Axises);
}
}
return s_axis.Value;
}
}
}
}

View File

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

View File

@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using UniGLTF;
using UniGLTF.M17N;
using UnityEngine;
namespace VRM

View File

@ -6,6 +6,7 @@ using System.Reflection;
using System.Linq;
using System.Collections.Generic;
using UniGLTF;
using UniGLTF.M17N;
namespace VRM
{
@ -48,7 +49,7 @@ namespace VRM
static string Msg(Options key)
{
return Getter.Msg(key);
return LanguageGetter.Msg(key);
}
public enum Options

View File

@ -1,5 +1,6 @@
using System.Collections.Generic;
using UniGLTF;
using UniGLTF.M17N;
using UnityEngine;
namespace VRM

View File

@ -1,14 +1,13 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using UniGLTF;
using UniGLTF.M17N;
namespace VRM
{
public class VRMExporterWizard : EditorWindow
public class VRMExporterWizard : ExportDialogBase
{
const string CONVERT_HUMANOID_KEY = VRMVersion.MENU + "/Export " + VRMVersion.VRM_VERSION;
@ -29,8 +28,6 @@ namespace VRM
}
Tabs _tab;
ExporterDialogState m_state;
VRMExportSettings m_settings;
VRMExportMeshes m_meshes;
@ -59,12 +56,8 @@ namespace VRM
Editor m_settingsInspector;
Editor m_meshesInspector;
void OnEnable()
protected override void Initialize()
{
// Debug.Log("OnEnable");
Undo.willFlushUndoRecord += Repaint;
Selection.selectionChanged += Repaint;
m_tmpMeta = ScriptableObject.CreateInstance<VRMMetaObject>();
m_settings = ScriptableObject.CreateInstance<VRMExportSettings>();
@ -73,8 +66,7 @@ namespace VRM
m_meshes = ScriptableObject.CreateInstance<VRMExportMeshes>();
m_meshesInspector = Editor.CreateEditor(m_meshes);
m_state = new ExporterDialogState();
m_state.ExportRootChanged += (root) =>
State.ExportRootChanged += (root) =>
{
// update meta
if (root == null)
@ -99,20 +91,11 @@ namespace VRM
|| m_meshes.Meshes.Any(x => x.ExportBlendShapeCount > 0 && !x.HasSkinning)
;
}
Repaint();
};
m_state.ExportRoot = Selection.activeObject as GameObject;
}
void OnDisable()
protected override void Clear()
{
m_state.Dispose();
// Debug.Log("OnDisable");
Selection.selectionChanged -= Repaint;
Undo.willFlushUndoRecord -= Repaint;
// m_metaEditor
UnityEditor.Editor.DestroyImmediate(m_metaEditor);
m_metaEditor = null;
@ -134,31 +117,20 @@ namespace VRM
m_meshes = null;
}
public delegate Vector2 BeginVerticalScrollViewFunc(Vector2 scrollPosition, bool alwaysShowVertical, GUIStyle verticalScrollbar, GUIStyle background, params GUILayoutOption[] options);
static BeginVerticalScrollViewFunc s_func;
static BeginVerticalScrollViewFunc BeginVerticalScrollView
{
get
{
if (s_func == null)
{
var methods = typeof(EditorGUILayout).GetMethods(BindingFlags.Static | BindingFlags.NonPublic).Where(x => x.Name == "BeginVerticalScrollView").ToArray();
var method = methods.First(x => x.GetParameters()[1].ParameterType == typeof(bool));
s_func = (BeginVerticalScrollViewFunc)method.CreateDelegate(typeof(BeginVerticalScrollViewFunc));
}
return s_func;
}
}
private Vector2 m_ScrollPosition;
protected override string SaveTitle => "Save vrm0";
IEnumerable<Validator> ValidatorFactory()
protected override string SaveName => $"{State.ExportRoot.name}.vrm";
protected override string[] SaveExtensions => new string[] { "vrm" };
protected override IEnumerable<Validator> ValidatorFactory()
{
HumanoidValidator.MeshInformations = m_meshes.Meshes;
HumanoidValidator.EnableFreeze = m_settings.PoseFreeze;
VRMExporterValidator.ReduceBlendshape = m_settings.ReduceBlendshape;
yield return HierarchyValidator.Validate;
if (!m_state.ExportRoot)
if (!State.ExportRoot)
{
yield break;
}
@ -167,13 +139,13 @@ namespace VRM
yield return VRMExporterValidator.Validate;
yield return VRMSpringBoneValidator.Validate;
var firstPerson = m_state.ExportRoot.GetComponent<VRMFirstPerson>();
var firstPerson = State.ExportRoot.GetComponent<VRMFirstPerson>();
if (firstPerson != null)
{
yield return firstPerson.Validate;
}
var proxy = m_state.ExportRoot.GetComponent<VRMBlendShapeProxy>();
var proxy = State.ExportRoot.GetComponent<VRMBlendShapeProxy>();
if (proxy != null)
{
yield return proxy.Validate;
@ -183,82 +155,13 @@ namespace VRM
yield return meta.Validate;
}
private void OnGUI()
protected override void ExportPath(string path)
{
// ArgumentException: Getting control 1's position in a group with only 1 controls when doing repaint Aborting
// Validation により GUI の表示項目が変わる場合があるので、
// EventType.Layout と EventType.Repaint 間で内容が変わらないようしている。
if (Event.current.type == EventType.Layout)
{
// m_settings, m_meshes.Meshes
m_meshes.SetRoot(m_state.ExportRoot, m_settings);
m_state.Validate(ValidatorFactory());
}
EditorGUIUtility.labelWidth = 150;
// lang
Getter.OnGuiSelectLang();
EditorGUILayout.LabelField("ExportRoot");
{
m_state.ExportRoot = (GameObject)EditorGUILayout.ObjectField(m_state.ExportRoot, typeof(GameObject), true);
}
// Render contents using Generic Inspector GUI
m_ScrollPosition = BeginVerticalScrollView(m_ScrollPosition, false, GUI.skin.verticalScrollbar, "OL Box");
GUIUtility.GetControlID(645789, FocusType.Passive);
bool modified = ScrollArea();
EditorGUILayout.EndScrollView();
// Create and Other Buttons
{
// errors
GUILayout.BeginVertical();
// GUILayout.FlexibleSpace();
{
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
GUI.enabled = m_state.Validations.All(x => x.CanExport);
if (GUILayout.Button("Export", GUILayout.MinWidth(100)))
{
OnExportClicked(m_state.ExportRoot, Meta != null ? Meta : m_tmpMeta, m_settings, m_meshes);
Close();
GUIUtility.ExitGUI();
}
GUI.enabled = true;
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();
}
GUILayout.Space(8);
if (modified)
{
m_state.Invalidate();
}
VRMEditorExporter.Export(path, State.ExportRoot, Meta != null ? Meta : m_tmpMeta, m_settings, m_meshes.Meshes);
}
bool ScrollArea()
protected override bool DoGUI()
{
//
// Validation
//
foreach (var v in m_state.Validations)
{
v.DrawGUI();
if (v.ErrorLevel == ErrorLevels.Critical)
{
// Export UI を表示しない
return false;
}
}
EditorGUILayout.HelpBox($"Mesh size: {m_meshes.ExpectedExportByteSize / 1000000.0f:0.0} MByte", MessageType.Info);
//
@ -308,14 +211,14 @@ namespace VRM
break;
case Tabs.BlendShape:
if (m_state.ExportRoot)
if (State.ExportRoot)
{
OnBlendShapeGUI(m_state.ExportRoot.GetComponent<VRMBlendShapeProxy>());
OnBlendShapeGUI(State.ExportRoot.GetComponent<VRMBlendShapeProxy>());
}
break;
case Tabs.ExportSettings:
m_settings.Root = m_state.ExportRoot;
m_settings.Root = State.ExportRoot;
m_settingsInspector.OnInspectorGUI();
break;
}
@ -347,7 +250,7 @@ namespace VRM
int m_selected = 0;
void OnBlendShapeGUI(VRMBlendShapeProxy proxy)
{
if (!m_state.ExportRoot.scene.IsValid())
if (!State.ExportRoot.scene.IsValid())
{
EditorGUILayout.HelpBox(BlendShapeTabMessages.CANNOT_MANIPULATE_PREFAB.Msg(), MessageType.Warning);
return;
@ -386,31 +289,5 @@ namespace VRM
m_merger.Apply();
}
}
const string EXTENSION = ".vrm";
private static string m_lastExportDir;
static void OnExportClicked(GameObject root, VRMMetaObject meta, VRMExportSettings settings, VRMExportMeshes meshes)
{
string directory;
if (string.IsNullOrEmpty(m_lastExportDir))
directory = Directory.GetParent(Application.dataPath).ToString();
else
directory = m_lastExportDir;
// save dialog
var path = EditorUtility.SaveFilePanel(
"Save vrm",
directory,
root.name + EXTENSION,
EXTENSION.Substring(1));
if (string.IsNullOrEmpty(path))
{
return;
}
m_lastExportDir = Path.GetDirectoryName(path).Replace("\\", "/");
// export
VRMEditorExporter.Export(path, root, meta, settings, meshes.Meshes);
}
}
}

View File

@ -2,6 +2,7 @@ using UnityEditor;
using UnityEngine;
using System.IO;
using UniGLTF;
using UniGLTF.M17N;
namespace VRM
{
@ -55,7 +56,7 @@ namespace VRM
static string RequiredMessage(string name)
{
switch (Getter.Lang)
switch (LanguageGetter.Lang)
{
case Languages.ja:
return $"必須項目。{name} を入力してください";
@ -162,7 +163,7 @@ namespace VRM
static string Msg(MessageKeys key)
{
return Getter.Msg(key);
return LanguageGetter.Msg(key);
}
bool m_foldoutInfo = true;

View File

@ -1,6 +1,7 @@
using UnityEditor;
using UnityEngine;
using UniGLTF;
using UniGLTF.M17N;
namespace UniVRM10
{
@ -82,7 +83,7 @@ namespace UniVRM10
static string RequiredMessage(string name)
{
switch (Getter.Lang)
switch (LanguageGetter.Lang)
{
case Languages.ja:
return $"必須項目。{name} を入力してください";
@ -184,7 +185,7 @@ namespace UniVRM10
static string Msg(MessageKeys key)
{
return Getter.Msg(key);
return LanguageGetter.Msg(key);
}
bool m_foldoutInfo = true;

View File

@ -12,7 +12,7 @@ using VRMShaders;
namespace UniVRM10
{
public class VRM10ExportDialog : EditorWindow
public class VRM10ExportDialog : ExportDialogBase
{
const string CONVERT_HUMANOID_KEY = VRMVersion.MENU + "/Export VRM-1.0";
@ -32,8 +32,6 @@ namespace UniVRM10
}
Tabs _tab;
ExporterDialogState m_state;
VRM10MetaObject m_meta;
VRM10MetaObject Meta
{
@ -59,21 +57,23 @@ namespace UniVRM10
m_meta = value;
}
}
protected override string SaveTitle => "Vrm1";
protected override string SaveName => $"{State.ExportRoot.name}.vrm";
protected override string[] SaveExtensions => new string[] { "vrm" };
VRM10MetaObject m_tmpMeta;
Editor m_metaEditor;
void OnEnable()
protected override void Initialize()
{
// Debug.Log("OnEnable");
Undo.willFlushUndoRecord += Repaint;
Selection.selectionChanged += Repaint;
m_tmpMeta = ScriptableObject.CreateInstance<VRM10MetaObject>();
m_tmpMeta.Authors = new List<string> { "" };
m_state = new ExporterDialogState();
m_state.ExportRootChanged += (root) =>
State.ExportRootChanged += (root) =>
{
// update meta
if (root == null)
@ -98,42 +98,19 @@ namespace UniVRM10
// || m_meshes.Meshes.Any(x => x.ExportBlendShapeCount > 0 && !x.HasSkinning)
// ;
}
Repaint();
};
m_state.ExportRoot = Selection.activeObject as GameObject;
}
void OnDisable()
protected override void Clear()
{
m_state.Dispose();
// Debug.Log("OnDisable");
Selection.selectionChanged -= Repaint;
Undo.willFlushUndoRecord -= Repaint;
ScriptableObject.DestroyImmediate(m_tmpMeta);
m_tmpMeta = null;
}
public delegate Vector2 BeginVerticalScrollViewFunc(Vector2 scrollPosition, bool alwaysShowVertical, GUIStyle verticalScrollbar, GUIStyle background, params GUILayoutOption[] options);
static BeginVerticalScrollViewFunc s_func;
static BeginVerticalScrollViewFunc BeginVerticalScrollView
{
get
{
if (s_func == null)
{
var methods = typeof(EditorGUILayout).GetMethods(BindingFlags.Static | BindingFlags.NonPublic).Where(x => x.Name == "BeginVerticalScrollView").ToArray();
var method = methods.First(x => x.GetParameters()[1].ParameterType == typeof(bool));
s_func = (BeginVerticalScrollViewFunc)method.CreateDelegate(typeof(BeginVerticalScrollViewFunc));
}
return s_func;
}
}
private Vector2 m_ScrollPosition;
IEnumerable<Validator> ValidatorFactory()
protected override IEnumerable<Validator> ValidatorFactory()
{
yield return HierarchyValidator.Validate;
if (!m_state.ExportRoot)
if (!State.ExportRoot)
{
yield break;
}
@ -144,13 +121,13 @@ namespace UniVRM10
// yield return VRMExporterValidator.Validate;
// yield return VRMSpringBoneValidator.Validate;
// var firstPerson = m_state.ExportRoot.GetComponent<VRMFirstPerson>();
// var firstPerson = State.ExportRoot.GetComponent<VRMFirstPerson>();
// if (firstPerson != null)
// {
// yield return firstPerson.Validate;
// }
// var proxy = m_state.ExportRoot.GetComponent<VRMBlendShapeProxy>();
// var proxy = State.ExportRoot.GetComponent<VRMBlendShapeProxy>();
// if (proxy != null)
// {
// yield return proxy.Validate;
@ -160,81 +137,36 @@ namespace UniVRM10
yield return meta.Validate;
}
private void OnGUI()
// private void OnGUI()
// {
// {
// var path = SaveFileDialog.GetPath("Save vrm1", $"{State.ExportRoot.name}.vrm", "vrm");
// if (!string.IsNullOrEmpty(path))
// {
// // export
// Export(State.ExportRoot, path);
// // close
// Close();
// GUIUtility.ExitGUI();
// }
// }
// GUI.enabled = true;
// GUILayout.EndHorizontal();
// }
// GUILayout.EndVertical();
// }
// GUILayout.Space(8);
// if (modified)
// {
// State.Invalidate();
// }
// }
protected override bool DoGUI()
{
// ArgumentException: Getting control 1's position in a group with only 1 controls when doing repaint Aborting
// Validation により GUI の表示項目が変わる場合があるので、
// EventType.Layout と EventType.Repaint 間で内容が変わらないようしている。
if (Event.current.type == EventType.Layout)
{
m_state.Validate(ValidatorFactory());
}
EditorGUIUtility.labelWidth = 150;
// lang
Getter.OnGuiSelectLang();
EditorGUILayout.LabelField("ExportRoot");
{
m_state.ExportRoot = (GameObject)EditorGUILayout.ObjectField(m_state.ExportRoot, typeof(GameObject), true);
}
// Render contents using Generic Inspector GUI
m_ScrollPosition = BeginVerticalScrollView(m_ScrollPosition, false, GUI.skin.verticalScrollbar, "OL Box");
GUIUtility.GetControlID(645789, FocusType.Passive);
bool modified = ScrollArea();
EditorGUILayout.EndScrollView();
// Create and Other Buttons
{
// errors
GUILayout.BeginVertical();
// GUILayout.FlexibleSpace();
{
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
GUI.enabled = m_state.Validations.All(x => x.CanExport);
if (GUILayout.Button("Export", GUILayout.MinWidth(100)))
{
OnExportClicked(m_state.ExportRoot);
Close();
GUIUtility.ExitGUI();
}
GUI.enabled = true;
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();
}
GUILayout.Space(8);
if (modified)
{
m_state.Invalidate();
}
}
bool ScrollArea()
{
//
// Validation
//
foreach (var v in m_state.Validations)
{
v.DrawGUI();
if (v.ErrorLevel == ErrorLevels.Critical)
{
// Export UI を表示しない
return false;
}
}
if (m_tmpMeta == null)
{
// disabled
@ -274,32 +206,14 @@ namespace UniVRM10
string m_logLabel;
const string EXTENSION = ".vrm";
private static string m_lastExportDir;
void OnExportClicked(GameObject root)
protected override void ExportPath(string path)
{
m_logLabel = "";
string directory;
if (string.IsNullOrEmpty(m_lastExportDir))
directory = Directory.GetParent(Application.dataPath).ToString();
else
directory = m_lastExportDir;
// save dialog
var path = EditorUtility.SaveFilePanel(
"Save vrm",
directory,
root.name + EXTENSION,
EXTENSION.Substring(1));
if (string.IsNullOrEmpty(path))
{
return;
}
m_lastExportDir = Path.GetDirectoryName(path).Replace("\\", "/");
m_logLabel += $"export...\n";
var root = State.ExportRoot;
try
{
var converter = new UniVRM10.RuntimeVrmConverter();