EditorApplication.cs 31 KB

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