EditorApplication.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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 bool sceneDirty;
  194. private static bool unitTestsExecuted;
  195. private static EditorPersistentData persistentData;
  196. /// <summary>
  197. /// Constructs a new editor application. Called at editor start-up by the runtime, and any time assembly refresh
  198. /// occurrs.
  199. /// </summary>
  200. internal EditorApplication()
  201. {
  202. instance = this;
  203. codeManager = new ScriptCodeManager();
  204. const string soName = "EditorPersistentData";
  205. SceneObject so = Scene.Root.FindChild(soName);
  206. if (so == null)
  207. so = new SceneObject(soName, true);
  208. persistentData = so.GetComponent<EditorPersistentData>();
  209. if (persistentData == null)
  210. persistentData = so.AddComponent<EditorPersistentData>();
  211. // Register controls
  212. InputConfiguration inputConfig = VirtualInput.KeyConfig;
  213. inputConfig.RegisterButton(SceneCamera.MoveForwardBinding, ButtonCode.W);
  214. inputConfig.RegisterButton(SceneCamera.MoveBackBinding, ButtonCode.S);
  215. inputConfig.RegisterButton(SceneCamera.MoveLeftBinding, ButtonCode.A);
  216. inputConfig.RegisterButton(SceneCamera.MoveRightBinding, ButtonCode.D);
  217. inputConfig.RegisterButton(SceneCamera.MoveUpBinding, ButtonCode.E);
  218. inputConfig.RegisterButton(SceneCamera.MoveDownBinding, ButtonCode.Q);
  219. inputConfig.RegisterButton(SceneCamera.MoveForwardBinding, ButtonCode.Up);
  220. inputConfig.RegisterButton(SceneCamera.MoveBackBinding, ButtonCode.Down);
  221. inputConfig.RegisterButton(SceneCamera.MoveLeftBinding, ButtonCode.Left);
  222. inputConfig.RegisterButton(SceneCamera.MoveRightBinding, ButtonCode.Right);
  223. inputConfig.RegisterButton(SceneCamera.FastMoveBinding, ButtonCode.LeftShift);
  224. inputConfig.RegisterButton(SceneCamera.RotateBinding, ButtonCode.MouseRight);
  225. inputConfig.RegisterButton(SceneCamera.PanBinding, ButtonCode.MouseMiddle);
  226. inputConfig.RegisterAxis(SceneCamera.HorizontalAxisBinding, InputAxis.MouseX);
  227. inputConfig.RegisterAxis(SceneCamera.VerticalAxisBinding, InputAxis.MouseY);
  228. inputConfig.RegisterAxis(SceneCamera.ScrollAxisBinding, InputAxis.MouseZ);
  229. inputConfig.RegisterButton(SceneWindow.ToggleProfilerOverlayBinding, ButtonCode.P, ButtonModifier.CtrlAlt);
  230. inputConfig.RegisterButton(SceneWindow.ViewToolBinding, ButtonCode.Q);
  231. inputConfig.RegisterButton(SceneWindow.FrameBinding, ButtonCode.F);
  232. inputConfig.RegisterButton(SceneWindow.MoveToolBinding, ButtonCode.W);
  233. inputConfig.RegisterButton(SceneWindow.RotateToolBinding, ButtonCode.E);
  234. inputConfig.RegisterButton(SceneWindow.ScaleToolBinding, ButtonCode.R);
  235. inputConfig.RegisterButton(SceneWindow.DuplicateBinding, ButtonCode.D, ButtonModifier.Ctrl);
  236. if (IsProjectLoaded)
  237. {
  238. monitor = new FolderMonitor(ProjectLibrary.ResourceFolder);
  239. monitor.OnAdded += OnAssetModified;
  240. monitor.OnRemoved += OnAssetModified;
  241. monitor.OnModified += OnAssetModified;
  242. }
  243. }
  244. /// <summary>
  245. /// Triggered when the folder monitor detects an asset in the monitored folder was modified.
  246. /// </summary>
  247. /// <param name="path">Path to the modified file or folder.</param>
  248. private static void OnAssetModified(string path)
  249. {
  250. ProjectLibrary.Refresh(path);
  251. }
  252. /// <summary>
  253. /// Called 60 times per second by the runtime.
  254. /// </summary>
  255. internal void OnEditorUpdate()
  256. {
  257. ProjectLibrary.Update();
  258. codeManager.Update();
  259. }
  260. /// <summary>
  261. /// Creates a new empty scene.
  262. /// </summary>
  263. [MenuItem("File/New Scene", 10051, true)]
  264. private static void NewScene()
  265. {
  266. LoadScene(null);
  267. }
  268. /// <summary>
  269. /// Opens a dialog that allows the user to select a new prefab to load as the current scene. If current scene
  270. /// is modified the user is offered a chance to save it.
  271. /// </summary>
  272. [MenuItem("File/Open Scene", ButtonModifier.Ctrl, ButtonCode.L, 10050)]
  273. private static void LoadScene()
  274. {
  275. string[] scenePaths;
  276. if (BrowseDialog.OpenFile(ProjectLibrary.ResourceFolder, "", false, out scenePaths))
  277. {
  278. if (scenePaths.Length > 0)
  279. LoadScene(scenePaths[0]);
  280. }
  281. }
  282. /// <summary>
  283. /// Opens a dialog to allows the user to select a location where to save the current scene. If scene was previously
  284. /// saved it is instead automatically saved at the last location.
  285. /// </summary>
  286. public static void SaveScene(Action onSuccess = null, Action onFailure = null)
  287. {
  288. if (!string.IsNullOrEmpty(Scene.ActiveSceneUUID))
  289. {
  290. string scenePath = ProjectLibrary.GetPath(Scene.ActiveSceneUUID);
  291. if (!string.IsNullOrEmpty(scenePath))
  292. {
  293. SaveScene(scenePath);
  294. if (onSuccess != null)
  295. onSuccess();
  296. }
  297. else
  298. SaveSceneAs(onSuccess, onFailure);
  299. }
  300. else
  301. SaveSceneAs(onSuccess, onFailure);
  302. }
  303. /// <summary>
  304. /// Opens a dialog to allows the user to select a location where to save the current scene.
  305. /// </summary>
  306. public static void SaveSceneAs(Action onSuccess = null, Action onFailure = null)
  307. {
  308. string scenePath = "";
  309. if (BrowseDialog.SaveFile(ProjectLibrary.ResourceFolder, "*.prefab", out scenePath))
  310. {
  311. if (!PathEx.IsPartOf(scenePath, ProjectLibrary.ResourceFolder))
  312. {
  313. DialogBox.Open("Error", "The location must be inside the Resources folder of the project.",
  314. DialogBox.Type.OK,
  315. x =>
  316. {
  317. if (onFailure != null)
  318. onFailure();
  319. });
  320. }
  321. else
  322. {
  323. // TODO - If path points to an existing non-scene asset or folder I should delete it otherwise
  324. // Internal_SaveScene will silently fail.
  325. scenePath = Path.ChangeExtension(scenePath, ".prefab");
  326. SaveScene(scenePath);
  327. }
  328. }
  329. else
  330. {
  331. // User canceled, so technically a success
  332. if (onSuccess != null)
  333. onSuccess();
  334. }
  335. }
  336. /// <summary>
  337. /// Loads a prefab as the current scene at the specified path. If current scene is modified the user is offered a
  338. /// chance to save it.
  339. /// </summary>
  340. /// <param name="path">Path to a valid prefab relative to the resource folder. If path is empty a brand new
  341. /// scene will be loaded.</param>
  342. public static void LoadScene(string path)
  343. {
  344. Action<string> continueLoad =
  345. (scenePath) =>
  346. {
  347. if (string.IsNullOrEmpty(path))
  348. Scene.Clear();
  349. else
  350. Scene.Load(path);
  351. SetSceneDirty(false);
  352. ProjectSettings.LastOpenScene = scenePath;
  353. ProjectSettings.Save();
  354. };
  355. Action<DialogBox.ResultType> dialogCallback =
  356. (result) =>
  357. {
  358. if (result == DialogBox.ResultType.Yes)
  359. {
  360. SaveScene();
  361. continueLoad(path);
  362. }
  363. else if (result == DialogBox.ResultType.No)
  364. continueLoad(path);
  365. };
  366. if (IsSceneModified())
  367. {
  368. DialogBox.Open("Warning", "You current scene has modifications. Do you wish to save them first?",
  369. DialogBox.Type.YesNoCancel, dialogCallback);
  370. }
  371. else
  372. continueLoad(path);
  373. }
  374. /// <summary>
  375. /// Saves the currently loaded scene to the specified path.
  376. /// </summary>
  377. /// <param name="path">Path relative to the resource folder. This can be the path to the existing scene
  378. /// prefab if it just needs updating. </param>
  379. public static void SaveScene(string path)
  380. {
  381. Prefab scene = Internal_SaveScene(path);
  382. Scene.SetActive(scene);
  383. ProjectLibrary.Refresh(true);
  384. SetSceneDirty(false);
  385. }
  386. /// <summary>
  387. /// Checks does the folder at the provieded path contain a valid project.
  388. /// </summary>
  389. /// <param name="path">Absolute path to the root project folder.</param>
  390. /// <returns>True if the folder contains a valid project.</returns>
  391. public static bool IsValidProject(string path)
  392. {
  393. return Internal_IsValidProject(path);
  394. }
  395. /// <summary>
  396. /// Contains a new project in the provided folder.
  397. /// </summary>
  398. /// <param name="path">Absolute path to the folder to create the project in. Name of this folder will be used as the
  399. /// project's name.</param>
  400. public static void CreateProject(string path)
  401. {
  402. Internal_CreateProject(path);
  403. }
  404. /// <summary>
  405. /// Wrapper for menu items for <see cref="SaveScene(Action, Action)"/> method
  406. /// </summary>
  407. [MenuItem("File/Save Scene", ButtonModifier.Ctrl, ButtonCode.S, 10049)]
  408. [ToolbarItem("Save Scene", ToolbarIcon.SaveScene, "Save scene (Ctrl + S)", 1998)]
  409. private static void SaveSceneMenu()
  410. {
  411. SaveScene();
  412. }
  413. /// <summary>
  414. /// Wrapper for menu items for <see cref="SaveSceneAs(Action, Action)"/> method
  415. /// </summary>
  416. [MenuItem("File/Save Scene As", 10048)]
  417. private static void SaveSceneAsMenu()
  418. {
  419. SaveSceneAs();
  420. }
  421. /// <summary>
  422. /// Opens a Project Window allowing you to browse for or create a project.
  423. /// </summary>
  424. [MenuItem("File/Open Project", 10100)]
  425. [ToolbarItem("Open Project", ToolbarIcon.OpenProject, "Project manager", 2000)]
  426. public static void BrowseForProject()
  427. {
  428. ProjectWindow.Open();
  429. }
  430. /// <summary>
  431. /// Saves all data in the currently open project.
  432. /// </summary>
  433. [MenuItem("File/Save Project", 10099)]
  434. [ToolbarItem("Save Project", ToolbarIcon.SaveProject, "Save project", 1999)]
  435. public static void SaveProject()
  436. {
  437. foreach (var KVP in persistentData.dirtyResources)
  438. {
  439. string resourceUUID = KVP.Key;
  440. string path = ProjectLibrary.GetPath(resourceUUID);
  441. if (!IsNative(path))
  442. continue; // Native resources can't be changed
  443. Resource resource = ProjectLibrary.Load<Resource>(path);
  444. if(resource != null)
  445. ProjectLibrary.Save(resource);
  446. }
  447. persistentData.dirtyResources.Clear();
  448. SetStatusProject(false);
  449. Internal_SaveProject();
  450. }
  451. /// <summary>
  452. /// Loads the project at the specified path. This method executes asynchronously.
  453. /// </summary>
  454. /// <param name="path">Absolute path to the project's root folder.</param>
  455. public static void LoadProject(string path)
  456. {
  457. if (IsProjectLoaded && path == ProjectPath)
  458. return;
  459. if (!Internal_IsValidProject(path))
  460. {
  461. Debug.LogWarning("Provided path: \"" + path + "\" is not a valid project.");
  462. return;
  463. }
  464. if (IsProjectLoaded)
  465. UnloadProject();
  466. Internal_LoadProject(path); // Triggers Internal_OnProjectLoaded when done
  467. }
  468. /// <summary>
  469. /// Closes the editor.
  470. /// </summary>
  471. public static void Quit()
  472. {
  473. Internal_Quit();
  474. }
  475. /// <summary>
  476. /// Toggles an existing toolbar button into an on or off state which changes the visuals of the button.
  477. /// </summary>
  478. /// <param name="name">Name of the existing button to toggle</param>
  479. /// <param name="on">True to toggle on, false to toggle off (default)</param>
  480. public static void ToggleToolbarItem(string name, bool on)
  481. {
  482. Internal_ToggleToolbarItem(name, on);
  483. }
  484. /// <summary>
  485. /// Opens a file or a folder in the default external application.
  486. /// </summary>
  487. /// <param name="path">Absolute path to the file or folder to open.</param>
  488. public static void OpenExternally(string path)
  489. {
  490. Internal_OpenExternally(path);
  491. }
  492. /// <summary>
  493. /// Marks a resource as dirty so that it may be saved the next time the project is saved. Optionally you may also
  494. /// call <see cref="ProjectLibrary.Save"/> to save it immediately.
  495. /// </summary>
  496. /// <param name="resource">Resource to mark as dirty</param>
  497. public static void SetDirty(Resource resource)
  498. {
  499. if (resource == null)
  500. return;
  501. SetStatusProject(true);
  502. persistentData.dirtyResources[resource.UUID] = true;
  503. }
  504. /// <summary>
  505. /// Marks the current scene as dirty.
  506. /// </summary>
  507. public static void SetSceneDirty()
  508. {
  509. SetSceneDirty(true);
  510. }
  511. /// <summary>
  512. /// Marks the current scene as clean or dirty.
  513. /// </summary>
  514. /// <param name="dirty">Should the scene be marked as clean or dirty.</param>
  515. internal static void SetSceneDirty(bool dirty)
  516. {
  517. sceneDirty = dirty;
  518. SetStatusScene(Scene.ActiveSceneName, dirty);
  519. if (!dirty && Scene.ActiveSceneUUID != null)
  520. persistentData.dirtyResources.Remove(Scene.ActiveSceneUUID);
  521. }
  522. /// <summary>
  523. /// Checks is the specific resource dirty and needs saving.
  524. /// </summary>
  525. /// <param name="resource">Resource to check.</param>
  526. /// <returns>True if the resource requires saving, false otherwise.</returns>
  527. public static bool IsDirty(Resource resource)
  528. {
  529. return persistentData.dirtyResources.ContainsKey(resource.UUID);
  530. }
  531. /// <summary>
  532. /// Checks does the path represent a native resource.
  533. /// </summary>
  534. /// <param name="path">Filename or path to check.</param>
  535. /// <returns>True if the path represents a native resource.</returns>
  536. public static bool IsNative(string path)
  537. {
  538. string extension = Path.GetExtension(path);
  539. return extension == ".asset" || extension == ".prefab";
  540. }
  541. /// <summary>
  542. /// Unloads the currently loaded project. Offers the user a chance to save the current scene if it is modified.
  543. /// Automatically saves all project data before unloading.
  544. /// </summary>
  545. private static void UnloadProject()
  546. {
  547. Action continueUnload =
  548. () =>
  549. {
  550. Scene.Clear();
  551. if (monitor != null)
  552. {
  553. monitor.Destroy();
  554. monitor = null;
  555. }
  556. LibraryWindow window = EditorWindow.GetWindow<LibraryWindow>();
  557. if(window != null)
  558. window.Reset();
  559. SetSceneDirty(false);
  560. Internal_UnloadProject();
  561. SetStatusProject(false);
  562. };
  563. Action<DialogBox.ResultType> dialogCallback =
  564. (result) =>
  565. {
  566. if (result == DialogBox.ResultType.Yes)
  567. SaveScene();
  568. continueUnload();
  569. };
  570. if (IsSceneModified())
  571. {
  572. DialogBox.Open("Warning", "You current scene has modifications. Do you wish to save them first?",
  573. DialogBox.Type.YesNoCancel, dialogCallback);
  574. }
  575. else
  576. continueUnload();
  577. }
  578. /// <summary>
  579. /// Reloads all script assemblies in case they were modified. This action is delayed and will be executed
  580. /// at the beginning of the next frame.
  581. /// </summary>
  582. public static void ReloadAssemblies()
  583. {
  584. Internal_ReloadAssemblies();
  585. }
  586. /// <summary>
  587. /// Changes the scene displayed on the status bar.
  588. /// </summary>
  589. /// <param name="name">Name of the scene.</param>
  590. /// <param name="modified">Whether to display the scene as modified or not.</param>
  591. private static void SetStatusScene(string name, bool modified)
  592. {
  593. Internal_SetStatusScene(name, modified);
  594. }
  595. /// <summary>
  596. /// Changes the project state displayed on the status bar.
  597. /// </summary>
  598. /// <param name="modified">Whether to display the project as modified or not.</param>
  599. private static void SetStatusProject(bool modified)
  600. {
  601. Internal_SetStatusProject(modified);
  602. }
  603. /// <summary>
  604. /// Displays or hides the "compilation in progress" visual on the status bar.
  605. /// </summary>
  606. /// <param name="compiling">True to display the visual, false otherwise.</param>
  607. internal static void SetStatusCompiling(bool compiling)
  608. {
  609. Internal_SetStatusCompiling(compiling);
  610. }
  611. /// <summary>
  612. /// Checks did we make any modifications to the scene since it was last saved.
  613. /// </summary>
  614. /// <returns>True if the scene was never saved, or was modified after last save.</returns>
  615. public static bool IsSceneModified()
  616. {
  617. return sceneDirty;
  618. }
  619. /// <summary>
  620. /// Runs a single frame of the game and pauses it. If the game is not currently running it will be started.
  621. /// </summary>
  622. public static void FrameStep()
  623. {
  624. if (IsStopped)
  625. {
  626. if (EditorSettings.GetBool(LogWindow.CLEAR_ON_PLAY_KEY, true))
  627. {
  628. Debug.Clear();
  629. LogWindow log = EditorWindow.GetWindow<LogWindow>();
  630. if (log != null)
  631. log.Refresh();
  632. }
  633. }
  634. ToggleToolbarItem("Play", false);
  635. ToggleToolbarItem("Pause", true);
  636. Internal_FrameStep();
  637. }
  638. /// <summary>
  639. /// Executes any editor-specific unit tests. This should be called after a project is loaded if possible.
  640. /// </summary>
  641. private static void RunUnitTests()
  642. {
  643. #if DEBUG
  644. Internal_RunUnitTests();
  645. #endif
  646. }
  647. /// <summary>
  648. /// Triggered by the runtime when <see cref="LoadProject"/> method completes.
  649. /// </summary>
  650. private static void Internal_OnProjectLoaded()
  651. {
  652. SetStatusProject(false);
  653. if (!unitTestsExecuted)
  654. {
  655. RunUnitTests();
  656. unitTestsExecuted = true;
  657. }
  658. if (!IsProjectLoaded)
  659. {
  660. ProjectWindow.Open();
  661. return;
  662. }
  663. string projectPath = ProjectPath;
  664. RecentProject[] recentProjects = EditorSettings.RecentProjects;
  665. bool foundPath = false;
  666. for (int i = 0; i < recentProjects.Length; i++)
  667. {
  668. if (PathEx.Compare(recentProjects[i].path, projectPath))
  669. {
  670. recentProjects[i].accessTimestamp = (ulong)DateTime.Now.Ticks;
  671. EditorSettings.RecentProjects = recentProjects;
  672. foundPath = true;
  673. break;
  674. }
  675. }
  676. if (!foundPath)
  677. {
  678. List<RecentProject> extendedRecentProjects = new List<RecentProject>();
  679. extendedRecentProjects.AddRange(recentProjects);
  680. RecentProject newProject = new RecentProject();
  681. newProject.path = projectPath;
  682. newProject.accessTimestamp = (ulong)DateTime.Now.Ticks;
  683. extendedRecentProjects.Add(newProject);
  684. EditorSettings.RecentProjects = extendedRecentProjects.ToArray();
  685. }
  686. EditorSettings.LastOpenProject = projectPath;
  687. EditorSettings.Save();
  688. ProjectLibrary.Refresh();
  689. monitor = new FolderMonitor(ProjectLibrary.ResourceFolder);
  690. monitor.OnAdded += OnAssetModified;
  691. monitor.OnRemoved += OnAssetModified;
  692. monitor.OnModified += OnAssetModified;
  693. if (!string.IsNullOrWhiteSpace(ProjectSettings.LastOpenScene))
  694. {
  695. Scene.Load(ProjectSettings.LastOpenScene);
  696. SetSceneDirty(false);
  697. }
  698. }
  699. /// <summary>
  700. /// Triggered by the runtime when the user clicks on the status bar.
  701. /// </summary>
  702. private static void Internal_OnStatusBarClicked()
  703. {
  704. EditorWindow.OpenWindow<LogWindow>();
  705. }
  706. [MethodImpl(MethodImplOptions.InternalCall)]
  707. private static extern void Internal_SetStatusScene(string name, bool modified);
  708. [MethodImpl(MethodImplOptions.InternalCall)]
  709. private static extern void Internal_SetStatusProject(bool modified);
  710. [MethodImpl(MethodImplOptions.InternalCall)]
  711. private static extern void Internal_SetStatusCompiling(bool compiling);
  712. [MethodImpl(MethodImplOptions.InternalCall)]
  713. private static extern string Internal_GetProjectPath();
  714. [MethodImpl(MethodImplOptions.InternalCall)]
  715. private static extern string Internal_GetProjectName();
  716. [MethodImpl(MethodImplOptions.InternalCall)]
  717. private static extern bool Internal_GetProjectLoaded();
  718. [MethodImpl(MethodImplOptions.InternalCall)]
  719. private static extern string Internal_GetCompilerPath();
  720. [MethodImpl(MethodImplOptions.InternalCall)]
  721. private static extern string Internal_GetBuiltinReleaseAssemblyPath();
  722. [MethodImpl(MethodImplOptions.InternalCall)]
  723. private static extern string Internal_GetBuiltinDebugAssemblyPath();
  724. [MethodImpl(MethodImplOptions.InternalCall)]
  725. private static extern string Internal_GetScriptAssemblyPath();
  726. [MethodImpl(MethodImplOptions.InternalCall)]
  727. private static extern string Internal_GetFrameworkAssemblyPath();
  728. [MethodImpl(MethodImplOptions.InternalCall)]
  729. private static extern string Internal_GetEngineAssemblyName();
  730. [MethodImpl(MethodImplOptions.InternalCall)]
  731. private static extern string Internal_GetEditorAssemblyName();
  732. [MethodImpl(MethodImplOptions.InternalCall)]
  733. private static extern string Internal_GetScriptGameAssemblyName();
  734. [MethodImpl(MethodImplOptions.InternalCall)]
  735. private static extern string Internal_GetScriptEditorAssemblyName();
  736. [MethodImpl(MethodImplOptions.InternalCall)]
  737. private static extern Prefab Internal_SaveScene(string path);
  738. [MethodImpl(MethodImplOptions.InternalCall)]
  739. private static extern bool Internal_IsValidProject(string path);
  740. [MethodImpl(MethodImplOptions.InternalCall)]
  741. private static extern void Internal_SaveProject();
  742. [MethodImpl(MethodImplOptions.InternalCall)]
  743. private static extern void Internal_LoadProject(string path);
  744. [MethodImpl(MethodImplOptions.InternalCall)]
  745. private static extern void Internal_UnloadProject();
  746. [MethodImpl(MethodImplOptions.InternalCall)]
  747. private static extern void Internal_CreateProject(string path);
  748. [MethodImpl(MethodImplOptions.InternalCall)]
  749. private static extern void Internal_ReloadAssemblies();
  750. [MethodImpl(MethodImplOptions.InternalCall)]
  751. private static extern void Internal_OpenExternally(string path);
  752. [MethodImpl(MethodImplOptions.InternalCall)]
  753. private static extern void Internal_RunUnitTests();
  754. [MethodImpl(MethodImplOptions.InternalCall)]
  755. private static extern void Internal_Quit();
  756. [MethodImpl(MethodImplOptions.InternalCall)]
  757. private static extern void Internal_ToggleToolbarItem(string name, bool on);
  758. [MethodImpl(MethodImplOptions.InternalCall)]
  759. private static extern bool Internal_GetIsPlaying();
  760. [MethodImpl(MethodImplOptions.InternalCall)]
  761. private static extern void Internal_SetIsPlaying(bool value);
  762. [MethodImpl(MethodImplOptions.InternalCall)]
  763. private static extern bool Internal_GetIsPaused();
  764. [MethodImpl(MethodImplOptions.InternalCall)]
  765. private static extern void Internal_SetIsPaused(bool value);
  766. [MethodImpl(MethodImplOptions.InternalCall)]
  767. private static extern void Internal_FrameStep();
  768. [MethodImpl(MethodImplOptions.InternalCall)]
  769. private static extern void Internal_SetMainRenderTarget(IntPtr rendertarget);
  770. [MethodImpl(MethodImplOptions.InternalCall)]
  771. private static extern bool Internal_HasFocus();
  772. }
  773. }