ExporterWindow.cs 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using UnityEditor;
  6. using UnityEngine;
  7. using UnityEngine.SceneManagement;
  8. using JsonFx.Json;
  9. using JsonFx.Json.Resolvers;
  10. using JsonFx.Serialization;
  11. using JsonFx.Serialization.Resolvers;
  12. using BabylonHosting;
  13. namespace Unity3D2Babylon
  14. {
  15. public class ExporterWindow : EditorWindow
  16. {
  17. public const double BabylonVersion = 2.5;
  18. public const string ToolkitVersion = "1.0";
  19. public const string DefaultHost = "localhost";
  20. public const int DefaultPort = 8888;
  21. public static ExportationOptions exportationOptions = null;
  22. public static readonly List<string> logs = new List<string>();
  23. Vector2 scrollPosMain;
  24. bool showLighting = false;
  25. bool showCollision = false;
  26. bool showShader = false;
  27. bool showPreview = false;
  28. EditorWindow assetStore = null;
  29. public static void ReportProgress(float value, string message = "")
  30. {
  31. EditorUtility.DisplayProgressBar("Babylon.js", message, value);
  32. if (!string.IsNullOrEmpty(message))
  33. {
  34. logs.Add(message);
  35. }
  36. }
  37. public static bool ShowMessage(string message, string title = "Babylon.js", string ok = "OK", string cancel = "Cancel")
  38. {
  39. return EditorUtility.DisplayDialog(title, message, ok, cancel);
  40. }
  41. public ExportationOptions CreateSettings()
  42. {
  43. ExportationOptions result = new ExportationOptions();
  44. string apath = Application.dataPath.Replace("/Assets", "");
  45. string ufile = Path.Combine(apath, "UnityBabylonOptions.ini");
  46. if (File.Exists(ufile))
  47. {
  48. var readText = File.ReadAllText(ufile);
  49. var jsReader = new JsonReader();
  50. result = jsReader.Read<ExportationOptions>(readText);
  51. }
  52. return result;
  53. }
  54. public void SaveSettings(bool refresh = false)
  55. {
  56. if (refresh) GetSceneInfomation(false);
  57. string apath = Application.dataPath.Replace("/Assets", "");
  58. string ufile = Path.Combine(apath, "UnityBabylonOptions.ini");
  59. var settings = new DataWriterSettings() { PrettyPrint = true };
  60. var jsWriter = new JsonWriter(settings);
  61. File.WriteAllText(ufile, jsWriter.Write(exportationOptions));
  62. }
  63. [MenuItem("BabylonJS/Scene Exporter", false, 0)]
  64. public static void InitExporter()
  65. {
  66. var exporter = (ExporterWindow)GetWindow(typeof(ExporterWindow));
  67. exporter.minSize = new Vector2(420.0f, 480.0f);
  68. exporter.OnInitialize();
  69. }
  70. [MenuItem("BabylonJS/Output Window", false, 1)]
  71. public static void InitOutput()
  72. {
  73. var output = (ExporterOutput)GetWindow(typeof(ExporterOutput));
  74. output.OnInitialize();
  75. }
  76. [MenuItem("BabylonJS/Update Libraries", false, 2)]
  77. public static void InitUpdate()
  78. {
  79. Tools.EnableRemoteCertificates();
  80. string prodVersion = ExporterWindow.exportationOptions.ProductionVersion.ToString();
  81. if (prodVersion.IndexOf(".", StringComparison.OrdinalIgnoreCase) < 0)
  82. {
  83. prodVersion += ".0";
  84. }
  85. string updateMsg = (exportationOptions.DefaultUpdateOptions == (int)BabylonUpdateOptions.PreviewRelease) ? "Are you sure you want to update libraries using the github preview release version?" : "Are you sure you want to update libraries using the github stable release version " + prodVersion + "?";
  86. if (ExporterWindow.ShowMessage(updateMsg, "Babylon.js", "Update"))
  87. {
  88. EditorUtility.DisplayProgressBar("Babylon.js", "Updating github editor toolkit library files...", 1);
  89. string libPath = Path.Combine(Application.dataPath, "Babylon/Library/");
  90. string bjsPath = (exportationOptions.DefaultUpdateOptions == (int)BabylonUpdateOptions.PreviewRelease) ? "https://raw.githubusercontent.com/BabylonJS/Babylon.js/master/dist/preview%20release/babylon.js" : "https://raw.githubusercontent.com/BabylonJS/Babylon.js/master/dist/babylon." + prodVersion + ".js";
  91. string bjsTsPath = (exportationOptions.DefaultUpdateOptions == (int)BabylonUpdateOptions.PreviewRelease) ? "https://raw.githubusercontent.com/BabylonJS/Babylon.js/master/dist/preview%20release/babylon.d.ts" : "https://raw.githubusercontent.com/BabylonJS/Babylon.js/master/dist/babylon." + prodVersion + ".d.ts";
  92. string c2dPath = (exportationOptions.DefaultUpdateOptions == (int)BabylonUpdateOptions.PreviewRelease) ? "https://raw.githubusercontent.com/BabylonJS/Babylon.js/master/dist/preview%20release/canvas2D/babylon.canvas2d.js" : "https://raw.githubusercontent.com/BabylonJS/Babylon.js/master/dist/babylon." + prodVersion + ".canvas2d.js";
  93. string c2dTsPath = (exportationOptions.DefaultUpdateOptions == (int)BabylonUpdateOptions.PreviewRelease) ? "https://raw.githubusercontent.com/BabylonJS/Babylon.js/master/dist/preview%20release/canvas2D/babylon.canvas2d.d.ts" : "https://raw.githubusercontent.com/BabylonJS/Babylon.js/master/dist/babylon." + prodVersion + ".canvas2d.d.ts";
  94. string cannonPath = (exportationOptions.DefaultUpdateOptions == (int)BabylonUpdateOptions.PreviewRelease) ? "https://raw.githubusercontent.com/BabylonJS/Babylon.js/master/dist/preview%20release/cannon.js" : "https://raw.githubusercontent.com/BabylonJS/Babylon.js/master/dist/cannon.js";
  95. string oimoPath = (exportationOptions.DefaultUpdateOptions == (int)BabylonUpdateOptions.PreviewRelease) ? "https://raw.githubusercontent.com/BabylonJS/Babylon.js/master/dist/preview%20release/Oimo.js" : "https://raw.githubusercontent.com/BabylonJS/Babylon.js/master/dist/Oimo.js";
  96. try
  97. {
  98. EditorUtility.DisplayProgressBar("Babylon.js", "Updating babylon.bjs...", 0.10f);
  99. Tools.DownloadFile(bjsPath, Path.Combine(libPath, "babylon.bjs"));
  100. EditorUtility.DisplayProgressBar("Babylon.js", "Updating babylon.d.ts...", 0.20f);
  101. Tools.DownloadFile(bjsTsPath, Path.Combine(libPath, "babylon.d.ts"));
  102. EditorUtility.DisplayProgressBar("Babylon.js", "Updating canvas2d.bjs...", 0.30f);
  103. Tools.DownloadFile(c2dPath, Path.Combine(libPath, "canvas2d.bjs"));
  104. EditorUtility.DisplayProgressBar("Babylon.js", "Updating canvas2d.d.ts...", 0.40f);
  105. Tools.DownloadFile(c2dTsPath, Path.Combine(libPath, "canvas2d.d.ts"));
  106. EditorUtility.DisplayProgressBar("Babylon.js", "Updating cannon.bjs...", 0.50f);
  107. Tools.DownloadFile(cannonPath, Path.Combine(libPath, "cannon.bjs"));
  108. EditorUtility.DisplayProgressBar("Babylon.js", "Updating oimo.bjs...", 0.60f);
  109. Tools.DownloadFile(oimoPath, Path.Combine(libPath, "oimo.bjs"));
  110. EditorUtility.DisplayProgressBar("Babylon.js", "Updating navmesh.bjs...", 0.70f);
  111. Tools.DownloadFile("https://raw.githubusercontent.com/BabylonJS/Extensions/master/SceneManager/dist/babylon.navigation.mesh.js", Path.Combine(libPath, "navmesh.bjs"));
  112. EditorUtility.DisplayProgressBar("Babylon.js", "Updating manager.bjs...", 0.80f);
  113. Tools.DownloadFile("https://raw.githubusercontent.com/BabylonJS/Extensions/master/SceneManager/dist/babylon.scenemanager.js", Path.Combine(libPath, "manager.bjs"));
  114. EditorUtility.DisplayProgressBar("Babylon.js", "Updating manager.d.ts...", 0.90f);
  115. Tools.DownloadFile("https://raw.githubusercontent.com/BabylonJS/Extensions/master/SceneManager/dist/babylon.scenemanager.d.ts", Path.Combine(libPath, "manager.d.ts"));
  116. }
  117. catch (System.Exception ex)
  118. {
  119. UnityEngine.Debug.LogException(ex);
  120. }
  121. finally
  122. {
  123. EditorUtility.DisplayProgressBar("Babylon.js", "Refresing assets database...", 1.0f);
  124. AssetDatabase.Refresh();
  125. EditorUtility.ClearProgressBar();
  126. }
  127. }
  128. }
  129. [MenuItem("BabylonJS/Babylon Dashboard", false, 999)]
  130. public static void InitDashboard()
  131. {
  132. Application.OpenURL("http://www.babylonjs.com");
  133. }
  134. [MenuItem("Assets/Create/BabylonJS/Javascript Class", false, 101)]
  135. public static void CreateJavascript()
  136. {
  137. string path = AssetDatabase.GetAssetPath(Selection.activeObject);
  138. if (String.IsNullOrEmpty(path))
  139. {
  140. path = "Assets";
  141. }
  142. else if (!String.IsNullOrEmpty(Path.GetExtension(path)))
  143. {
  144. path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), "");
  145. }
  146. string filename = Path.Combine(path, "NewJavascript.bjs");
  147. string template = "Assets/Babylon/Templates/Scripts/javascript.template";
  148. if (!File.Exists(template))
  149. {
  150. string defaultTemplate = "// Babylon Javascript File";
  151. File.WriteAllText(template, defaultTemplate);
  152. }
  153. var icon = EditorGUIUtility.FindTexture("ScriptableObject Icon") as Texture2D;
  154. var DoCreateScriptAsset = Type.GetType("UnityEditor.ProjectWindowCallback.DoCreateScriptAsset, UnityEditor");
  155. ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance(DoCreateScriptAsset) as UnityEditor.ProjectWindowCallback.EndNameEditAction, filename, icon, template);
  156. }
  157. [MenuItem("Assets/Create/BabylonJS/Typescript Class", false, 102)]
  158. public static void CreateTypescript()
  159. {
  160. string path = AssetDatabase.GetAssetPath(Selection.activeObject);
  161. if (String.IsNullOrEmpty(path))
  162. {
  163. path = "Assets";
  164. }
  165. else if (!String.IsNullOrEmpty(Path.GetExtension(path)))
  166. {
  167. path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), "");
  168. }
  169. string filename = Path.Combine(path, "NewTypescript.ts");
  170. string template = "Assets/Babylon/Templates/Scripts/typescript.template";
  171. if (!File.Exists(template))
  172. {
  173. string defaultTemplate = "// Babylon Typescript File";
  174. File.WriteAllText(template, defaultTemplate);
  175. }
  176. var icon = EditorGUIUtility.FindTexture("ScriptableObject Icon") as Texture2D;
  177. var DoCreateScriptAsset = Type.GetType("UnityEditor.ProjectWindowCallback.DoCreateScriptAsset, UnityEditor");
  178. ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance(DoCreateScriptAsset) as UnityEditor.ProjectWindowCallback.EndNameEditAction, filename, icon, template);
  179. }
  180. [MenuItem("Assets/Create/BabylonJS/Scene Controller", false, 201)]
  181. public static void CreateSceneController_TS()
  182. {
  183. string path = AssetDatabase.GetAssetPath(Selection.activeObject);
  184. if (String.IsNullOrEmpty(path))
  185. {
  186. path = "Assets";
  187. }
  188. else if (!String.IsNullOrEmpty(Path.GetExtension(path)))
  189. {
  190. path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), "");
  191. }
  192. string filename = Path.Combine(path, "NewSceneController.ts");
  193. string template = "Assets/Babylon/Templates/Scripts/controller.template";
  194. if (!File.Exists(template))
  195. {
  196. string defaultTemplate = "// Babylon Scene Controller";
  197. File.WriteAllText(template, defaultTemplate);
  198. }
  199. var icon = EditorGUIUtility.FindTexture("ScriptableObject Icon") as Texture2D;
  200. var DoCreateScriptAsset = Type.GetType("UnityEditor.ProjectWindowCallback.DoCreateScriptAsset, UnityEditor");
  201. ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance(DoCreateScriptAsset) as UnityEditor.ProjectWindowCallback.EndNameEditAction, filename, icon, template);
  202. }
  203. [MenuItem("Assets/Create/BabylonJS/Mesh Component", false, 301)]
  204. public static void CreateMeshComponent_TS()
  205. {
  206. string path = AssetDatabase.GetAssetPath(Selection.activeObject);
  207. if (String.IsNullOrEmpty(path))
  208. {
  209. path = "Assets";
  210. }
  211. else if (!String.IsNullOrEmpty(Path.GetExtension(path)))
  212. {
  213. path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), "");
  214. }
  215. string filename = Path.Combine(path, "NewMeshComponent.ts");
  216. string template = "Assets/Babylon/Templates/Scripts/mesh.template";
  217. if (!File.Exists(template))
  218. {
  219. string defaultTemplate = "// Babylon Mesh Class";
  220. File.WriteAllText(template, defaultTemplate);
  221. }
  222. var icon = EditorGUIUtility.FindTexture("ScriptableObject Icon") as Texture2D;
  223. var DoCreateScriptAsset = Type.GetType("UnityEditor.ProjectWindowCallback.DoCreateScriptAsset, UnityEditor");
  224. ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance(DoCreateScriptAsset) as UnityEditor.ProjectWindowCallback.EndNameEditAction, filename, icon, template);
  225. }
  226. [MenuItem("Assets/Create/BabylonJS/Light Component", false, 302)]
  227. public static void CreateLightComponent_TS()
  228. {
  229. string path = AssetDatabase.GetAssetPath(Selection.activeObject);
  230. if (String.IsNullOrEmpty(path))
  231. {
  232. path = "Assets";
  233. }
  234. else if (!String.IsNullOrEmpty(Path.GetExtension(path)))
  235. {
  236. path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), "");
  237. }
  238. string filename = Path.Combine(path, "NewLightComponent.ts");
  239. string template = "Assets/Babylon/Templates/Scripts/light.template";
  240. if (!File.Exists(template))
  241. {
  242. string defaultTemplate = "// Babylon Light Class";
  243. File.WriteAllText(template, defaultTemplate);
  244. }
  245. var icon = EditorGUIUtility.FindTexture("ScriptableObject Icon") as Texture2D;
  246. var DoCreateScriptAsset = Type.GetType("UnityEditor.ProjectWindowCallback.DoCreateScriptAsset, UnityEditor");
  247. ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance(DoCreateScriptAsset) as UnityEditor.ProjectWindowCallback.EndNameEditAction, filename, icon, template);
  248. }
  249. [MenuItem("Assets/Create/BabylonJS/Camera Component", false, 303)]
  250. public static void CreateCameraComponent_TS()
  251. {
  252. string path = AssetDatabase.GetAssetPath(Selection.activeObject);
  253. if (String.IsNullOrEmpty(path))
  254. {
  255. path = "Assets";
  256. }
  257. else if (!String.IsNullOrEmpty(Path.GetExtension(path)))
  258. {
  259. path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), "");
  260. }
  261. string filename = Path.Combine(path, "NewCameraComponent.ts");
  262. string template = "Assets/Babylon/Templates/Scripts/camera.template";
  263. if (!File.Exists(template))
  264. {
  265. string defaultTemplate = "// Babylon Camera Class";
  266. File.WriteAllText(template, defaultTemplate);
  267. }
  268. var icon = EditorGUIUtility.FindTexture("ScriptableObject Icon") as Texture2D;
  269. var DoCreateScriptAsset = Type.GetType("UnityEditor.ProjectWindowCallback.DoCreateScriptAsset, UnityEditor");
  270. ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance(DoCreateScriptAsset) as UnityEditor.ProjectWindowCallback.EndNameEditAction, filename, icon, template);
  271. }
  272. [MenuItem("Assets/Create/BabylonJS/Unity Editor Script/Babylon Scene Controller (C#)", false, 901)]
  273. public static void CreateEditorController_CS()
  274. {
  275. string path = AssetDatabase.GetAssetPath(Selection.activeObject);
  276. if (String.IsNullOrEmpty(path))
  277. {
  278. path = "Assets";
  279. }
  280. else if (!String.IsNullOrEmpty(Path.GetExtension(path)))
  281. {
  282. path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), "");
  283. }
  284. string filename = Path.Combine(path, "NewSceneController.cs");
  285. string template = "Assets/Babylon/Templates/Scripts/editora.template";
  286. if (!File.Exists(template))
  287. {
  288. string defaultTemplate = "// Babylon Controller Class";
  289. File.WriteAllText(template, defaultTemplate);
  290. }
  291. var icon = EditorGUIUtility.FindTexture("ScriptableObject Icon") as Texture2D;
  292. var DoCreateScriptAsset = Type.GetType("UnityEditor.ProjectWindowCallback.DoCreateScriptAsset, UnityEditor");
  293. ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance(DoCreateScriptAsset) as UnityEditor.ProjectWindowCallback.EndNameEditAction, filename, icon, template);
  294. }
  295. [MenuItem("Assets/Create/BabylonJS/Unity Editor Script/Babylon Script Component (C#)", false, 902)]
  296. public static void CreateEditorComponent_CS()
  297. {
  298. string path = AssetDatabase.GetAssetPath(Selection.activeObject);
  299. if (String.IsNullOrEmpty(path))
  300. {
  301. path = "Assets";
  302. }
  303. else if (!String.IsNullOrEmpty(Path.GetExtension(path)))
  304. {
  305. path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), "");
  306. }
  307. string filename = Path.Combine(path, "NewScriptComponent.cs");
  308. string template = "Assets/Babylon/Templates/Scripts/editorb.template";
  309. if (!File.Exists(template))
  310. {
  311. string defaultTemplate = "// Babylon Script Class";
  312. File.WriteAllText(template, defaultTemplate);
  313. }
  314. var icon = EditorGUIUtility.FindTexture("ScriptableObject Icon") as Texture2D;
  315. var DoCreateScriptAsset = Type.GetType("UnityEditor.ProjectWindowCallback.DoCreateScriptAsset, UnityEditor");
  316. ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance(DoCreateScriptAsset) as UnityEditor.ProjectWindowCallback.EndNameEditAction, filename, icon, template);
  317. }
  318. [MenuItem("Assets/Create/BabylonJS/Unity Editor Script/Universal Shader Program (GLSL)", false, 999)]
  319. public static void CreateShader()
  320. {
  321. string path = AssetDatabase.GetAssetPath(Selection.activeObject);
  322. if (String.IsNullOrEmpty(path))
  323. {
  324. path = "Assets";
  325. }
  326. else if (!String.IsNullOrEmpty(Path.GetExtension(path)))
  327. {
  328. path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), "");
  329. }
  330. string filename = Path.Combine(path, "NewShaderProgram.shader");
  331. string template = "Assets/Babylon/Templates/Shaders/amiga.template";
  332. if (!File.Exists(template))
  333. {
  334. string defaultTemplate = "// Babylon Shader Class";
  335. File.WriteAllText(template, defaultTemplate);
  336. }
  337. var icon = EditorGUIUtility.FindTexture("ScriptableObject Icon") as Texture2D;
  338. var DoCreateScriptAsset = Type.GetType("UnityEditor.ProjectWindowCallback.DoCreateScriptAsset, UnityEditor");
  339. ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance(DoCreateScriptAsset) as UnityEditor.ProjectWindowCallback.EndNameEditAction, filename, icon, template);
  340. }
  341. public void OnInitialize() { }
  342. public void OnEnable()
  343. {
  344. this.titleContent = new GUIContent("Exporter");
  345. if (ExporterWindow.exportationOptions == null)
  346. {
  347. ExporterWindow.exportationOptions = CreateSettings();
  348. }
  349. // Attach unity editor buttons
  350. UnityEditor.EditorApplication.playmodeStateChanged = () =>
  351. {
  352. if (exportationOptions != null && exportationOptions.AttachUnityEditor)
  353. {
  354. bool wantsToPlay = UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode;
  355. bool wantsToPause = UnityEditor.EditorApplication.isPaused;
  356. if (wantsToPlay || wantsToPause)
  357. {
  358. UnityEditor.EditorApplication.isPlaying = false;
  359. UnityEditor.EditorApplication.isPaused = false;
  360. }
  361. if (wantsToPlay) Execute();
  362. }
  363. };
  364. // Activate asset store window
  365. if (this.assetStore == null)
  366. {
  367. this.assetStore = Tools.GetAssetStoreWindow();
  368. }
  369. // Activate internl web server
  370. this.StartServer();
  371. }
  372. public void StartServer()
  373. {
  374. if (exportationOptions.HostPreviewPage)
  375. {
  376. // Validate default project folder selected
  377. if (String.IsNullOrEmpty(exportationOptions.DefaultProjectFolder))
  378. {
  379. UnityEngine.Debug.LogWarning("No default project file selected. Web server not started.");
  380. return;
  381. }
  382. // Validate default project folder exists
  383. if (!Directory.Exists(exportationOptions.DefaultProjectFolder))
  384. {
  385. UnityEngine.Debug.LogWarning("No default project file created. Web server not started.");
  386. return;
  387. }
  388. bool supported = WebServer.IsSupported;
  389. string prefix = "http://*:";
  390. string root = exportationOptions.DefaultProjectFolder;
  391. int port = exportationOptions.DefaultServerPort;
  392. bool started = WebServer.Activate(prefix, root, port);
  393. if (started) UnityEngine.Debug.Log("Babylon.js web server started on port: " + port.ToString());
  394. if (!started && supported) UnityEngine.Debug.LogWarning("Babylon.js web server is available.");
  395. if (!started && !supported) UnityEngine.Debug.LogWarning("Babylon.js web server is not available.");
  396. }
  397. }
  398. public void OnGUI()
  399. {
  400. GUILayout.Label("BabylonJS Toolkit - Version: " + ExporterWindow.ToolkitVersion, EditorStyles.boldLabel);
  401. EditorGUI.BeginDisabledGroup(true);
  402. exportationOptions.DefaultProjectFolder = EditorGUILayout.TextField("", exportationOptions.DefaultProjectFolder);
  403. EditorGUI.EndDisabledGroup();
  404. if (GUILayout.Button("Select Project Folder"))
  405. {
  406. SelectFolder();
  407. }
  408. if (GUILayout.Button("Save Export Settings"))
  409. {
  410. SaveSettings();
  411. ShowMessage("Export settings saved.");
  412. }
  413. scrollPosMain = EditorGUILayout.BeginScrollView(scrollPosMain, GUILayout.ExpandWidth(false), GUILayout.ExpandHeight(true));
  414. EditorGUILayout.Space();
  415. exportationOptions.DefaultBuildPath = EditorGUILayout.TextField(" Project Build Path", exportationOptions.DefaultBuildPath);
  416. EditorGUILayout.Space();
  417. exportationOptions.DefaultScenePath = EditorGUILayout.TextField(" Project Scene Path", exportationOptions.DefaultScenePath);
  418. EditorGUILayout.Space();
  419. exportationOptions.DefaultScriptPath = EditorGUILayout.TextField(" Project Script Path", exportationOptions.DefaultScriptPath);
  420. EditorGUILayout.Space();
  421. exportationOptions.DefaultIndexPage = EditorGUILayout.TextField(" Project Index Page", exportationOptions.DefaultIndexPage);
  422. EditorGUILayout.Space();
  423. EditorGUILayout.BeginHorizontal();
  424. exportationOptions.ExportPhysics = EditorGUILayout.Toggle(" Enable Physics Engine", exportationOptions.ExportPhysics);
  425. exportationOptions.DefaultPhysicsEngine = (int)(BabylonPhysicsEngine)EditorGUILayout.EnumPopup("Default Physics Engine", (BabylonPhysicsEngine)exportationOptions.DefaultPhysicsEngine, GUILayout.ExpandWidth(true));
  426. EditorGUILayout.EndHorizontal();
  427. EditorGUILayout.Space();
  428. EditorGUILayout.BeginHorizontal();
  429. GUILayout.Label(" Light Rotation Offset");
  430. exportationOptions.LightRotationOffset = EditorGUILayout.Vector3Field("", exportationOptions.LightRotationOffset, GUILayout.ExpandWidth(false));
  431. EditorGUILayout.EndHorizontal();
  432. EditorGUILayout.Space();
  433. exportationOptions.LightIntensityFactor = EditorGUILayout.Slider(" Light Intensity Factor", exportationOptions.LightIntensityFactor, 0, 10.0f);
  434. EditorGUILayout.Space();
  435. exportationOptions.DefaultAspectRatio = EditorGUILayout.Slider(" Viewport Camera Ratio", exportationOptions.DefaultAspectRatio, 0, 10.0f);
  436. EditorGUILayout.Space();
  437. exportationOptions.ReflectionDefaultLevel = EditorGUILayout.Slider(" Default Reflection Level", exportationOptions.ReflectionDefaultLevel, 0, 1.0f);
  438. EditorGUILayout.Space();
  439. exportationOptions.DefaultQualityLevel = (int)EditorGUILayout.Slider(" Texture Image Quality", exportationOptions.DefaultQualityLevel, 0, 100);
  440. EditorGUILayout.Space();
  441. exportationOptions.DefaultImageFormat = (int)(BabylonImageFormat)EditorGUILayout.EnumPopup(" Prefered Texture Format", (BabylonImageFormat)exportationOptions.DefaultImageFormat, GUILayout.ExpandWidth(true));
  442. EditorGUILayout.Space();
  443. showCollision = EditorGUILayout.Foldout(showCollision, "Scene Collision Options");
  444. if (showCollision)
  445. {
  446. EditorGUILayout.Space();
  447. exportationOptions.ExportCollisions = EditorGUILayout.Toggle(" Enable Collisions", exportationOptions.ExportCollisions);
  448. EditorGUILayout.Space();
  449. exportationOptions.WorkerCollisions = EditorGUILayout.Toggle(" Worker Collisions", exportationOptions.WorkerCollisions);
  450. EditorGUILayout.Space();
  451. EditorGUILayout.BeginHorizontal();
  452. GUILayout.Label(" Camera Ellipsoid");
  453. exportationOptions.CameraEllipsoid = EditorGUILayout.Vector3Field("", exportationOptions.CameraEllipsoid, GUILayout.ExpandWidth(false));
  454. EditorGUILayout.EndHorizontal();
  455. EditorGUILayout.Space();
  456. EditorGUILayout.BeginHorizontal();
  457. GUILayout.Label(" Default Scene Gravity");
  458. exportationOptions.Gravity = EditorGUILayout.Vector3Field("", exportationOptions.Gravity, GUILayout.ExpandWidth(false));
  459. EditorGUILayout.EndHorizontal();
  460. EditorGUILayout.Space();
  461. exportationOptions.DefaultColliderDetail = (int)(BabylonColliderDetail)EditorGUILayout.EnumPopup(" Default Collider Detail", (BabylonColliderDetail)exportationOptions.DefaultColliderDetail, GUILayout.ExpandWidth(true));
  462. EditorGUILayout.Space();
  463. }
  464. showShader = EditorGUILayout.Foldout(showShader, "Shader Program Options");
  465. if (showShader)
  466. {
  467. EditorGUILayout.Space();
  468. exportationOptions.EmbeddedShaders = EditorGUILayout.Toggle(" Embed Shader Files", exportationOptions.EmbeddedShaders);
  469. EditorGUILayout.Space();
  470. exportationOptions.DefaultShaderFolder = EditorGUILayout.TextField(" Output Src Shader Path", exportationOptions.DefaultShaderFolder);
  471. EditorGUILayout.Space();
  472. }
  473. showLighting = EditorGUILayout.Foldout(showLighting, "Lightmap Baking Options");
  474. if (showLighting)
  475. {
  476. EditorGUILayout.Space();
  477. EditorGUILayout.BeginHorizontal();
  478. exportationOptions.ExportLightmaps = EditorGUILayout.Toggle(" Export Lightmaps", exportationOptions.ExportLightmaps);
  479. exportationOptions.DefaultLightmapBaking = (int)(BabylonLightmapBaking)EditorGUILayout.EnumPopup(" Synchronous Baking", (BabylonLightmapBaking)exportationOptions.DefaultLightmapBaking, GUILayout.ExpandWidth(true));
  480. EditorGUILayout.EndHorizontal();
  481. EditorGUILayout.Space();
  482. exportationOptions.DefaultLightmapMode = (int)(BabylonLightmapMode)EditorGUILayout.EnumPopup(" Light Mapper Mode", (BabylonLightmapMode)exportationOptions.DefaultLightmapMode, GUILayout.ExpandWidth(true));
  483. EditorGUILayout.Space();
  484. exportationOptions.DefaultCoordinatesIndex = (int)EditorGUILayout.Slider(" Coordinates Index", exportationOptions.DefaultCoordinatesIndex, 0, 1);
  485. EditorGUILayout.Space();
  486. EditorGUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
  487. exportationOptions.ExportShadows = EditorGUILayout.Toggle(" Enable Shadow Map", exportationOptions.ExportShadows);
  488. GUILayout.Label(" Default Shadow Map Size");
  489. exportationOptions.ShadowMapSize = EditorGUILayout.IntField("", exportationOptions.ShadowMapSize, GUILayout.Width(50));
  490. EditorGUILayout.EndHorizontal();
  491. EditorGUILayout.Space();
  492. exportationOptions.DefaultLightFilter = (int)(BabylonLightingFilter)EditorGUILayout.EnumPopup(" Shadow Map Filter", (BabylonLightingFilter)exportationOptions.DefaultLightFilter, GUILayout.ExpandWidth(true));
  493. EditorGUILayout.Space();
  494. exportationOptions.ShadowMapBias = EditorGUILayout.Slider(" Shadow Map Bias", exportationOptions.ShadowMapBias, 0, 1.0f);
  495. EditorGUILayout.Space();
  496. exportationOptions.ShadowBlurScale = EditorGUILayout.Slider(" Shadow Blur Scale", exportationOptions.ShadowBlurScale, 0, 5.0f);
  497. EditorGUILayout.Space();
  498. }
  499. showPreview = EditorGUILayout.Foldout(showPreview, "Default Exporting Options");
  500. if (showPreview)
  501. {
  502. EditorGUILayout.Space();
  503. exportationOptions.AttachUnityEditor = EditorGUILayout.Toggle(" Attach Unity Editor", exportationOptions.AttachUnityEditor);
  504. EditorGUILayout.Space();
  505. exportationOptions.HostPreviewPage = EditorGUILayout.Toggle(" Host Preview Server", exportationOptions.HostPreviewPage);
  506. EditorGUILayout.Space();
  507. exportationOptions.ShowDebugControls = EditorGUILayout.Toggle(" Show Debug Controls", exportationOptions.ShowDebugControls);
  508. EditorGUILayout.Space();
  509. exportationOptions.DefaultServerPort = EditorGUILayout.IntField(" Default Server Port", exportationOptions.DefaultServerPort);
  510. EditorGUILayout.Space();
  511. exportationOptions.DefaultPreviewWindow = (int)(BabylonPreviewWindow)EditorGUILayout.EnumPopup(" Default Preview Window", (BabylonPreviewWindow)exportationOptions.DefaultPreviewWindow, GUILayout.ExpandWidth(true));
  512. EditorGUILayout.Space();
  513. exportationOptions.BuildJavaScript = EditorGUILayout.Toggle(" Build Javascript Files", exportationOptions.BuildJavaScript);
  514. EditorGUILayout.Space();
  515. exportationOptions.CompileTypeScript = EditorGUILayout.Toggle(" Build Typescript Files", exportationOptions.CompileTypeScript);
  516. EditorGUILayout.Space();
  517. exportationOptions.DefaultTypeSriptPath = EditorGUILayout.TextField(" Typescript Compiler", exportationOptions.DefaultTypeSriptPath);
  518. EditorGUILayout.Space();
  519. exportationOptions.DefaultNodeRuntimePath = EditorGUILayout.TextField(" Node Runtime System", exportationOptions.DefaultNodeRuntimePath);
  520. EditorGUILayout.Space();
  521. exportationOptions.ProductionVersion = EditorGUILayout.FloatField(" Stable Babylon Version", exportationOptions.ProductionVersion);
  522. EditorGUILayout.Space();
  523. exportationOptions.DefaultUpdateOptions = (int)(BabylonUpdateOptions)EditorGUILayout.EnumPopup(" Github Update Version", (BabylonUpdateOptions)exportationOptions.DefaultUpdateOptions, GUILayout.ExpandWidth(true));
  524. EditorGUILayout.Space();
  525. }
  526. EditorGUILayout.EndScrollView();
  527. EditorGUILayout.Space();
  528. // Exporter buttons
  529. if (GUILayout.Button("Build Script"))
  530. {
  531. if (ExporterWindow.ShowMessage("Are you sure you want to build the project script files?", "Babylon.js", "Build"))
  532. {
  533. Build(true);
  534. }
  535. }
  536. if (GUILayout.Button("Export Scene"))
  537. {
  538. Export(false);
  539. }
  540. if (GUILayout.Button("Export & Preview"))
  541. {
  542. Export(true);
  543. }
  544. if (ExporterWindow.exportationOptions.HostPreviewPage == true)
  545. {
  546. if (GUILayout.Button("Start Preview Server"))
  547. {
  548. StartServer();
  549. }
  550. }
  551. if (GUILayout.Button("Launch Preview Window"))
  552. {
  553. Execute();
  554. }
  555. EditorGUILayout.Space();
  556. }
  557. public void OnInspectorUpdate()
  558. {
  559. this.Repaint();
  560. }
  561. public void SelectFolder()
  562. {
  563. exportationOptions.DefaultProjectFolder = EditorUtility.SaveFolderPanel("Please select a folder", exportationOptions.DefaultProjectFolder, "");
  564. }
  565. public void Build(bool solo, string[] info = null)
  566. {
  567. if (solo)
  568. {
  569. // Validate default project folder selected
  570. if (String.IsNullOrEmpty(exportationOptions.DefaultProjectFolder))
  571. {
  572. ShowMessage("No default project file selected.");
  573. return;
  574. }
  575. // Validate default project folder exists
  576. if (!Directory.Exists(exportationOptions.DefaultProjectFolder))
  577. {
  578. ShowMessage("No default project file created.");
  579. return;
  580. }
  581. }
  582. try
  583. {
  584. var sceneInfo = info ?? GetSceneInfomation(false);
  585. string scriptPath = sceneInfo[2];
  586. string projectScript = sceneInfo[4];
  587. // Save and clear console
  588. if (solo)
  589. {
  590. try { UnityEngine.Debug.ClearDeveloperConsole(); } catch { }
  591. SaveSettings();
  592. }
  593. // Assemble javascript files
  594. string javascriptFile = Tools.FormatProjectJavaScript(scriptPath, projectScript);
  595. if (solo || exportationOptions.BuildJavaScript)
  596. {
  597. ReportProgress(1, "Building project javascript files...");
  598. Tools.BuildProjectJavaScript(scriptPath, projectScript, javascriptFile);
  599. }
  600. // Compile typescript files
  601. if (solo || exportationOptions.CompileTypeScript)
  602. {
  603. ReportProgress(1, "Compiling project typescript files... This may take a while.");
  604. string config = String.Empty;
  605. string options = Path.Combine(Application.dataPath, "Babylon/Templates/Config/options.json");
  606. if (File.Exists(options)) config = File.ReadAllText(options);
  607. Tools.BuildProjectTypeScript(exportationOptions.DefaultNodeRuntimePath, exportationOptions.DefaultTypeSriptPath, scriptPath, javascriptFile, config);
  608. }
  609. }
  610. catch (Exception ex)
  611. {
  612. UnityEngine.Debug.LogException(ex);
  613. }
  614. finally
  615. {
  616. if (solo) EditorUtility.ClearProgressBar();
  617. }
  618. }
  619. public void Export(bool preview)
  620. {
  621. try
  622. {
  623. // Validate lightmap bake in progress
  624. if (exportationOptions.ExportLightmaps && exportationOptions.DefaultLightmapBaking == (int)BabylonLightmapBaking.Enabled && Lightmapping.isRunning)
  625. {
  626. ShowMessage("There is a bake already in progress.");
  627. return;
  628. }
  629. // Validate default project folder selected
  630. if (String.IsNullOrEmpty(exportationOptions.DefaultProjectFolder))
  631. {
  632. ShowMessage("No default project file selected.");
  633. return;
  634. }
  635. // Validate default project folder exists
  636. if (!Directory.Exists(exportationOptions.DefaultProjectFolder))
  637. {
  638. if (ExporterWindow.ShowMessage("Create default project folder: " + exportationOptions.DefaultProjectFolder, "Babylon.js - Project not found", "Create"))
  639. {
  640. Directory.CreateDirectory(exportationOptions.DefaultProjectFolder);
  641. }
  642. else
  643. {
  644. return;
  645. }
  646. }
  647. // Get validate scene path info
  648. string[] sceneInfo = GetSceneInfomation(true);
  649. string sceneName = sceneInfo[0];
  650. string scenePath = sceneInfo[1];
  651. string scriptPath = sceneInfo[2];
  652. string outputFile = sceneInfo[3];
  653. string projectScript = sceneInfo[4];
  654. if (!ExporterWindow.ShowMessage("Export current scene to babylon: " + sceneName, "Babylon.js", "Export"))
  655. {
  656. return;
  657. }
  658. // Save current scene info
  659. SaveSettings();
  660. ExporterWindow.logs.Clear();
  661. Stopwatch watch = new Stopwatch();
  662. watch.Start();
  663. ReportProgress(0, "Exporting " + scenePath);
  664. // Auto lightmap baking
  665. if (exportationOptions.ExportLightmaps && exportationOptions.DefaultLightmapBaking == (int)BabylonLightmapBaking.Enabled)
  666. {
  667. ReportProgress(1, "Baking lightmap textures... This may take a while.");
  668. Lightmapping.GIWorkflowMode workflow = Lightmapping.giWorkflowMode;
  669. Lightmapping.giWorkflowMode = Lightmapping.GIWorkflowMode.OnDemand;
  670. Lightmapping.Bake();
  671. Lightmapping.giWorkflowMode = workflow;
  672. }
  673. // Save all open scenes
  674. ReportProgress(1, "Saving open scene information...");
  675. UnityEditor.SceneManagement.EditorSceneManager.SaveOpenScenes();
  676. // Build project preview
  677. if (preview)
  678. {
  679. Tools.GenerateProjectIndexPage(exportationOptions.DefaultProjectFolder, exportationOptions.ShowDebugControls, exportationOptions.DefaultScenePath, Path.GetFileName(outputFile), exportationOptions.DefaultScriptPath, Path.GetFileName(projectScript));
  680. if (exportationOptions.BuildJavaScript || exportationOptions.CompileTypeScript)
  681. {
  682. Build(false, sceneInfo);
  683. }
  684. }
  685. // Build current scene
  686. BabylonSceneController sceneController = Tools.GetSceneController();
  687. var sceneBuilder = new SceneBuilder(scenePath, sceneName, exportationOptions, sceneController, scriptPath);
  688. sceneBuilder.ConvertFromUnity();
  689. ReportProgress(1, "Generating babylon scene... This may take a while.");
  690. sceneBuilder.WriteToBabylonFile(outputFile);
  691. watch.Stop();
  692. ReportProgress(1, string.Format("Exportation done in {0:0.00}s", watch.Elapsed.TotalSeconds));
  693. EditorUtility.ClearProgressBar();
  694. sceneBuilder.GenerateStatus(logs);
  695. string done = preview ? "Preview" : "OK";
  696. bool ok = ShowMessage("Scene exportation complete.", "Babylon.js", done);
  697. if (preview && ok)
  698. {
  699. Preview();
  700. }
  701. }
  702. catch (Exception ex)
  703. {
  704. EditorUtility.ClearProgressBar();
  705. ShowMessage("A problem occurred: " + ex.Message + ex.StackTrace, "Error");
  706. }
  707. }
  708. public string[] GetSceneInfomation(bool validate)
  709. {
  710. string[] result = new string[6];
  711. string sceneName = SceneManager.GetActiveScene().name;
  712. if (String.IsNullOrEmpty(exportationOptions.DefaultBuildPath))
  713. {
  714. exportationOptions.DefaultBuildPath = "Build";
  715. }
  716. string buildPath = Tools.FormatSafePath(Path.Combine(exportationOptions.DefaultProjectFolder, exportationOptions.DefaultBuildPath));
  717. if (validate && !Directory.Exists(buildPath))
  718. {
  719. Directory.CreateDirectory(buildPath);
  720. }
  721. if (String.IsNullOrEmpty(exportationOptions.DefaultScenePath))
  722. {
  723. exportationOptions.DefaultScenePath = "Scenes";
  724. }
  725. string scenePath = Tools.FormatSafePath(Path.Combine(exportationOptions.DefaultProjectFolder, exportationOptions.DefaultScenePath));
  726. if (validate && !Directory.Exists(scenePath))
  727. {
  728. Directory.CreateDirectory(scenePath);
  729. }
  730. if (String.IsNullOrEmpty(exportationOptions.DefaultScriptPath))
  731. {
  732. exportationOptions.DefaultScriptPath = "Scripts";
  733. }
  734. string scriptPath = Tools.FormatSafePath(Path.Combine(exportationOptions.DefaultProjectFolder, exportationOptions.DefaultScriptPath));
  735. if (validate && !Directory.Exists(scriptPath))
  736. {
  737. Directory.CreateDirectory(scriptPath);
  738. }
  739. if (String.IsNullOrEmpty(exportationOptions.DefaultIndexPage))
  740. {
  741. exportationOptions.DefaultIndexPage = "Index.html";
  742. }
  743. if (String.IsNullOrEmpty(exportationOptions.DefaultTypeSriptPath))
  744. {
  745. exportationOptions.DefaultTypeSriptPath = Tools.GetDefaultTypeScriptPath();
  746. }
  747. if (String.IsNullOrEmpty(exportationOptions.DefaultNodeRuntimePath))
  748. {
  749. exportationOptions.DefaultNodeRuntimePath = Tools.GetDefaultNodeRuntimePath();
  750. }
  751. if (exportationOptions.DefaultServerPort < 1024)
  752. {
  753. exportationOptions.DefaultServerPort = ExporterWindow.DefaultPort;
  754. }
  755. string projectName = Application.productName;
  756. if (String.IsNullOrEmpty(projectName))
  757. {
  758. projectName = "Application";
  759. }
  760. string outputFile = Tools.FormatSafePath(Path.Combine(scenePath, sceneName.Replace(" ", "") + ".babylon"));
  761. string projectScript = Tools.FormatSafePath(Path.Combine(scenePath, projectName.Replace(" ", "") + ".babylon"));
  762. result[0] = sceneName;
  763. result[1] = scenePath;
  764. result[2] = buildPath;
  765. result[3] = outputFile;
  766. result[4] = projectScript;
  767. return result;
  768. }
  769. public void Preview()
  770. {
  771. string hostProtocol = "http://";
  772. string previewUrl = hostProtocol + "localhost:" + exportationOptions.DefaultServerPort.ToString() + "/" + exportationOptions.DefaultIndexPage;
  773. if (exportationOptions.DefaultPreviewWindow == (int)BabylonPreviewWindow.AttachUnityBrowser)
  774. {
  775. var browser = Tools.AttachToAssetStoreWindow("Babylon", previewUrl);
  776. if (browser == null)
  777. {
  778. ShowMessage("The asset store browser must be opened. Please try your request again after the asset store has opened.", "Babylon.js - Failed to attach browser");
  779. }
  780. }
  781. else
  782. {
  783. Application.OpenURL(previewUrl);
  784. }
  785. }
  786. public void Execute()
  787. {
  788. Scene scene = SceneManager.GetActiveScene();
  789. string[] sceneInfo = GetSceneInfomation(true);
  790. string outputFile = sceneInfo[3];
  791. if (!scene.isDirty && File.Exists(outputFile))
  792. {
  793. Preview();
  794. }
  795. else
  796. {
  797. Export(true);
  798. }
  799. }
  800. }
  801. }