Merge pull request #1233 from ousttrue/fix/cleanup_menu

restore UniGLTF/import menu
This commit is contained in:
PoChang007
2021-09-17 19:18:46 +09:00
committed by GitHub
11 changed files with 202 additions and 83 deletions

View File

@@ -0,0 +1,161 @@
using System.IO;
using UnityEditor;
using UnityEngine;
namespace UniGLTF
{
/// <summary>
/// Menuは一か所で管理する
/// </summary>
public static class Menu
{
#region UniGLTF
[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;
}
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));
window.titleContent = new GUIContent("Gltf Exporter");
window.Show();
}
[MenuItem(UniGLTFVersion.MENU + "/GLTF: Generate Deserializer")]
static void GenerateDeserializer()
{
DeserializerGenerator.GenerateSerializer();
}
[MenuItem(UniGLTFVersion.MENU + "/GLTF: Generate Serializer")]
static void GenerateSerializer()
{
DeserializerGenerator.GenerateSerializer();
}
#endregion
#region MeshUtility
// [MenuItem(MeshUtility.MENU_PARENT + "BoneMeshEraser Wizard", priority = 31)]
// static void CreateWizard()
// {
// ScriptableWizard.DisplayWizard<BoneMeshEraserWizard>("BoneMeshEraser", "Erase triangles by bone", "Erase");
// }
const string MESH_UTILITY_DICT = "UniGLTF/Mesh Utility/";
[MenuItem(MESH_UTILITY_DICT + "MeshProcessing Wizard", priority = 30)]
static void MeshProcessFromMenu()
{
var window = (MeshUtility.MeshProcessDialog)EditorWindow.GetWindowWithRect(typeof(MeshUtility.MeshProcessDialog), new Rect(0, 0, 650, 500));
window.titleContent = new GUIContent("Mesh Processing Window");
window.Show();
}
[MenuItem(MESH_UTILITY_DICT + "MeshUtility Docs", priority = 32)]
public static void MeshUtilityDocs()
{
Application.OpenURL("https://vrm.dev/en/docs/univrm/gltf/mesh_utility/");
}
[MenuItem("Assets/SaveAsPng", true)]
[MenuItem("Assets/SaveAsPngLinear", true)]
static bool IsTextureAsset()
{
return Selection.activeObject is Texture2D;
}
[MenuItem("Assets/SaveAsPng")]
static void SaveAsPng()
{
MeshUtility.EditorChangeTextureType.SaveAsPng(true);
}
[MenuItem("Assets/SaveAsPngLinear")]
static void SaveAsPngLinear()
{
MeshUtility.EditorChangeTextureType.SaveAsPng(false);
}
#endregion
[MenuItem("UniGLTF/UniJSON/Generate ConcreteCast")]
static void GenerateConcreteCast()
{
UniJSON.ConcreteCast.GenerateGenericCast();
}
}
}

View File

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

View File

