EditorApplication.cs 31 KB

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