Exporter.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Newtonsoft.Json;
  5. using UnityEngine;
  6. namespace Siccity.GLTFUtility {
  7. /// <summary> API used for exporting .gltf and .glb files </summary>
  8. public static class Exporter {
  9. #if UNITY_EDITOR
  10. [UnityEditor.MenuItem("File/Export Selection/.glb")]
  11. public static void ExportSelectedGLB() {
  12. ExportGLB(UnityEditor.Selection.activeGameObject);
  13. }
  14. #endif
  15. public static void ExportGLB(GameObject root) {
  16. GLTFObject gltfObject = CreateGLTFObject(root.transform);
  17. Debug.Log(JsonConvert.SerializeObject(gltfObject, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }));
  18. }
  19. public static void ExportGLTF(GameObject root) {
  20. Debug.Log("Not implemented yet");
  21. }
  22. public static GLTFObject CreateGLTFObject(Transform root) {
  23. GLTFObject gltfObject = new GLTFObject();
  24. GLTFAsset asset = new GLTFAsset() {
  25. generator = "GLTFUtility by Siccity https: //github.com/Siccity/GLTFUtility",
  26. version = "2.0"
  27. };
  28. //List<GLTFAccessor.ExportResult> accessors
  29. List<GLTFNode.ExportResult> nodes = GLTFNode.Export(root);
  30. List<GLTFMesh.ExportResult> meshes = GLTFMesh.Export(nodes);
  31. gltfObject.scene = 0;
  32. gltfObject.asset = asset;
  33. gltfObject.nodes = nodes.Cast<GLTFNode>().ToList();
  34. gltfObject.meshes = meshes.Cast<GLTFMesh>().ToList();
  35. return gltfObject;
  36. }
  37. }
  38. }