EditorApplication.cs 33 KB

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