EditorApplication.cs 32 KB

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