@@ -52,12 +52,6 @@ namespace UniGLTF.MeshUtility
[SerializeField]
BoneMeshEraser.EraseBone[] m_eraseBones;
// [MenuItem(MeshUtility.MENU_PARENT + "BoneMeshEraser Wizard", priority = 31)]
// static void CreateWizard()
// {
// ScriptableWizard.DisplayWizard<BoneMeshEraserWizard>("BoneMeshEraser", "Erase triangles by bone", "Erase");
// }
private void OnEnable()
{
var root = Selection.activeGameObject;

View File

@@ -15,7 +15,7 @@ namespace UniGLTF.MeshUtility
public override void OnInspectorGUI()
{
serializedObject.Update();
var skinnedMesh = serializedObject.FindProperty("_cSkinnedMesh");
var skinnedMesh = serializedObject.FindProperty("_cSkinnedMesh");
EditorGUILayout.PropertyField(skinnedMesh, new GUIContent("Skinned Mesh"), true);
var animator = serializedObject.FindProperty("_cAnimator");
EditorGUILayout.PropertyField(animator, new GUIContent("Animator"), false);
@@ -29,16 +29,6 @@ namespace UniGLTF.MeshUtility
public class MeshProcessDialog : EditorWindow
{
const string MESH_UTILITY_DICT = "UniGLTF/Mesh Utility/";
[MenuItem(MESH_UTILITY_DICT + "MeshProcessing Wizard", priority = 30)]
static void MeshProcessFromMenu()
{
var window = (MeshProcessDialog)EditorWindow.GetWindowWithRect(typeof(MeshProcessDialog), new Rect(0, 0, 650, 500));
window.titleContent = new GUIContent("Mesh Processing Window");
window.Show();
}
enum Tabs
{
MeshSeparator,
@@ -180,7 +170,7 @@ namespace UniGLTF.MeshUtility
}
// Create Other Buttons
{
{
GUILayout.BeginVertical();
{
GUILayout.BeginHorizontal();
@@ -260,13 +250,13 @@ namespace UniGLTF.MeshUtility
{
if (_exportTarget == null) return GameObjectNull();
var go = _exportTarget;
Component[] allComponents = go.GetComponents(typeof(Component));
Component[] allComponents = go.GetComponents(typeof(Component));
var keyWord = "VRMMeta";
foreach (var component in allComponents)
{
if (component == null) continue;
{
if (component == null) continue;
var sourceString = component.ToString();
if (sourceString.Contains(keyWord))
{

View File

@@ -8,7 +8,6 @@ namespace UniGLTF.MeshUtility
{
public class MeshUtility
{
public const string MENU_PARENT = "UniGLTF/Mesh Utility/";
public const string ASSET_SUFFIX = ".mesh.asset";
private static readonly Vector3 ZERO_MOVEMENT = Vector3.zero;
@@ -27,12 +26,6 @@ namespace UniGLTF.MeshUtility
WithoutBlendShape,
}
[MenuItem(MENU_PARENT + "MeshUtility Docs", priority = 32)]
public static void MeshUtilityDocs()
{
Application.OpenURL("https://vrm.dev/en/docs/univrm/gltf/mesh_utility/");
}
public static void SeparationProcessing(GameObject go)
{
var outputObject = GameObject.Instantiate(go);
@@ -319,7 +312,7 @@ namespace UniGLTF.MeshUtility
#endif
public static void MeshIntegrator(GameObject go)
{
MeshIntegratorUtility.Integrate(go, onlyBlendShapeRenderers: true);
MeshIntegratorUtility.Integrate(go, onlyBlendShapeRenderers: true);
MeshIntegratorUtility.Integrate(go, onlyBlendShapeRenderers: false);
var outputObject = GameObject.Instantiate(go);
@@ -328,13 +321,13 @@ namespace UniGLTF.MeshUtility
var normalMeshes = outputObject.GetComponentsInChildren<MeshFilter>();
// destroy integrated meshes in the source
foreach (var skinnedMesh in go.GetComponentsInChildren<SkinnedMeshRenderer>())
foreach (var skinnedMesh in go.GetComponentsInChildren<SkinnedMeshRenderer>())
{
if (skinnedMesh.sharedMesh.name == MeshIntegratorUtility.INTEGRATED_MESH_NAME ||
skinnedMesh.sharedMesh.name == MeshIntegratorUtility.INTEGRATED_MESH_BLENDSHAPE_NAME)
{
GameObject.DestroyImmediate(skinnedMesh.gameObject);
}
if (skinnedMesh.sharedMesh.name == MeshIntegratorUtility.INTEGRATED_MESH_NAME ||
skinnedMesh.sharedMesh.name == MeshIntegratorUtility.INTEGRATED_MESH_BLENDSHAPE_NAME)
{
GameObject.DestroyImmediate(skinnedMesh.gameObject);
}
}
foreach (var skinnedMesh in skinnedMeshes)
{
@@ -358,14 +351,14 @@ namespace UniGLTF.MeshUtility
}
foreach (var normalMesh in normalMeshes)
{
if (normalMesh.sharedMesh.name != MeshIntegratorUtility.INTEGRATED_MESH_NAME)
{
if (normalMesh.sharedMesh.name != MeshIntegratorUtility.INTEGRATED_MESH_NAME)
{
if (normalMesh.gameObject.GetComponent<MeshRenderer>())
{
GameObject.DestroyImmediate(normalMesh.gameObject.GetComponent<MeshRenderer>());
}
GameObject.DestroyImmediate(normalMesh);
}
}
}
}

View File

@@ -8,26 +8,7 @@ namespace UniGLTF.MeshUtility
{
public static class EditorChangeTextureType
{
[MenuItem("Assets/SaveAsPng", true)]
[MenuItem("Assets/SaveAsPngLinear", true)]
static bool IsTextureAsset()
{
return Selection.activeObject is Texture2D;
}
[MenuItem("Assets/SaveAsPng")]
static void SaveAsPng()
{
SaveAsPng(true);
}
[MenuItem("Assets/SaveAsPngLinear")]
static void SaveAsPngLinear()
{
SaveAsPng(false);
}
static void SaveAsPng(bool sRGB)
public static void SaveAsPng(bool sRGB)
{
var texture = Selection.activeObject as Texture2D;
var path = SaveDialog(AssetsPath.FromAsset(texture));

View File

@@ -10,15 +10,6 @@ namespace UniGLTF
{
public class GltfExportWindow : ExportDialogBase
{
const string MENU_KEY = UniGLTFVersion.MENU + "/Export " + UniGLTFVersion.UNIGLTF_VERSION;
[MenuItem(MENU_KEY, false, 0)]
private static void ExportFromMenu()
{
var window = (GltfExportWindow)GetWindow(typeof(GltfExportWindow));
window.titleContent = new GUIContent("Gltf Exporter");
window.Show();
}
enum Tabs

View File

@@ -39,8 +39,7 @@ public static class GltfDeserializer
}
}
[MenuItem(UniGLTFVersion.MENU + "/GLTF: Generate Deserializer")]
static void GenerateSerializer()
public static void GenerateSerializer()
{
var info = new ObjectSerialization(typeof(glTF), "gltf", "Deserialize_");
Debug.Log(info);

View File

@@ -39,8 +39,7 @@ namespace UniGLTF {
}
}
[MenuItem(UniGLTFVersion.MENU + "/GLTF: Generate Serializer")]
static void GenerateSerializer()
public static void GenerateSerializer()
{
var info = new ObjectSerialization(typeof(glTF), "gltf", "Serialize_");
Debug.Log(info);

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()

View File

@@ -43,7 +43,6 @@ namespace UniJSON
typeof(double),
};
[MenuItem("UniGLTF/UniJSON/Generate ConcreteCast")]
public static void GenerateGenericCast()
{
var s = new StringBuilder();