EditorApplication.cs 37 KB

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