This commit is contained in:
Wojtek Figat
2023-05-05 15:58:18 +02:00
parent 50c6b23e35
commit 404340b02e

View File

@@ -307,6 +307,7 @@ namespace FlaxEditor
_areModulesInited = true;
// Preload initial scene asset
try
{
var startupSceneMode = Options.Options.General.StartupSceneMode;
if (startupSceneMode == GeneralOptions.StartupSceneModes.LastOpened && !ProjectCache.HasCustomData(ProjectDataLastScene))
@@ -325,8 +326,8 @@ namespace FlaxEditor
{
if (ProjectCache.TryGetCustomData(ProjectDataLastScene, out var lastSceneIdName))
{
var lastSceneList = JsonSerializer.Deserialize<Guid[]>(lastSceneIdName);
foreach (var scene in lastSceneList)
var lastScenes = JsonSerializer.Deserialize<Guid[]>(lastSceneIdName);
foreach (var scene in lastScenes)
{
var lastScene = scene;
Internal_LoadAsset(ref lastScene);
@@ -336,6 +337,10 @@ namespace FlaxEditor
}
}
}
catch (Exception ex)
{
// Ignore errors
}
InitializationStart?.Invoke();
@@ -408,64 +413,69 @@ namespace FlaxEditor
}
// Load scene
// scene cmd line argument
var scene = ContentDatabase.Find(_startupSceneCmdLine);
if (scene is SceneItem)
try
{
Editor.Log("Loading scene specified in command line");
Scene.OpenScene(_startupSceneCmdLine);
return;
}
// if no scene cmd line argument is provided
var startupSceneMode = Options.Options.General.StartupSceneMode;
if (startupSceneMode == GeneralOptions.StartupSceneModes.LastOpened && !ProjectCache.HasCustomData(ProjectDataLastScene))
{
// Fallback to default project scene if nothing saved in the cache
startupSceneMode = GeneralOptions.StartupSceneModes.ProjectDefault;
}
switch (startupSceneMode)
{
case GeneralOptions.StartupSceneModes.ProjectDefault:
{
if (string.IsNullOrEmpty(GameProject.DefaultScene))
break;
JsonSerializer.ParseID(GameProject.DefaultScene, out var defaultSceneId);
var defaultScene = ContentDatabase.Find(defaultSceneId);
if (defaultScene is SceneItem)
// Scene cmd line argument
var scene = ContentDatabase.Find(_startupSceneCmdLine);
if (scene is SceneItem)
{
Editor.Log("Loading default project scene");
Scene.OpenScene(defaultSceneId);
// Use spawn point
Windows.EditWin.Viewport.ViewRay = GameProject.DefaultSceneSpawn;
Editor.Log("Loading scene specified in command line");
Scene.OpenScene(_startupSceneCmdLine);
return;
}
break;
}
case GeneralOptions.StartupSceneModes.LastOpened:
{
if (ProjectCache.TryGetCustomData(ProjectDataLastScene, out var lastSceneIdName))
var startupSceneMode = Options.Options.General.StartupSceneMode;
if (startupSceneMode == GeneralOptions.StartupSceneModes.LastOpened && !ProjectCache.HasCustomData(ProjectDataLastScene))
{
var lastSceneList = JsonSerializer.Deserialize<Guid[]>(lastSceneIdName);
foreach (var sceneId in lastSceneList)
// Fallback to default project scene if nothing saved in the cache
startupSceneMode = GeneralOptions.StartupSceneModes.ProjectDefault;
}
switch (startupSceneMode)
{
case GeneralOptions.StartupSceneModes.ProjectDefault:
{
if (string.IsNullOrEmpty(GameProject.DefaultScene))
break;
JsonSerializer.ParseID(GameProject.DefaultScene, out var defaultSceneId);
var defaultScene = ContentDatabase.Find(defaultSceneId);
if (defaultScene is SceneItem)
{
var lastScene = ContentDatabase.Find(sceneId);
if (!(lastScene is SceneItem))
continue;
Editor.Log($"Loading last opened scene: {lastScene.ShortName}");
if (sceneId == lastSceneList[0])
Scene.OpenScene(sceneId);
else
Level.LoadSceneAsync(sceneId);
Editor.Log("Loading default project scene");
Scene.OpenScene(defaultSceneId);
// Use spawn point
Windows.EditWin.Viewport.ViewRay = GameProject.DefaultSceneSpawn;
}
// Restore view
if (ProjectCache.TryGetCustomData(ProjectDataLastSceneSpawn, out var lastSceneSpawnName))
Windows.EditWin.Viewport.ViewRay = JsonSerializer.Deserialize<Ray>(lastSceneSpawnName);
break;
}
case GeneralOptions.StartupSceneModes.LastOpened:
{
if (ProjectCache.TryGetCustomData(ProjectDataLastScene, out var lastSceneIdName))
{
var lastScenes = JsonSerializer.Deserialize<Guid[]>(lastSceneIdName);
foreach (var sceneId in lastScenes)
{
var lastScene = ContentDatabase.Find(sceneId);
if (!(lastScene is SceneItem))
continue;
Editor.Log($"Loading last opened scene: {lastScene.ShortName}");
if (sceneId == lastScenes[0])
Scene.OpenScene(sceneId);
else
Level.LoadSceneAsync(sceneId);
}
// Restore view
if (ProjectCache.TryGetCustomData(ProjectDataLastSceneSpawn, out var lastSceneSpawnName))
Windows.EditWin.Viewport.ViewRay = JsonSerializer.Deserialize<Ray>(lastSceneSpawnName);
}
break;
}
}
break;
}
catch (Exception ex)
{
// Ignore errors
}
}
@@ -594,8 +604,8 @@ namespace FlaxEditor
UI.UpdateStatusBar();
}
if (UI?.StatusBar?.Text != null && !UI.StatusBar.Text.Contains("Auto") &&
_saveNowButton != null && _cancelSaveButton != null &&
if (UI?.StatusBar?.Text != null && !UI.StatusBar.Text.Contains("Auto") &&
_saveNowButton != null && _cancelSaveButton != null &&
(_saveNowButton.Visible || _cancelSaveButton.Visible))
{
_saveNowButton.Visible = false;
@@ -677,20 +687,12 @@ namespace FlaxEditor
// Cache last opened scenes
{
List<Guid> lastSceneIds = new List<Guid>();
if (Level.ScenesCount > 0)
{
foreach (var scene in Level.Scenes)
{
lastSceneIds.Add(scene.ID);
}
}
else
{
lastSceneIds.Add(Guid.Empty);
}
var lastScenes = Level.Scenes;
var lastSceneIds = new Guid[lastScenes.Length];
for (int i = 0; i < lastScenes.Length; i++)
lastSceneIds[i] = lastScenes[i].ID;
var lastSceneSpawn = Windows.EditWin.Viewport.ViewRay;
ProjectCache.SetCustomData(ProjectDataLastScene, JsonSerializer.Serialize(lastSceneIds.ToArray()));
ProjectCache.SetCustomData(ProjectDataLastScene, JsonSerializer.Serialize(lastSceneIds));
ProjectCache.SetCustomData(ProjectDataLastSceneSpawn, JsonSerializer.Serialize(lastSceneSpawn));
}