ExporterWindow.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using JsonFx;
  6. using UnityEditor;
  7. using UnityEngine;
  8. using JsonFx.Json;
  9. namespace Unity3D2Babylon
  10. {
  11. public class ExporterWindow : EditorWindow
  12. {
  13. private static readonly List<string> logs = new List<string>();
  14. Vector2 scrollPos;
  15. ExportationOptions exportationOptions;
  16. public static void ReportProgress(float value, string message = "")
  17. {
  18. EditorUtility.DisplayProgressBar("Babylon.js", message, value);
  19. if (!string.IsNullOrEmpty(message))
  20. {
  21. logs.Add(message);
  22. }
  23. }
  24. public static void ShowMessage(string message, string title = "Babylon.js")
  25. {
  26. EditorUtility.DisplayDialog(title, message, "OK");
  27. }
  28. [MenuItem("BabylonJS/Export to .babylon")]
  29. public static void Init()
  30. {
  31. var window = (ExporterWindow)GetWindow(typeof(ExporterWindow));
  32. window.Initialize();
  33. }
  34. void Initialize()
  35. {
  36. title = "Babylon.js";
  37. }
  38. void OnGUI()
  39. {
  40. if (exportationOptions == null)
  41. {
  42. exportationOptions = new ExportationOptions();
  43. if (File.Exists("Unity3D2Babylon.ini"))
  44. {
  45. var readText = File.ReadAllText("Unity3D2Babylon.ini");
  46. var jsReader = new JsonReader();
  47. exportationOptions = jsReader.Read<ExportationOptions>(readText);
  48. }
  49. }
  50. GUILayout.Label("Exportation options", EditorStyles.boldLabel);
  51. exportationOptions.ReflectionDefaultLevel = EditorGUILayout.Slider("Reflection default level", exportationOptions.ReflectionDefaultLevel, 0, 1.0f);
  52. EditorGUILayout.Space();
  53. GUILayout.Label("Collisions options", EditorStyles.boldLabel);
  54. exportationOptions.ExportCollisions = EditorGUILayout.Toggle("Collisions", exportationOptions.ExportCollisions);
  55. exportationOptions.CameraEllipsoid = EditorGUILayout.Vector3Field("Camera's Ellipsoid:", exportationOptions.CameraEllipsoid);
  56. exportationOptions.Gravity = EditorGUILayout.Vector3Field("Gravity:", exportationOptions.Gravity);
  57. EditorGUILayout.Space();
  58. GUILayout.Label("Physics options", EditorStyles.boldLabel);
  59. exportationOptions.ExportPhysics = EditorGUILayout.Toggle("Physics", exportationOptions.ExportPhysics);
  60. EditorGUILayout.Space();
  61. EditorGUILayout.Space();
  62. if (GUILayout.Button("Export"))
  63. {
  64. Export(false);
  65. }
  66. if (WebServer.IsSupported)
  67. {
  68. if (GUILayout.Button("Export & Run"))
  69. {
  70. Export(true);
  71. }
  72. }
  73. EditorGUILayout.Space();
  74. scrollPos = EditorGUILayout.BeginScrollView(scrollPos);
  75. foreach (var log in logs)
  76. {
  77. var bold = log.StartsWith("*");
  78. GUILayout.Label(bold ? log.Remove(0, 1) : log, bold ? (EditorStyles.boldLabel) : EditorStyles.label);
  79. }
  80. EditorGUILayout.EndScrollView();
  81. Repaint();
  82. }
  83. public void Export(bool run)
  84. {
  85. try
  86. {
  87. int pos = EditorApplication.currentScene.LastIndexOf("/", StringComparison.Ordinal);
  88. string sceneName = EditorApplication.currentScene.Substring(pos + 1);
  89. exportationOptions.DefaultFolder = EditorUtility.SaveFolderPanel("Please select a folder", exportationOptions.DefaultFolder, "");
  90. if (string.IsNullOrEmpty(exportationOptions.DefaultFolder))
  91. {
  92. return;
  93. }
  94. Stopwatch watch = new Stopwatch();
  95. watch.Start();
  96. var jsWriter = new JsonWriter();
  97. File.WriteAllText("Unity3D2Babylon.ini", jsWriter.Write(exportationOptions));
  98. logs.Clear();
  99. ReportProgress(0);
  100. var sceneBuilder = new SceneBuilder(exportationOptions.DefaultFolder, sceneName, exportationOptions);
  101. sceneBuilder.ConvertFromUnity();
  102. ReportProgress(1, "Generating output file");
  103. var outputFile = sceneBuilder.WriteToBabylonFile();
  104. watch.Stop();
  105. ReportProgress(1, string.Format("Exportation done in {0:0.00}s", watch.Elapsed.TotalSeconds));
  106. EditorUtility.ClearProgressBar();
  107. sceneBuilder.GenerateStatus(logs);
  108. ShowMessage("Exportation done");
  109. if (run)
  110. {
  111. WebServer.SceneFolder = Path.GetDirectoryName(outputFile);
  112. WebServer.SceneFilename = Path.GetFileName(outputFile);
  113. Process.Start("http://localhost:" + WebServer.Port);
  114. }
  115. }
  116. catch (Exception ex)
  117. {
  118. EditorUtility.ClearProgressBar();
  119. ShowMessage("A problem occurred: " + ex.Message + ex.StackTrace, "Error");
  120. }
  121. }
  122. }
  123. }