EditorApplication.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.CompilerServices;
  6. using System.IO;
  7. using BansheeEngine;
  8. namespace BansheeEditor
  9. {
  10. /// <summary>
  11. /// Available tools in the scene view.
  12. /// </summary>
  13. public enum SceneViewTool
  14. {
  15. View,
  16. Move,
  17. Rotate,
  18. Scale
  19. }
  20. /// <summary>
  21. /// Pivot mode used by the scene view tools.
  22. /// </summary>
  23. public enum HandlePivotMode
  24. {
  25. Center,
  26. Pivot
  27. }
  28. /// <summary>
  29. /// Coordinate mode used by the scene view tools.
  30. /// </summary>
  31. public enum HandleCoordinateMode
  32. {
  33. Local,
  34. World
  35. }
  36. /// <summary>
  37. /// Manages various generic and global settings relating to the editor.
  38. /// </summary>
  39. public class EditorApplication
  40. {
  41. /// <summary>
  42. /// Determines the active tool shown in the scene view.
  43. /// </summary>
  44. public static SceneViewTool ActiveSceneTool
  45. {
  46. get { return EditorSettings.ActiveSceneTool; }
  47. set { EditorSettings.ActiveSceneTool = value; }
  48. }
  49. /// <summary>
  50. /// Determines the coordinate mode used by the tools in the scene view.
  51. /// </summary>
  52. public static HandleCoordinateMode ActiveCoordinateMode
  53. {
  54. get { return EditorSettings.ActiveCoordinateMode; }
  55. set { EditorSettings.ActiveCoordinateMode = value; }
  56. }
  57. /// <summary>
  58. /// Determines the pivot mode used by the tools in the scene view.
  59. /// </summary>
  60. public static HandlePivotMode ActivePivotMode
  61. {
  62. get { return EditorSettings.ActivePivotMode; }
  63. set { EditorSettings.ActivePivotMode = value; }
  64. }
  65. /// <summary>
  66. /// Camera used for rendering the scene view.
  67. /// </summary>
  68. public static Camera SceneViewCamera
  69. {
  70. get { return EditorWindow.GetWindow<SceneWindow>().Camera; }
  71. }
  72. /// <summary>
  73. /// Absolute path to the folder containing the currently open project.
  74. /// </summary>
  75. public static string ProjectPath { get { return Internal_GetProjectPath(); } }
  76. /// <summary>
  77. /// Name of the currently open project.
  78. /// </summary>
  79. public static string ProjectName { get { return Internal_GetProjectName(); } }
  80. /// <summary>
  81. /// Checks is any project currently loaded.
  82. /// </summary>
  83. public static bool IsProjectLoaded { get { return Internal_GetProjectLoaded(); } }
  84. /// <summary>
  85. /// Determines is the game currently running in the editor, or is it stopped or paused. Setting this value to false
  86. /// will stop the game, but if you just want to pause it use <see cref="IsPaused"/> property.
  87. /// </summary>
  88. public static bool IsPlaying
  89. {
  90. get { return Internal_GetIsPlaying(); }
  91. set
  92. {
  93. ToggleToolbarItem("Play", value);
  94. ToggleToolbarItem("Pause", false);
  95. if (!value)
  96. Selection.SceneObject = null;
  97. else
  98. {
  99. if (EditorSettings.GetBool(LogWindow.CLEAR_ON_PLAY_KEY, true))
  100. {
  101. Debug.Clear();
  102. LogWindow log = EditorWindow.GetWindow<LogWindow>();
  103. if (log != null)
  104. log.Refresh();
  105. }
  106. }
  107. Internal_SetIsPlaying(value);
  108. }
  109. }
  110. /// <summary>
  111. /// Determines if the game is currently running in the editor, but paused. If the game is stopped and not running
  112. /// this will return false. If the game is not running and this is enabled, the game will start running but be
  113. /// immediately paused.
  114. /// </summary>
  115. public static bool IsPaused
  116. {
  117. get { return Internal_GetIsPaused(); }
  118. set
  119. {
  120. ToggleToolbarItem("Play", !value);
  121. ToggleToolbarItem("Pause", value);
  122. Internal_SetIsPaused(value);
  123. }
  124. }
  125. /// <summary>
  126. /// Returns true if the game is currently neither running nor paused. Use <see cref="IsPlaying"/> or
  127. /// <see cref="IsPaused"/> to actually change these states.
  128. /// </summary>
  129. public static bool IsStopped
  130. {
  131. get { return !IsPlaying && !IsPaused; }
  132. }
  133. /// <summary>
  134. /// Checks whether the editor currently has focus.
  135. /// </summary>
  136. public static bool HasFocus
  137. {
  138. get { return Internal_HasFocus(); }
  139. }
  140. /// <summary>
  141. /// Render target that the main camera in the scene (if any) will render its view to. This generally means the main
  142. /// game window when running standalone, or the Game viewport when running in editor.
  143. /// </summary>
  144. internal static RenderTarget MainRenderTarget
  145. {
  146. set
  147. {
  148. IntPtr rtPtr = IntPtr.Zero;
  149. if (value != null)
  150. rtPtr = value.GetCachedPtr();
  151. Internal_SetMainRenderTarget(rtPtr);
  152. }
  153. }
  154. /// <summary>
  155. /// Returns the path where the script compiler is located at.
  156. /// </summary>
  157. internal static string CompilerPath { get { return Internal_GetCompilerPath(); } }
  158. /// <summary>
  159. /// Returns the path to the folder where the custom script assemblies are located at.
  160. /// </summary>
  161. internal static string ScriptAssemblyPath { get { return Internal_GetScriptAssemblyPath(); } }
  162. /// <summary>
  163. /// Returns the path to the folder where the .NET framework assemblies are located at.
  164. /// </summary>
  165. internal static string FrameworkAssemblyPath { get { return Internal_GetFrameworkAssemblyPath(); } }
  166. /// <summary>
  167. /// Name of the builtin assembly containing engine specific types.
  168. /// </summary>
  169. internal static string EngineAssemblyName { get { return Internal_GetEngineAssemblyName(); } }
  170. /// <summary>
  171. /// Name of the builtin assembly containing editor specific types.
  172. /// </summary>
  173. internal static string EditorAssemblyName { get { return Internal_GetEditorAssemblyName(); } }
  174. /// <summary>
  175. /// Name of the custom assembly compiled from non-editor scripts within the project.
  176. /// </summary>
  177. internal static string ScriptGameAssemblyName { get { return Internal_GetScriptGameAssemblyName(); } }
  178. /// <summary>
  179. /// Name of the custom assembly compiled from editor scripts within the project.
  180. /// </summary>
  181. internal static string ScriptEditorAssemblyName { get { return Internal_GetScriptEditorAssemblyName(); } }
  182. /// <summary>
  183. /// Returns the path to the folder where the builtin release script assemblies are located at.
  184. /// </summary>
  185. internal static string BuiltinReleaseAssemblyPath { get { return Internal_GetBuiltinReleaseAssemblyPath(); } }
  186. /// <summary>
  187. /// Returns the path to the folder where the builtin debug script assemblies are located at.
  188. /// </summary>
  189. internal static string BuiltinDebugAssemblyPath { get { return Internal_GetBuiltinDebugAssemblyPath(); } }
  190. private static EditorApplication instance;
  191. private static FolderMonitor monitor;
  192. private static ScriptCodeManager codeManager;
  193. private static HashSet<string> dirtyResources = new HashSet<string>();
  194. private static bool sceneDirty;
  195. private static bool unitTestsExecuted;
  196. /// <summary>
  197. /// Constructs a new editor application. Called at editor start-up by the runtime.
  198. /// </summary>
  199. internal EditorApplication()
  200. {
  201. instance = this;
  202. codeManager = new ScriptCodeManager();
  203. // Register controls
  204. InputConfiguration inputConfig = VirtualInput.KeyConfig;
  205. inputConfig.RegisterButton(SceneCamera.MoveForwardBinding, ButtonCode.W);
  206. inputConfig.RegisterButton(SceneCamera.MoveBackBinding, ButtonCode.S);
  207. inputConfig.RegisterButton(SceneCamera.MoveLeftBinding, ButtonCode.A);
  208. inputConfig.RegisterButton(SceneCamera.MoveRightBinding, ButtonCode.D);
  209. inputConfig.RegisterButton(SceneCamera.MoveUpBinding, ButtonCode.E);
  210. inputConfig.RegisterButton(SceneCamera.MoveDownBinding, ButtonCode.Q);
  211. inputConfig.RegisterButton(SceneCamera.MoveForwardBinding, ButtonCode.Up);
  212. inputConfig.RegisterButton(SceneCamera.MoveBackBinding, ButtonCode.Down);
  213. inputConfig.RegisterButton(SceneCamera.MoveLeftBinding, ButtonCode.Left);
  214. inputConfig.RegisterButton(SceneCamera.MoveRightBinding, ButtonCode.Right);
  215. inputConfig.RegisterButton(SceneCamera.FastMoveBinding, ButtonCode.LeftShift);
  216. inputConfig.RegisterButton(SceneCamera.RotateBinding, ButtonCode.MouseRight);
  217. inputConfig.RegisterButton(SceneCamera.PanBinding, ButtonCode.MouseMiddle);
  218. inputConfig.RegisterAxis(SceneCamera.HorizontalAxisBinding, InputAxis.MouseX);
  219. inputConfig.RegisterAxis(SceneCamera.VerticalAxisBinding, InputAxis.MouseY);
  220. inputConfig.RegisterAxis(SceneCamera.ScrollAxisBinding, InputAxis.MouseZ);
  221. inputConfig.RegisterButton(SceneWindow.ToggleProfilerOverlayBinding, ButtonCode.P, ButtonModifier.CtrlAlt);
  222. inputConfig.RegisterButton(SceneWindow.ViewToolBinding, ButtonCode.Q);
  223. inputConfig.RegisterButton(SceneWindow.FrameBinding, ButtonCode.F);
  224. inputConfig.RegisterButton(SceneWindow.MoveToolBinding, ButtonCode.W);
  225. inputConfig.RegisterButton(SceneWindow.RotateToolBinding, ButtonCode.E);
  226. inputConfig.RegisterButton(SceneWindow.ScaleToolBinding, ButtonCode.R);
  227. inputConfig.RegisterButton(SceneWindow.DuplicateBinding, ButtonCode.D, ButtonModifier.Ctrl);
  228. if (IsProjectLoaded)
  229. {
  230. monitor = new FolderMonitor(ProjectLibrary.ResourceFolder);
  231. monitor.OnAdded += OnAssetModified;
  232. monitor.OnRemoved += OnAssetModified;
  233. monitor.OnModified += OnAssetModified;
  234. }
  235. }
  236. /// <summary>
  237. /// Triggered when the folder monitor detects an asset in the monitored folder was modified.
  238. /// </summary>
  239. /// <param name="path">Path to the modified file or folder.</param>
  240. private static void OnAssetModified(string path)
  241. {
  242. ProjectLibrary.Refresh(path);
  243. }
  244. /// <summary>
  245. /// Called 60 times per second by the runtime.
  246. /// </summary>
  247. internal void OnEditorUpdate()
  248. {
  249. ProjectLibrary.Update();
  250. codeManager.Update();
  251. }
  252. /// <summary>
  253. /// Creates a new empty scene.
  254. /// </summary>
  255. [MenuItem("File/New Scene", 10051, true)]
  256. private static void NewScene()
  257. {
  258. LoadScene(null);
  259. }
  260. /// <summary>
  261. /// Opens a dialog that allows the user to select a new prefab to load as the current scene. If current scene
  262. /// is modified the user is offered a chance to save it.
  263. /// </summary>
  264. [MenuItem("File/Open Scene", ButtonModifier.Ctrl, ButtonCode.L, 10050)]
  265. private static void LoadScene()
  266. {
  267. string[] scenePaths;
  268. if (BrowseDialog.OpenFile(ProjectLibrary.ResourceFolder, "", false, out scenePaths))
  269. {
  270. if (scenePaths.Length > 0)
  271. LoadScene(scenePaths[0]);
  272. }
  273. }
  274. /// <summary>
  275. /// Opens a dialog to allows the user to select a location where to save the current scene. If scene was previously
  276. /// saved it is instead automatically saved at the last location.
  277. /// </summary>
  278. public static void SaveScene(Action onSuccess = null, Action onFailure = null)
  279. {
  280. if (!string.IsNullOrEmpty(Scene.ActiveSceneUUID))
  281. {
  282. string scenePath = ProjectLibrary.GetPath(Scene.ActiveSceneUUID);
  283. if (!string.IsNullOrEmpty(scenePath))
  284. {
  285. SaveScene(scenePath);
  286. if (onSuccess != null)
  287. onSuccess();
  288. }
  289. else
  290. SaveSceneAs(onSuccess, onFailure);
  291. }
  292. else
  293. SaveSceneAs(onSuccess, onFailure);
  294. }
  295. /// <summary>
  296. /// Opens a dialog to allows the user to select a location where to save the current scene.
  297. /// </summary>
  298. public static void SaveSceneAs(Action onSuccess = null, Action onFailure = null)
  299. {
  300. string scenePath = "";
  301. if (BrowseDialog.SaveFile(ProjectLibrary.ResourceFolder, "*.prefab", out scenePath))
  302. {
  303. if (!PathEx.IsPartOf(scenePath, ProjectLibrary.ResourceFolder))
  304. {
  305. DialogBox.Open("Error", "The location must be inside the Resources folder of the project.",
  306. DialogBox.Type.OK,
  307. x =>
  308. {
  309. if (onFailure != null)
  310. onFailure();
  311. });
  312. }
  313. else
  314. {
  315. // TODO - If path points to an existing non-scene asset or folder I should delete it otherwise
  316. // Internal_SaveScene will silently fail.
  317. scenePath = Path.ChangeExtension(scenePath, ".prefab");
  318. SaveScene(scenePath);
  319. }
  320. }
  321. else
  322. {
  323. // User canceled, so technically a success
  324. if (onSuccess != null)
  325. onSuccess();
  326. }
  327. }
  328. /// <summary>
  329. /// Loads a prefab as the current scene at the specified path. If current scene is modified the user is offered a
  330. /// chance to save it.
  331. /// </summary>
  332. /// <param name="path">Path to a valid prefab relative to the resource folder. If path is empty a brand new
  333. /// scene will be loaded.</param>
  334. public static void LoadScene(string path)
  335. {
  336. Action<string> continueLoad =
  337. (scenePath) =>
  338. {
  339. if (string.IsNullOrEmpty(path))
  340. Scene.Clear();
  341. else
  342. Scene.Load(path);
  343. SetSceneDirty(false);
  344. ProjectSettings.LastOpenScene = scenePath;
  345. ProjectSettings.Save();
  346. };
  347. Action<DialogBox.ResultType> dialogCallback =
  348. (result) =>
  349. {
  350. if (result == DialogBox.ResultType.Yes)
  351. {
  352. SaveScene();
  353. continueLoad(path);
  354. }
  355. else if (result == DialogBox.ResultType.No)
  356. continueLoad(path);
  357. };
  358. if (IsSceneModified())
  359. {
  360. DialogBox.Open("Warning", "You current scene has modifications. Do you wish to save them first?",
  361. DialogBox.Type.YesNoCancel, dialogCallback);
  362. }
  363. else
  364. continueLoad(path);
  365. }
  366. /// <summary>
  367. /// Saves the currently loaded scene to the specified path.
  368. /// </summary>
  369. /// <param name="path">Path relative to the resource folder. This can be the path to the existing scene
  370. /// prefab if it just needs updating. </param>
  371. public static void SaveScene(string path)
  372. {
  373. Prefab scene = Internal_SaveScene(path);
  374. Scene.SetActive(scene);
  375. ProjectLibrary.Refresh(true);
  376. SetSceneDirty(false);
  377. }
  378. /// <summary>
  379. /// Checks does the folder at the provieded path contain a valid project.
  380. /// </summary>
  381. /// <param name="path">Absolute path to the root project folder.</param>
  382. /// <returns>True if the folder contains a valid project.</returns>
  383. public static bool IsValidProject(string path)
  384. {
  385. return Internal_IsValidProject(path);
  386. }
  387. /// <summary>
  388. /// Contains a new project in the provided folder.
  389. /// </summary>
  390. /// <param name="path">Absolute path to the folder to create the project in. Name of this folder will be used as the
  391. /// project's name.</param>
  392. public static void CreateProject(string path)
  393. {
  394. Internal_CreateProject(path);
  395. }
  396. /// <summary>
  397. /// Wrapper for menu items for <see cref="SaveScene(Action, Action)"/> method
  398. /// </summary>
  399. [MenuItem("File/Save Scene", ButtonModifier.Ctrl, ButtonCode.S, 10049)]
  400. [ToolbarItem("Save Scene", ToolbarIcon.SaveScene, "Save scene (Ctrl + S)", 1998)]
  401. private static void SaveSceneMenu()
  402. {
  403. SaveScene();
  404. }
  405. /// <summary>
  406. /// Wrapper for menu items for <see cref="SaveSceneAs(Action, Action)"/> method
  407. /// </summary>
  408. [MenuItem("File/Save Scene As", 10048)]
  409. private static void SaveSceneAsMenu()
  410. {
  411. SaveSceneAs();
  412. }
  413. /// <summary>
  414. /// Opens a Project Window allowing you to browse for or create a project.
  415. /// </summary>
  416. [MenuItem("File/Open Project", 10100)]
  417. [ToolbarItem("Open Project", ToolbarIcon.OpenProject, "Project manager", 2000)]
  418. public static void BrowseForProject()
  419. {
  420. ProjectWindow.Open();
  421. }
  422. /// <summary>
  423. /// Saves all data in the currently open project.
  424. /// </summary>
  425. [MenuItem("File/Save Project", 10099)]
  426. [ToolbarItem("Save Project", ToolbarIcon.SaveProject, "Save project", 1999)]
  427. public static void SaveProject()
  428. {
  429. foreach (var resourceUUID in dirtyResources)
  430. {
  431. string path = ProjectLibrary.GetPath(resourceUUID);
  432. if (!IsNative(path))
  433. continue; // Native resources can't be changed
  434. Resource resource = ProjectLibrary.Load<Resource>(path);
  435. if(resource != null)
  436. ProjectLibrary.Save(resource);
  437. }
  438. dirtyResources.Clear();
  439. SetStatusProject(false);
  440. Internal_SaveProject();
  441. }
  442. /// <summary>
  443. /// Loads the project at the specified path. This method executes asynchronously.
  444. /// </summary>
  445. /// <param name="path">Absolute path to the project's root folder.</param>
  446. public static void LoadProject(string path)
  447. {
  448. if (IsProjectLoaded && path == ProjectPath)
  449. return;
  450. if (!Internal_IsValidProject(path))
  451. {
  452. Debug.LogWarning("Provided path: \"" + path + "\" is not a valid project.");
  453. return;
  454. }
  455. if (IsProjectLoaded)
  456. UnloadProject();
  457. Internal_LoadProject(path); // Triggers Internal_OnProjectLoaded when done
  458. }
  459. /// <summary>
  460. /// Closes the editor.
  461. /// </summary>
  462. public static void Quit()
  463. {
  464. Internal_Quit();
  465. }
  466. /// <summary>
  467. /// Toggles an existing toolbar button into an on or off state which changes the visuals of the button.
  468. /// </summary>
  469. /// <param name="name">Name of the existing button to toggle</param>
  470. /// <param name="on">True to toggle on, false to toggle off (default)</param>
  471. public static void ToggleToolbarItem(string name, bool on)
  472. {
  473. Internal_ToggleToolbarItem(name, on);
  474. }
  475. /// <summary>
  476. /// Opens a file or a folder in the default external application.
  477. /// </summary>
  478. /// <param name="path">Absolute path to the file or folder to open.</param>
  479. public static void OpenExternally(string path)
  480. {
  481. Internal_OpenExternally(path);
  482. }
  483. /// <summary>
  484. /// Marks a resource as dirty so that it may be saved the next time the project is saved. Optionally you may also
  485. /// call <see cref="ProjectLibrary.Save"/> to save it immediately.
  486. /// </summary>
  487. /// <param name="resource">Resource to mark as dirty</param>
  488. public static void SetDirty(Resource resource)
  489. {
  490. if (resource == null)
  491. return;
  492. SetStatusProject(true);
  493. dirtyResources.Add(resource.UUID);
  494. }
  495. /// <summary>
  496. /// Marks the current scene as dirty.
  497. /// </summary>
  498. public static void SetSceneDirty()
  499. {
  500. SetSceneDirty(true);
  501. }
  502. /// <summary>
  503. /// Marks the current scene as clean or dirty.
  504. /// </summary>
  505. /// <param name="dirty">Should the scene be marked as clean or dirty.</param>
  506. internal static void SetSceneDirty(bool dirty)
  507. {
  508. sceneDirty = dirty;
  509. SetStatusScene(Scene.ActiveSceneName, dirty);
  510. if (!dirty)
  511. dirtyResources.Remove(Scene.ActiveSceneUUID);
  512. }
  513. /// <summary>
  514. /// Checks is the specific resource dirty and needs saving.
  515. /// </summary>
  516. /// <param name="resource">Resource to check.</param>
  517. /// <returns>True if the resource requires saving, false otherwise.</returns>
  518. public static bool IsDirty(Resource resource)
  519. {
  520. return dirtyResources.Contains(resource.UUID);
  521. }
  522. /// <summary>
  523. /// Checks does the path represent a native resource.
  524. /// </summary>
  525. /// <param name="path">Filename or path to check.</param>
  526. /// <returns>True if the path represents a native resource.</returns>
  527. public static bool IsNative(string path)
  528. {
  529. string extension = Path.GetExtension(path);
  530. return extension == ".asset" || extension == ".prefab";
  531. }
  532. /// <summary>
  533. /// Unloads the currently loaded project. Offers the user a chance to save the current scene if it is modified.
  534. /// Automatically saves all project data before unloading.
  535. /// </summary>
  536. private static void UnloadProject()
  537. {
  538. Action continueUnload =
  539. () =>
  540. {
  541. Scene.Clear();
  542. if (monitor != null)
  543. {
  544. monitor.Destroy();
  545. monitor = null;
  546. }
  547. LibraryWindow window = EditorWindow.GetWindow<LibraryWindow>();
  548. if(window != null)
  549. window.Reset();
  550. SetSceneDirty(false);
  551. Internal_UnloadProject();
  552. SetStatusProject(false);
  553. };
  554. Action<DialogBox.ResultType> dialogCallback =
  555. (result) =>
  556. {
  557. if (result == DialogBox.ResultType.Yes)
  558. SaveScene();
  559. continueUnload();
  560. };
  561. if (IsSceneModified())
  562. {
  563. DialogBox.Open("Warning", "You current scene has modifications. Do you wish to save them first?",
  564. DialogBox.Type.YesNoCancel, dialogCallback);
  565. }
  566. else
  567. continueUnload();
  568. }
  569. /// <summary>
  570. /// Reloads all script assemblies in case they were modified. This action is delayed and will be executed
  571. /// at the beginning of the next frame.
  572. /// </summary>
  573. public static void ReloadAssemblies()
  574. {
  575. Internal_ReloadAssemblies();
  576. }
  577. /// <summary>
  578. /// Changes the scene displayed on the status bar.
  579. /// </summary>
  580. /// <param name="name">Name of the scene.</param>
  581. /// <param name="modified">Whether to display the scene as modified or not.</param>
  582. private static void SetStatusScene(string name, bool modified)
  583. {
  584. Internal_SetStatusScene(name, modified);
  585. }
  586. /// <summary>
  587. /// Changes the project state displayed on the status bar.
  588. /// </summary>
  589. /// <param name="modified">Whether to display the project as modified or not.</param>
  590. private static void SetStatusProject(bool modified)
  591. {
  592. Internal_SetStatusProject(modified);
  593. }
  594. /// <summary>
  595. /// Displays or hides the "compilation in progress" visual on the status bar.
  596. /// </summary>
  597. /// <param name="compiling">True to display the visual, false otherwise.</param>
  598. internal static void SetStatusCompiling(bool compiling)
  599. {
  600. Internal_SetStatusCompiling(compiling);
  601. }
  602. /// <summary>
  603. /// Checks did we make any modifications to the scene since it was last saved.
  604. /// </summary>
  605. /// <returns>True if the scene was never saved, or was modified after last save.</returns>
  606. public static bool IsSceneModified()
  607. {
  608. return sceneDirty;
  609. }
  610. /// <summary>
  611. /// Runs a single frame of the game and pauses it. If the game is not currently running it will be started.
  612. /// </summary>
  613. public static void FrameStep()
  614. {
  615. if (IsStopped)
  616. {
  617. if (EditorSettings.GetBool(LogWindow.CLEAR_ON_PLAY_KEY, true))
  618. {
  619. Debug.Clear();
  620. LogWindow log = EditorWindow.GetWindow<LogWindow>();
  621. if (log != null)
  622. log.Refresh();
  623. }
  624. }
  625. ToggleToolbarItem("Play", false);
  626. ToggleToolbarItem("Pause", true);
  627. Internal_FrameStep();
  628. }
  629. /// <summary>
  630. /// Executes any editor-specific unit tests. This should be called after a project is loaded if possible.
  631. /// </summary>
  632. private static void RunUnitTests()
  633. {
  634. #if DEBUG
  635. Internal_RunUnitTests();
  636. #endif
  637. }
  638. /// <summary>
  639. /// Triggered by the runtime when <see cref="LoadProject"/> method completes.
  640. /// </summary>
  641. private static void Internal_OnProjectLoaded()
  642. {
  643. SetStatusProject(false);
  644. if (!unitTestsExecuted)
  645. {
  646. RunUnitTests();
  647. unitTestsExecuted = true;
  648. }
  649. if (!IsProjectLoaded)
  650. {
  651. ProjectWindow.Open();
  652. return;
  653. }
  654. string projectPath = ProjectPath;
  655. RecentProject[] recentProjects = EditorSettings.RecentProjects;
  656. bool foundPath = false;
  657. for (int i = 0; i < recentProjects.Length; i++)
  658. {
  659. if (PathEx.Compare(recentProjects[i].path, projectPath))
  660. {
  661. recentProjects[i].accessTimestamp = (ulong)DateTime.Now.Ticks;
  662. EditorSettings.RecentProjects = recentProjects;
  663. foundPath = true;
  664. break;
  665. }
  666. }
  667. if (!foundPath)
  668. {
  669. List<RecentProject> extendedRecentProjects = new List<RecentProject>();
  670. extendedRecentProjects.AddRange(recentProjects);
  671. RecentProject newProject = new RecentProject();
  672. newProject.path = projectPath;
  673. newProject.accessTimestamp = (ulong)DateTime.Now.Ticks;
  674. extendedRecentProjects.Add(newProject);
  675. EditorSettings.RecentProjects = extendedRecentProjects.ToArray();
  676. }
  677. EditorSettings.LastOpenProject = projectPath;
  678. EditorSettings.Save();
  679. ProjectLibrary.Refresh();
  680. monitor = new FolderMonitor(ProjectLibrary.ResourceFolder);
  681. monitor.OnAdded += OnAssetModified;
  682. monitor.OnRemoved += OnAssetModified;
  683. monitor.OnModified += OnAssetModified;
  684. if (!string.IsNullOrWhiteSpace(ProjectSettings.LastOpenScene))
  685. {
  686. Scene.Load(ProjectSettings.LastOpenScene);
  687. SetSceneDirty(false);
  688. }
  689. }
  690. /// <summary>
  691. /// Triggered by the runtime when the user clicks on the status bar.
  692. /// </summary>
  693. private static void Internal_OnStatusBarClicked()
  694. {
  695. EditorWindow.OpenWindow<LogWindow>();
  696. }
  697. [MethodImpl(MethodImplOptions.InternalCall)]
  698. private static extern void Internal_SetStatusScene(string name, bool modified);
  699. [MethodImpl(MethodImplOptions.InternalCall)]
  700. private static extern void Internal_SetStatusProject(bool modified);
  701. [MethodImpl(MethodImplOptions.InternalCall)]
  702. private static extern void Internal_SetStatusCompiling(bool compiling);
  703. [MethodImpl(MethodImplOptions.InternalCall)]
  704. private static extern string Internal_GetProjectPath();
  705. [MethodImpl(MethodImplOptions.InternalCall)]
  706. private static extern string Internal_GetProjectName();
  707. [MethodImpl(MethodImplOptions.InternalCall)]
  708. private static extern bool Internal_GetProjectLoaded();
  709. [MethodImpl(MethodImplOptions.InternalCall)]
  710. private static extern string Internal_GetCompilerPath();
  711. [MethodImpl(MethodImplOptions.InternalCall)]
  712. private static extern string Internal_GetBuiltinReleaseAssemblyPath();
  713. [MethodImpl(MethodImplOptions.InternalCall)]
  714. private static extern string Internal_GetBuiltinDebugAssemblyPath();
  715. [MethodImpl(MethodImplOptions.InternalCall)]
  716. private static extern string Internal_GetScriptAssemblyPath();
  717. [MethodImpl(MethodImplOptions.InternalCall)]
  718. private static extern string Internal_GetFrameworkAssemblyPath();
  719. [MethodImpl(MethodImplOptions.InternalCall)]
  720. private static extern string Internal_GetEngineAssemblyName();
  721. [MethodImpl(MethodImplOptions.InternalCall)]
  722. private static extern string Internal_GetEditorAssemblyName();
  723. [MethodImpl(MethodImplOptions.InternalCall)]
  724. private static extern string Internal_GetScriptGameAssemblyName();
  725. [MethodImpl(MethodImplOptions.InternalCall)]
  726. private static extern string Internal_GetScriptEditorAssemblyName();
  727. [MethodImpl(MethodImplOptions.InternalCall)]
  728. private static extern Prefab Internal_SaveScene(string path);
  729. [MethodImpl(MethodImplOptions.InternalCall)]
  730. private static extern bool Internal_IsValidProject(string path);
  731. [MethodImpl(MethodImplOptions.InternalCall)]
  732. private static extern void Internal_SaveProject();
  733. [MethodImpl(MethodImplOptions.InternalCall)]
  734. private static extern void Internal_LoadProject(string path);
  735. [MethodImpl(MethodImplOptions.InternalCall)]
  736. private static extern void Internal_UnloadProject();
  737. [MethodImpl(MethodImplOptions.InternalCall)]
  738. private static extern void Internal_CreateProject(string path);
  739. [MethodImpl(MethodImplOptions.InternalCall)]
  740. private static extern void Internal_ReloadAssemblies();
  741. [MethodImpl(MethodImplOptions.InternalCall)]
  742. private static extern void Internal_OpenExternally(string path);
  743. [MethodImpl(MethodImplOptions.InternalCall)]
  744. private static extern void Internal_RunUnitTests();
  745. [MethodImpl(MethodImplOptions.InternalCall)]
  746. private static extern void Internal_Quit();
  747. [MethodImpl(MethodImplOptions.InternalCall)]
  748. private static extern void Internal_ToggleToolbarItem(string name, bool on);
  749. [MethodImpl(MethodImplOptions.InternalCall)]
  750. private static extern bool Internal_GetIsPlaying();
  751. [MethodImpl(MethodImplOptions.InternalCall)]
  752. private static extern void Internal_SetIsPlaying(bool value);
  753. [MethodImpl(MethodImplOptions.InternalCall)]
  754. private static extern bool Internal_GetIsPaused();
  755. [MethodImpl(MethodImplOptions.InternalCall)]
  756. private static extern void Internal_SetIsPaused(bool value);
  757. [MethodImpl(MethodImplOptions.InternalCall)]
  758. private static extern void Internal_FrameStep();
  759. [MethodImpl(MethodImplOptions.InternalCall)]
  760. private static extern void Internal_SetMainRenderTarget(IntPtr rendertarget);
  761. [MethodImpl(MethodImplOptions.InternalCall)]
  762. private static extern bool Internal_HasFocus();
  763. }
  764. }