private static void ExportFromMenu(bool isGlb, MeshExportSettings settings) { var go = Selection.activeObject as GameObject; var ext = isGlb ? "glb" : "gltf"; if (go.transform.position == Vector3.zero && go.transform.rotation == Quaternion.identity && go.transform.localScale == Vector3.one) { var path = EditorUtility.SaveFilePanel( $"Save {ext}", "", go.name + $".{ext}", $"{ext}"); if (string.IsNullOrEmpty(path)) { return; } var gltf = new glTF(); using (var exporter = new gltfExporter(gltf)) { exporter.Prepare(go); exporter.Export(settings); } if (isGlb) { var bytes = gltf.ToGlbBytes(); File.WriteAllBytes(path, bytes); } else { var(json, buffers) = gltf.ToGltf(path); // without BOM var encoding = new System.Text.UTF8Encoding(false); File.WriteAllText(path, json, encoding); // write to local folder var dir = Path.GetDirectoryName(path); foreach (var b in buffers) { var bufferPath = Path.Combine(dir, b.uri); File.WriteAllBytes(bufferPath, b.GetBytes().ToArray()); } } if (path.StartsWithUnityAssetPath()) { AssetDatabase.ImportAsset(path.ToUnityRelativePath()); AssetDatabase.Refresh(); } } else { EditorUtility.DisplayDialog("Error", "The Root transform should have Default translation, rotation and scale.", "ok"); } }
protected override void ExportPath(string path) { var ext = Path.GetExtension(path).ToLower(); var isGlb = false; switch (ext) { case ".glb": isGlb = true; break; case ".gltf": isGlb = false; break; default: throw new System.Exception(); } var gltf = new glTF(); using (var exporter = new gltfExporter(gltf, m_settings.InverseAxis)) { exporter.Prepare(State.ExportRoot); var settings = new MeshExportSettings { ExportOnlyBlendShapePosition = m_settings.DropNormal, UseSparseAccessorForMorphTarget = m_settings.Sparse, DivideVertexBuffer = m_settings.DivideVertexBuffer, }; exporter.Export(settings, new EditorTextureSerializer()); } if (isGlb) { var bytes = gltf.ToGlbBytes(); File.WriteAllBytes(path, bytes); } else { var(json, buffers) = gltf.ToGltf(path); // without BOM var encoding = new System.Text.UTF8Encoding(false); File.WriteAllText(path, json, encoding); // write to local folder var dir = Path.GetDirectoryName(path); foreach (var b in buffers) { var bufferPath = Path.Combine(dir, b.uri); File.WriteAllBytes(bufferPath, b.GetBytes().ToArray()); } } if (path.StartsWithUnityAssetPath()) { AssetDatabase.ImportAsset(path.ToUnityRelativePath()); AssetDatabase.Refresh(); } }
private static void Export(GameObject go, string path, MeshExportSettings settings, Axises inverseAxis) { var ext = Path.GetExtension(path).ToLower(); var isGlb = false; switch (ext) { case ".glb": isGlb = true; break; case ".gltf": isGlb = false; break; default: throw new System.Exception(); } var gltf = new glTF(); using (var exporter = new gltfExporter(gltf, inverseAxis)) { exporter.Prepare(go); exporter.Export(settings, AssetTextureUtil.IsTextureEditorAsset); } if (isGlb) { var bytes = gltf.ToGlbBytes(); File.WriteAllBytes(path, bytes); } else { var(json, buffers) = gltf.ToGltf(path); // without BOM var encoding = new System.Text.UTF8Encoding(false); File.WriteAllText(path, json, encoding); // write to local folder var dir = Path.GetDirectoryName(path); foreach (var b in buffers) { var bufferPath = Path.Combine(dir, b.uri); File.WriteAllBytes(bufferPath, b.GetBytes().ToArray()); } } if (path.StartsWithUnityAssetPath()) { AssetDatabase.ImportAsset(path.ToUnityRelativePath()); AssetDatabase.Refresh(); } }