| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227 |
- //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
- //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
- using System;
- using System.Collections.Generic;
- using System.Runtime.CompilerServices;
- using System.IO;
- using bs;
- namespace bs.Editor
- {
- /** @addtogroup Editor-General
- * @{
- */
- /// <summary>
- /// Available tools in the scene view.
- /// </summary>
- public enum SceneViewTool
- {
- View,
- Move,
- Rotate,
- Scale
- }
- /// <summary>
- /// Pivot mode used by the scene view tools.
- /// </summary>
- public enum HandlePivotMode
- {
- Center,
- Pivot
- }
- /// <summary>
- /// Coordinate mode used by the scene view tools.
- /// </summary>
- public enum HandleCoordinateMode
- {
- Local,
- World
- }
- /// <summary>
- /// Manages various generic and global settings relating to the editor.
- /// </summary>
- public class EditorApplication
- {
- internal const string CutBinding = "Cut";
- internal const string CopyBinding = "Copy";
- internal const string RenameBinding = "Rename";
- internal const string DuplicateBinding = "Duplicate";
- internal const string DeleteBinding = "Delete";
- internal const string PasteBinding = "Paste";
- internal const string EditorSceneDataPrefix = "__EditorSceneData";
- /// <summary>
- /// Determines the active tool shown in the scene view.
- /// </summary>
- public static SceneViewTool ActiveSceneTool
- {
- get { return EditorSettings.ActiveSceneTool; }
- set { EditorSettings.ActiveSceneTool = value; }
- }
- /// <summary>
- /// Determines the coordinate mode used by the tools in the scene view.
- /// </summary>
- public static HandleCoordinateMode ActiveCoordinateMode
- {
- get { return EditorSettings.ActiveCoordinateMode; }
- set { EditorSettings.ActiveCoordinateMode = value; }
- }
- /// <summary>
- /// Determines the pivot mode used by the tools in the scene view.
- /// </summary>
- public static HandlePivotMode ActivePivotMode
- {
- get { return EditorSettings.ActivePivotMode; }
- set { EditorSettings.ActivePivotMode = value; }
- }
- /// <summary>
- /// Camera used for rendering the scene view.
- /// </summary>
- public static Camera SceneViewCamera
- {
- get
- {
- SceneWindow sceneWindow = EditorWindow.GetWindow<SceneWindow>();
- return sceneWindow?.Camera.Camera;
- }
- }
- /// <summary>
- /// Absolute path to the folder containing the currently open project.
- /// </summary>
- public static string ProjectPath { get { return Internal_GetProjectPath(); } }
- /// <summary>
- /// Name of the currently open project.
- /// </summary>
- public static string ProjectName { get { return Internal_GetProjectName(); } }
- /// <summary>
- /// Checks is any project currently loaded.
- /// </summary>
- public static bool IsProjectLoaded { get { return Internal_GetProjectLoaded(); } }
- /// <summary>
- /// Checks whether the editor currently has focus.
- /// </summary>
- public static bool HasFocus
- {
- get { return Internal_HasFocus(); }
- }
- /// <summary>
- /// Returns true if the editor is waiting on a scene to be asynchronously loaded.
- /// </summary>
- public static bool IsSceneLoading
- {
- get
- {
- if (lastLoadedScene != null)
- return !lastLoadedScene.IsLoaded;
- return false;
- }
- }
- /// <summary>
- /// Returns the load progress of the scene that's being asynchronously loaded
- /// </summary>
- public static float SceneLoadProgress
- {
- get
- {
- if (lastLoadedScene != null)
- return Resources.GetLoadProgress(lastLoadedScene);
- return 0.0f;
- }
- }
- /// <summary>
- /// Triggered right before the project is being saved.
- /// </summary>
- public static event Action OnProjectSave;
- /// <summary>
- /// Render target that the main camera in the scene (if any) will render its view to. This generally means the main
- /// game window when running standalone, or the Game viewport when running in editor.
- /// </summary>
- internal static RenderTarget MainRenderTarget
- {
- set
- {
- IntPtr rtPtr = IntPtr.Zero;
- if (value != null)
- rtPtr = value.GetCachedPtr();
- Internal_SetMainRenderTarget(rtPtr);
- }
- }
- /// <summary>
- /// Returns an object that can be used for storing data that persists throughout the entire editor session.
- /// </summary>
- internal static EditorPersistentData PersistentData => persistentData;
- /// <summary>
- /// Editor specific data for the currently loaded scene. Can be null if no scene is loaded.
- /// </summary>
- internal static EditorSceneData EditorSceneData
- {
- get => PersistentData.editorSceneData;
- private set => PersistentData.editorSceneData = value;
- }
- /// <summary>
- /// Returns the path where the script compiler is located at.
- /// </summary>
- internal static string CompilerPath { get { return Internal_GetCompilerPath(); } }
- /// <summary>
- /// Returns the absolute path to the executable capable of executing managed assemblies.
- /// </summary>
- internal static string MonoExecPath { get { return Internal_GetMonoExecPath(); } }
- /// <summary>
- /// Returns the path to the folder where the custom script assemblies are located at.
- /// </summary>
- internal static string ScriptAssemblyPath { get { return Internal_GetScriptAssemblyPath(); } }
- /// <summary>
- /// Returns the path to the folder where the .NET framework assemblies are located at.
- /// </summary>
- internal static string FrameworkAssemblyPath { get { return Internal_GetFrameworkAssemblyPath(); } }
- /// <summary>
- /// Name of the builtin assembly containing engine specific types.
- /// </summary>
- internal static string EngineAssemblyName { get { return Internal_GetEngineAssemblyName(); } }
- /// <summary>
- /// Name of the builtin assembly containing editor specific types.
- /// </summary>
- internal static string EditorAssemblyName { get { return Internal_GetEditorAssemblyName(); } }
- /// <summary>
- /// Name of the custom assembly compiled from non-editor scripts within the project.
- /// </summary>
- internal static string ScriptGameAssemblyName { get { return Internal_GetScriptGameAssemblyName(); } }
- /// <summary>
- /// Name of the custom assembly compiled from editor scripts within the project.
- /// </summary>
- internal static string ScriptEditorAssemblyName { get { return Internal_GetScriptEditorAssemblyName(); } }
- /// <summary>
- /// Returns the path to the folder where the builtin release script assemblies are located at.
- /// </summary>
- internal static string BuiltinReleaseAssemblyPath { get { return Internal_GetBuiltinReleaseAssemblyPath(); } }
- /// <summary>
- /// Returns the path to the folder where the builtin debug script assemblies are located at.
- /// </summary>
- internal static string BuiltinDebugAssemblyPath { get { return Internal_GetBuiltinDebugAssemblyPath(); } }
- internal static VirtualButton CutKey = new VirtualButton(CutBinding);
- internal static VirtualButton CopyKey = new VirtualButton(CopyBinding);
- internal static VirtualButton PasteKey = new VirtualButton(PasteBinding);
- internal static VirtualButton RenameKey = new VirtualButton(RenameBinding);
- internal static VirtualButton DuplicateKey = new VirtualButton(DuplicateBinding);
- internal static VirtualButton DeleteKey = new VirtualButton(DeleteBinding);
- private static FolderMonitor monitor;
- private static ScriptCodeManager codeManager;
- private static RRef<Prefab> lastLoadedScene;
- private static bool sceneDirty;
- private static bool unitTestsExecuted;
- private static EditorPersistentData persistentData;
- private static bool delayUnloadProject;
- private static Action delayUnloadCallback;
- #pragma warning disable 0414
- private static EditorApplication instance;
- #pragma warning restore 0414
- /// <summary>
- /// Constructs a new editor application. Called at editor start-up by the runtime, and any time assembly refresh
- /// occurs.
- /// </summary>
- internal EditorApplication()
- {
- instance = this;
- const string soName = "EditorPersistentData";
- SceneObject so = Scene.Root.FindChild(soName);
- if (so == null)
- so = new SceneObject(soName, true);
- persistentData = so.GetComponent<EditorPersistentData>();
- if (persistentData == null)
- persistentData = so.AddComponent<EditorPersistentData>();
- codeManager = new ScriptCodeManager();
- Scene.OnSceneLoad += OnSceneLoad;
- Scene.OnSceneUnload += OnSceneUnload;
- PlayInEditor.OnPlay += () =>
- {
- if (EditorSettings.GetBool(LogWindow.CLEAR_ON_PLAY_KEY, true))
- {
- Debug.Clear();
- LogWindow log = EditorWindow.GetWindow<LogWindow>();
- if (log != null)
- log.Refresh();
- }
- ToggleOnDemandDrawing(false);
- ToggleToolbarItem("Play", true);
- };
- PlayInEditor.OnStopped += () =>
- {
- ToggleOnDemandDrawing(true);
- ToggleToolbarItem("Play", false);
- };
- PlayInEditor.OnPaused += () =>
- {
- ToggleOnDemandDrawing(true);
- ToggleToolbarItem("Pause", true);
- };
- PlayInEditor.OnUnpaused += () =>
- {
- ToggleOnDemandDrawing(false);
- ToggleToolbarItem("Pause", false);
- };
- Selection.OnSelectionChanged += OnSelectionChanged;
- // Register controls
- InputConfiguration inputConfig = VirtualInput.KeyConfig;
- inputConfig.RegisterButton(SceneCamera.MoveForwardBinding, ButtonCode.W);
- inputConfig.RegisterButton(SceneCamera.MoveBackBinding, ButtonCode.S);
- inputConfig.RegisterButton(SceneCamera.MoveLeftBinding, ButtonCode.A);
- inputConfig.RegisterButton(SceneCamera.MoveRightBinding, ButtonCode.D);
- inputConfig.RegisterButton(SceneCamera.MoveUpBinding, ButtonCode.E);
- inputConfig.RegisterButton(SceneCamera.MoveDownBinding, ButtonCode.Q);
- inputConfig.RegisterButton(SceneCamera.MoveForwardBinding, ButtonCode.Up);
- inputConfig.RegisterButton(SceneCamera.MoveBackBinding, ButtonCode.Down);
- inputConfig.RegisterButton(SceneCamera.MoveLeftBinding, ButtonCode.Left);
- inputConfig.RegisterButton(SceneCamera.MoveRightBinding, ButtonCode.Right);
- inputConfig.RegisterButton(SceneCamera.FastMoveBinding, ButtonCode.LeftShift);
- inputConfig.RegisterButton(SceneCamera.RotateBinding, ButtonCode.MouseRight);
- inputConfig.RegisterButton(SceneCamera.PanBinding, ButtonCode.MouseMiddle);
- inputConfig.RegisterAxis(SceneCamera.HorizontalAxisBinding, InputAxis.MouseX);
- inputConfig.RegisterAxis(SceneCamera.VerticalAxisBinding, InputAxis.MouseY);
- inputConfig.RegisterAxis(SceneCamera.ScrollAxisBinding, InputAxis.MouseZ);
- inputConfig.RegisterButton(SceneWindow.ToggleProfilerOverlayBinding, ButtonCode.P, ButtonModifier.CtrlAlt);
- inputConfig.RegisterButton(SceneWindow.ViewToolBinding, ButtonCode.Q);
- inputConfig.RegisterButton(SceneWindow.FrameBinding, ButtonCode.F);
- inputConfig.RegisterButton(SceneWindow.MoveToolBinding, ButtonCode.W);
- inputConfig.RegisterButton(SceneWindow.RotateToolBinding, ButtonCode.E);
- inputConfig.RegisterButton(SceneWindow.ScaleToolBinding, ButtonCode.R);
- inputConfig.RegisterButton(CutBinding, ButtonCode.X, ButtonModifier.Ctrl);
- inputConfig.RegisterButton(CopyBinding, ButtonCode.C, ButtonModifier.Ctrl);
- inputConfig.RegisterButton(PasteBinding, ButtonCode.V, ButtonModifier.Ctrl);
- inputConfig.RegisterButton(DuplicateBinding, ButtonCode.D, ButtonModifier.Ctrl);
- inputConfig.RegisterButton(DeleteBinding, ButtonCode.Delete);
- inputConfig.RegisterButton(RenameBinding, ButtonCode.F2);
- if (IsProjectLoaded)
- {
- monitor = new FolderMonitor(ProjectLibrary.ResourceFolder);
- monitor.OnAdded += OnAssetModified;
- monitor.OnRemoved += OnAssetModified;
- monitor.OnModified += OnAssetModified;
- }
- }
- /// <summary>
- /// Updates <see cref="EditorSceneData"/> with the current state of the active scene.
- /// </summary>
- internal static void UpdateEditorSceneData()
- {
- if (EditorSceneData == null)
- EditorSceneData = EditorSceneData.FromScene(Scene.Root);
- else
- EditorSceneData.UpdateFromScene(Scene.Root);
- HierarchyWindow hierarcyWindow = EditorWindow.GetWindow<HierarchyWindow>();
- hierarcyWindow?.SaveHierarchyState(EditorSceneData);
- }
- /// <summary>
- /// Triggered whenever object or resource selection changes.
- /// </summary>
- private static void OnSelectionChanged(SceneObject[] sceneObjects, string[] resourcePaths)
- {
- NotifyNeedsRedraw();
- }
- /// <summary>
- /// Triggered when the scene has been loaded.
- /// </summary>
- /// <param name="uuid">UUID of the loaded scene.</param>
- private static void OnSceneUnload(UUID uuid)
- {
- UpdateEditorSceneData();
- string key = EditorSceneDataPrefix + uuid;
- ProjectSettings.SetObject(key, EditorSceneData);
- }
- /// <summary>
- /// Triggered when a scene is about to be unloaded.
- /// </summary>
- /// <param name="uuid">UUID of the scene to be unloaded.</param>
- private static void OnSceneLoad(UUID uuid)
- {
- string key = EditorSceneDataPrefix + uuid;
- EditorSceneData = ProjectSettings.GetObject<EditorSceneData>(key);
- if (EditorSceneData == null)
- EditorSceneData = EditorSceneData.FromScene(Scene.Root);
- }
- /// <summary>
- /// Triggered when the folder monitor detects an asset in the monitored folder was modified.
- /// </summary>
- /// <param name="path">Path to the modified file or folder.</param>
- private static void OnAssetModified(string path)
- {
- ProjectLibrary.Refresh(path);
- }
- /// <summary>
- /// Called every frame by the runtime.
- /// </summary>
- internal void OnEditorUpdate()
- {
- // Update managers
- ProjectLibrary.Update();
- codeManager.Update();
- if (delayUnloadProject)
- {
- delayUnloadProject = false;
- UnloadProject();
- delayUnloadCallback?.Invoke();
- delayUnloadCallback = null;
- }
- }
- /// <summary>
- /// Manually triggers a global shortcut.
- /// </summary>
- /// <param name="btn">Button for the shortcut. If this doesn't correspond to any shortcut, it is ignored.</param>
- internal static void TriggerGlobalShortcut(VirtualButton btn)
- {
- IGlobalShortcuts window = null;
- if (btn != PasteKey)
- {
- // The system ensures elsewhere that only either a resource or a scene object is selected, but not both
- if (Selection.ResourcePaths.Length > 0)
- {
- window = EditorWindow.GetWindow<LibraryWindow>();
- }
- else if (Selection.SceneObjects.Length > 0)
- {
- window = EditorWindow.GetWindow<HierarchyWindow>();
- if (window == null)
- window = EditorWindow.GetWindow<SceneWindow>();
- }
- if (window != null)
- {
- if (btn == CopyKey)
- window.OnCopyPressed();
- else if (btn == CutKey)
- window.OnCutPressed();
- else if (btn == PasteKey)
- window.OnPastePressed();
- else if (btn == DuplicateKey)
- window.OnDuplicatePressed();
- else if (btn == RenameKey)
- window.OnRenamePressed();
- else if (btn == DeleteKey)
- window.OnDeletePressed();
- }
- }
- else
- {
- HierarchyWindow hierarchy = EditorWindow.GetWindow<HierarchyWindow>();
- if (hierarchy != null && hierarchy.HasFocus)
- window = hierarchy;
- else
- {
- LibraryWindow library = EditorWindow.GetWindow<LibraryWindow>();
- if (library != null && library.HasFocus)
- window = library;
- }
- if (window != null)
- window.OnPastePressed();
- }
- }
- /// <summary>
- /// Creates a new empty scene.
- /// </summary>
- [MenuItem("File/New Scene", 10051, true)]
- private static void NewScene()
- {
- LoadScene(null);
- }
- /// <summary>
- /// Opens a dialog that allows the user to select a new prefab to load as the current scene. If current scene
- /// is modified the user is offered a chance to save it.
- /// </summary>
- [MenuItem("File/Open Scene", ButtonModifier.Ctrl, ButtonCode.L, 10050)]
- private static void LoadScene()
- {
- string[] scenePaths;
- if (BrowseDialog.OpenFile(ProjectLibrary.ResourceFolder, "", false, out scenePaths))
- {
- if (scenePaths.Length > 0)
- LoadScene(scenePaths[0]);
- }
- }
- /// <summary>
- /// Opens a dialog to allows the user to select a location where to save the current scene. If scene was previously
- /// saved it is instead automatically saved at the last location.
- /// </summary>
- public static void SaveScene(Action onSuccess = null, Action onFailure = null)
- {
- if (!Scene.ActiveSceneUUID.IsEmpty())
- {
- string scenePath = ProjectLibrary.GetPath(Scene.ActiveSceneUUID);
- if (!string.IsNullOrEmpty(scenePath))
- {
- if (Scene.IsGenericPrefab)
- {
- SaveGenericPrefab(onSuccess, onFailure);
- }
- else
- {
- SaveScene(scenePath);
- if (onSuccess != null)
- onSuccess();
- }
- }
- else
- SaveSceneAs(onSuccess, onFailure);
- }
- else
- SaveSceneAs(onSuccess, onFailure);
- }
- /// <summary>
- /// Opens a dialog to allows the user to select a location where to save the current scene.
- /// </summary>
- public static void SaveSceneAs(Action onSuccess = null, Action onFailure = null)
- {
- string scenePath = "";
- if (BrowseDialog.SaveFile(ProjectLibrary.ResourceFolder, "*.prefab", out scenePath))
- {
- if (!PathEx.IsPartOf(scenePath, ProjectLibrary.ResourceFolder))
- {
- DialogBox.Open("Error", "The location must be inside the Resources folder of the project.",
- DialogBox.Type.OK,
- x =>
- {
- if (onFailure != null)
- onFailure();
- });
- }
- else
- {
- // TODO - If path points to an existing non-scene asset or folder I should delete it otherwise
- // Internal_SaveScene will silently fail.
- scenePath = Path.ChangeExtension(scenePath, ".prefab");
- SaveScene(scenePath);
- }
- }
- else
- {
- // User canceled, so technically a success
- if (onSuccess != null)
- onSuccess();
- }
- }
- /// <summary>
- /// Loads a prefab as the current scene at the specified path. If current scene is modified the user is offered a
- /// chance to save it.
- /// </summary>
- /// <param name="path">Path to a valid prefab relative to the resource folder. If path is empty a brand new
- /// scene will be loaded.</param>
- public static void LoadScene(string path)
- {
- Action<string> continueLoad =
- (scenePath) =>
- {
- if (string.IsNullOrEmpty(path))
- {
- Scene.Clear();
- lastLoadedScene = null;
- }
- else
- lastLoadedScene = Scene.LoadAsync(path);
- SetSceneDirty(false);
- ProjectSettings.LastOpenScene = scenePath;
- ProjectSettings.Save();
- };
- Action<DialogBox.ResultType> dialogCallback =
- (result) =>
- {
- if (result == DialogBox.ResultType.Yes)
- {
- SaveScene();
- continueLoad(path);
- }
- else if (result == DialogBox.ResultType.No)
- continueLoad(path);
- };
- if (IsSceneModified())
- {
- DialogBox.Open("Warning", "You current scene has modifications. Do you wish to save them first?",
- DialogBox.Type.YesNoCancel, dialogCallback);
- }
- else
- continueLoad(path);
- }
- /// <summary>
- /// Saves the currently loaded scene to the specified path.
- /// </summary>
- /// <param name="path">Path relative to the resource folder. This can be the path to the existing scene
- /// prefab if it just needs updating. </param>
- internal static void SaveScene(string path)
- {
- Prefab scene = Internal_SaveScene(path);
- Scene.SetActive(scene);
- ProjectLibrary.Refresh(true);
- SetSceneDirty(false);
- }
- /// <summary>
- /// Attempts to save the current scene by applying the changes to a prefab, instead of saving it as a brand new
- /// scene. This is necessary for generic prefabs that have don't have a scene root included in the prefab. If the
- /// object added any other objects to the root, or has moved or deleted the original generic prefab the user
- /// will be asked to save the scene normally, creating a brand new prefab.
- /// </summary>
- private static void SaveGenericPrefab(Action onSuccess = null, Action onFailure = null)
- {
- // Find prefab root
- SceneObject root = null;
- int numChildren = Scene.Root.GetNumChildren();
- int numNormalChildren = 0;
- for (int i = 0; i < numChildren; i++)
- {
- SceneObject child = Scene.Root.GetChild(i);
- if (EditorUtility.IsInternal(child))
- continue;
- UUID prefabUUID = PrefabUtility.GetPrefabUUID(child);
- if (prefabUUID == Scene.ActiveSceneUUID)
- root = child;
- // If user added any other prefabs other than the initial one, the scene no longer represents a generic
- // prefab (as we can now longer save it by applying changes only to that prefab)
- numNormalChildren++;
- if (numNormalChildren > 1)
- {
- root = null;
- break;
- }
- }
-
- if (root != null)
- {
- PrefabUtility.ApplyPrefab(root, false);
- ProjectLibrary.Refresh(true);
- SetSceneDirty(false);
- if (onSuccess != null)
- onSuccess();
- }
- else
- {
- SaveSceneAs(onSuccess, onFailure);
- }
- }
- /// <summary>
- /// Checks does the folder at the provieded path contain a valid project.
- /// </summary>
- /// <param name="path">Absolute path to the root project folder.</param>
- /// <returns>True if the folder contains a valid project.</returns>
- public static bool IsValidProject(string path)
- {
- return Internal_IsValidProject(path);
- }
- /// <summary>
- /// Contains a new project in the provided folder.
- /// </summary>
- /// <param name="path">Absolute path to the folder to create the project in. Name of this folder will be used as the
- /// project's name.</param>
- public static void CreateProject(string path)
- {
- Internal_CreateProject(path);
- }
- /// <summary>
- /// Wrapper for menu items for <see cref="SaveScene(Action, Action)"/> method
- /// </summary>
- [MenuItem("File/Save Scene", ButtonModifier.Ctrl, ButtonCode.S, 10049)]
- [ToolbarItem("Save Scene", ToolbarIcon.SaveScene, "Save scene (Ctrl + S)", 1998)]
- private static void SaveSceneMenu()
- {
- SaveScene();
- }
- /// <summary>
- /// Wrapper for menu items for <see cref="SaveSceneAs(Action, Action)"/> method
- /// </summary>
- [MenuItem("File/Save Scene As", 10048)]
- private static void SaveSceneAsMenu()
- {
- SaveSceneAs();
- }
- /// <summary>
- /// Opens a Project Window allowing you to browse for or create a project.
- /// </summary>
- [MenuItem("File/Open Project", 10100)]
- [ToolbarItem("Open Project", ToolbarIcon.OpenProject, "Project manager", 2000)]
- public static void BrowseForProject()
- {
- ProjectWindow.Open();
- }
- /// <summary>
- /// Saves all data in the currently open project.
- /// </summary>
- [MenuItem("File/Save Project", 10099)]
- [ToolbarItem("Save Project", ToolbarIcon.SaveProject, "Save project", 1999)]
- public static void SaveProject()
- {
- OnProjectSave?.Invoke();
- // Apply changes to any animation clips edited using the animation editor
- foreach (var KVP in persistentData.dirtyAnimClips)
- KVP.Value.SaveToClip();
- // Save all dirty resources to disk
- foreach (var KVP in persistentData.dirtyResources)
- {
- UUID resourceUUID = KVP.Key;
- string path = ProjectLibrary.GetPath(resourceUUID);
- if (!IsNative(path))
- continue; // Imported resources can't be changed
- Resource resource = ProjectLibrary.Load<Resource>(path);
- if(resource != null)
- ProjectLibrary.Save(resource);
- }
- persistentData.dirtyAnimClips.Clear();
- persistentData.dirtyResources.Clear();
- SetStatusProject(false);
- Internal_SaveProject();
- }
- /// <summary>
- /// Loads the project at the specified path. This method executes asynchronously.
- /// </summary>
- /// <param name="path">Absolute path to the project's root folder.</param>
- public static void LoadProject(string path)
- {
- if (IsProjectLoaded && path == ProjectPath)
- return;
- if (!Internal_IsValidProject(path))
- {
- Debug.LogWarning("Provided path: \"" + path + "\" is not a valid project.");
- return;
- }
- if (IsProjectLoaded)
- TryUnloadProject(() => Internal_LoadProject(path));
- else
- Internal_LoadProject(path); // Triggers Internal_OnProjectLoaded when done
- }
- /// <summary>
- /// Closes the editor.
- /// </summary>
- public static void Quit()
- {
- Internal_Quit();
- }
- /// <summary>
- /// Toggles an existing toolbar button into an on or off state which changes the visuals of the button.
- /// </summary>
- /// <param name="name">Name of the existing button to toggle</param>
- /// <param name="on">True to toggle on, false to toggle off (default)</param>
- public static void ToggleToolbarItem(string name, bool on)
- {
- Internal_ToggleToolbarItem(name, on);
- }
- /// <summary>
- /// Opens a folder in the default external application.
- /// </summary>
- /// <param name="path">Absolute path to the folder to open.</param>
- public static void OpenFolder(string path)
- {
- Internal_OpenFolder(path);
- }
- /// <summary>
- /// Marks a resource as dirty so that it may be saved the next time the project is saved. Optionally you may also
- /// call <see cref="ProjectLibrary.Save"/> to save it immediately.
- /// </summary>
- /// <param name="resource">Resource to mark as dirty</param>
- public static void SetDirty(Resource resource)
- {
- if (resource == null)
- return;
- SetStatusProject(true);
- persistentData.dirtyResources[resource.UUID] = resource;
- }
- /// <summary>
- /// Marks the current project dirty (requires saving in order for changes not to be lost).
- /// </summary>
- public static void SetProjectDirty()
- {
- SetStatusProject(true);
- }
- /// <summary>
- /// Marks the current scene as dirty.
- /// </summary>
- public static void SetSceneDirty()
- {
- SetSceneDirty(true);
- NotifyNeedsRedraw();
- }
- /// <summary>
- /// Marks the current scene as clean or dirty.
- /// </summary>
- /// <param name="dirty">Should the scene be marked as clean or dirty.</param>
- internal static void SetSceneDirty(bool dirty)
- {
- sceneDirty = dirty;
- SetStatusScene(Scene.ActiveSceneName, dirty);
- if (!dirty && !Scene.ActiveSceneUUID.IsEmpty())
- persistentData.dirtyResources.Remove(Scene.ActiveSceneUUID);
- }
- /// <summary>
- /// Checks is the specific resource dirty and needs saving.
- /// </summary>
- /// <param name="resource">Resource to check.</param>
- /// <returns>True if the resource requires saving, false otherwise.</returns>
- public static bool IsDirty(Resource resource)
- {
- return persistentData.dirtyResources.ContainsKey(resource.UUID);
- }
- /// <summary>
- /// Notifies the system that the 3D viewports should be redrawn.
- /// </summary>
- public static void NotifyNeedsRedraw()
- {
- SceneWindow sceneWindow = EditorWindow.GetWindow<SceneWindow>();
- sceneWindow?.NotifyNeedsRedraw();
- GameWindow gameWindow = EditorWindow.GetWindow<GameWindow>();
- gameWindow?.NotifyNeedsRedraw();
- }
- /// <summary>
- /// Enables or disables on-demand drawing for 3D viewports. When enabled the viewports will only be
- /// redrawn when <see cref="NotifyNeedsRedraw"/> is called. If disabled the viewport will be redrawn
- /// every frame.
- /// </summary>
- /// <param name="enabled">True to enable on-demand drawing, false otherwise.</param>
- public static void ToggleOnDemandDrawing(bool enabled)
- {
- SceneWindow sceneWindow = EditorWindow.GetWindow<SceneWindow>();
- sceneWindow?.ToggleOnDemandDrawing(enabled);
- GameWindow gameWindow = EditorWindow.GetWindow<GameWindow>();
- gameWindow?.ToggleOnDemandDrawing(enabled);
- }
- /// <summary>
- /// Checks does the path represent a native resource.
- /// </summary>
- /// <param name="path">Filename or path to check.</param>
- /// <returns>True if the path represents a native resource.</returns>
- public static bool IsNative(string path)
- {
- string extension = Path.GetExtension(path);
- return extension == ".asset" || extension == ".prefab";
- }
- /// <summary>
- /// Attempts to unload the currently loaded project. Offers the user a chance to save the current scene if it is
- /// modified. Automatically saves all project data before unloading.
- /// </summary>
- /// <param name="onDone">Callback to trigger when project project unload is done.</param>
- private static void TryUnloadProject(Action onDone)
- {
- if (delayUnloadProject)
- return;
- AskToSaveSceneAndContinue(
- () =>
- {
- if (ProjectLibrary.ImportInProgress)
- {
- ConfirmImportInProgressWindow.Show();
- delayUnloadCallback = onDone;
- delayUnloadProject = true;
- }
- else
- {
- UnloadProject();
- onDone?.Invoke();
- }
- });
- }
- /// <summary>
- /// Unloads the currently loaded project, without making any checks or requiring confirmation.
- /// </summary>
- private static void UnloadProject()
- {
- Scene.Clear();
- if (monitor != null)
- {
- monitor.Destroy();
- monitor = null;
- }
- LibraryWindow window = EditorWindow.GetWindow<LibraryWindow>();
- if (window != null)
- window.Reset();
- SetSceneDirty(false);
- Internal_UnloadProject();
- SetStatusProject(false);
- }
- /// <summary>
- /// Checks if the current scene is modified and asks the user to save the scene if it is. Triggers the
- /// <see cref="next"/> callback when done, unless user cancels the operation.
- /// </summary>
- /// <param name="next">Callback to trigger after this method finishes.</param>
- internal static void AskToSaveSceneAndContinue(Action next)
- {
- Action trySaveScene = null;
- trySaveScene = () =>
- {
- SaveScene(next, trySaveScene);
- };
- Action<DialogBox.ResultType> dialogCallback =
- (result) =>
- {
- if (result == DialogBox.ResultType.Yes)
- trySaveScene();
- else if (result == DialogBox.ResultType.No)
- next?.Invoke();
- };
- if (IsSceneModified())
- {
- DialogBox.Open("Warning", "You current scene has modifications. Do you wish to save them first?",
- DialogBox.Type.YesNoCancel, dialogCallback);
- }
- else
- next?.Invoke();
- }
- /// <summary>
- /// Reloads all script assemblies in case they were modified. This action is delayed and will be executed
- /// at the beginning of the next frame.
- /// </summary>
- public static void ReloadAssemblies()
- {
- UpdateEditorSceneData();
- Internal_ReloadAssemblies();
- }
- /// <summary>
- /// Changes the scene displayed on the status bar.
- /// </summary>
- /// <param name="name">Name of the scene.</param>
- /// <param name="modified">Whether to display the scene as modified or not.</param>
- private static void SetStatusScene(string name, bool modified)
- {
- Internal_SetStatusScene(name, modified);
- }
- /// <summary>
- /// Changes the project state displayed on the status bar.
- /// </summary>
- /// <param name="modified">Whether to display the project as modified or not.</param>
- private static void SetStatusProject(bool modified)
- {
- Internal_SetStatusProject(modified);
- }
- /// <summary>
- /// Displays or hides the "compilation in progress" visual on the status bar.
- /// </summary>
- /// <param name="compiling">True to display the visual, false otherwise.</param>
- internal static void SetStatusCompiling(bool compiling)
- {
- Internal_SetStatusCompiling(compiling);
- }
- /// <summary>
- /// Displays or hides the "import in progress" visuals on the status bar and updates the related progress bar.
- /// </summary>
- /// <param name="importing">True to display the visual, false otherwise.</param>
- /// <param name="percent">Percent in range [0, 1] to display on the progress bar.</param>
- internal static void SetStatusImporting(bool importing, float percent)
- {
- Internal_SetStatusImporting(importing, percent);
- }
- /// <summary>
- /// Checks did we make any modifications to the scene since it was last saved.
- /// </summary>
- /// <returns>True if the scene was never saved, or was modified after last save.</returns>
- public static bool IsSceneModified()
- {
- return sceneDirty;
- }
- /// <summary>
- /// Executes any editor-specific unit tests. This should be called after a project is loaded if possible.
- /// </summary>
- private static void RunUnitTests()
- {
- #if DEBUG
- Internal_RunUnitTests();
- #endif
- }
- /// <summary>
- /// Triggered by the runtime when <see cref="LoadProject"/> method completes.
- /// </summary>
- private static void Internal_OnProjectLoaded()
- {
- SetStatusProject(false);
- if (!unitTestsExecuted)
- {
- RunUnitTests();
- unitTestsExecuted = true;
- }
- if (!IsProjectLoaded)
- {
- ProjectWindow.Open();
- return;
- }
- string projectPath = ProjectPath;
- RecentProject[] recentProjects = EditorSettings.RecentProjects;
- bool foundPath = false;
- for (int i = 0; i < recentProjects.Length; i++)
- {
- if (PathEx.Compare(recentProjects[i].path, projectPath))
- {
- recentProjects[i].accessTimestamp = (ulong)DateTime.Now.Ticks;
- EditorSettings.RecentProjects = recentProjects;
- foundPath = true;
- break;
- }
- }
- if (!foundPath)
- {
- List<RecentProject> extendedRecentProjects = new List<RecentProject>();
- extendedRecentProjects.AddRange(recentProjects);
- RecentProject newProject = new RecentProject();
- newProject.path = projectPath;
- newProject.accessTimestamp = (ulong)DateTime.Now.Ticks;
- extendedRecentProjects.Add(newProject);
- EditorSettings.RecentProjects = extendedRecentProjects.ToArray();
- }
- EditorSettings.LastOpenProject = projectPath;
- EditorSettings.Save();
- ProjectLibrary.Refresh();
- if(monitor != null)
- {
- monitor.Destroy();
- monitor = null;
- }
- monitor = new FolderMonitor(ProjectLibrary.ResourceFolder);
- monitor.OnAdded += OnAssetModified;
- monitor.OnRemoved += OnAssetModified;
- monitor.OnModified += OnAssetModified;
- if (!string.IsNullOrWhiteSpace(ProjectSettings.LastOpenScene))
- {
- lastLoadedScene = Scene.LoadAsync(ProjectSettings.LastOpenScene);
- SetSceneDirty(false);
- }
- }
- /// <summary>
- /// Triggered by the runtime when the user clicks on the status bar.
- /// </summary>
- private static void Internal_OnStatusBarClicked()
- {
- EditorWindow.OpenWindow<LogWindow>();
- }
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern void Internal_SetStatusScene(string name, bool modified);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern void Internal_SetStatusProject(bool modified);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern void Internal_SetStatusCompiling(bool compiling);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern void Internal_SetStatusImporting(bool importing, float percent);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern string Internal_GetProjectPath();
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern string Internal_GetProjectName();
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern bool Internal_GetProjectLoaded();
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern string Internal_GetCompilerPath();
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern string Internal_GetMonoExecPath();
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern string Internal_GetBuiltinReleaseAssemblyPath();
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern string Internal_GetBuiltinDebugAssemblyPath();
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern string Internal_GetScriptAssemblyPath();
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern string Internal_GetFrameworkAssemblyPath();
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern string Internal_GetEngineAssemblyName();
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern string Internal_GetEditorAssemblyName();
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern string Internal_GetScriptGameAssemblyName();
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern string Internal_GetScriptEditorAssemblyName();
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern Prefab Internal_SaveScene(string path);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern bool Internal_IsValidProject(string path);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern void Internal_SaveProject();
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern void Internal_LoadProject(string path);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern void Internal_UnloadProject();
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern void Internal_CreateProject(string path);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern void Internal_ReloadAssemblies();
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern void Internal_OpenFolder(string path);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern void Internal_RunUnitTests();
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern void Internal_Quit();
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern void Internal_ToggleToolbarItem(string name, bool on);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern void Internal_SetMainRenderTarget(IntPtr rendertarget);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern bool Internal_HasFocus();
- }
- /** @} */
- }
|