From a27d69f8529e726a46dc3ffd90912cc879f9f729 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Wed, 13 Apr 2022 21:19:10 +0200 Subject: [PATCH] Improve Json Asset development workflow --- .../CustomEditors/CustomEditorPresenter.cs | 26 +++++++++++--- .../Editor/Windows/Assets/JsonAssetWindow.cs | 36 +++++++++++++++++-- Source/Engine/Core/Config/GameSettings.cs | 8 ++--- 3 files changed, 60 insertions(+), 10 deletions(-) diff --git a/Source/Editor/CustomEditors/CustomEditorPresenter.cs b/Source/Editor/CustomEditors/CustomEditorPresenter.cs index 62ce2df77..48371a936 100644 --- a/Source/Editor/CustomEditors/CustomEditorPresenter.cs +++ b/Source/Editor/CustomEditors/CustomEditorPresenter.cs @@ -29,7 +29,7 @@ namespace FlaxEditor.CustomEditors /// Enables using prefab-related features of the properties editor (eg. revert to prefab option). /// UsePrefab = 1 << 1, - + /// /// Enables using default-value-related features of the properties editor (eg. revert to default option). /// @@ -88,7 +88,6 @@ namespace FlaxEditor.CustomEditors /// protected class RootEditor : SyncPointEditor { - private readonly string _noSelectionText; private CustomEditor _overrideEditor; /// @@ -109,13 +108,18 @@ namespace FlaxEditor.CustomEditors } } + /// + /// The text to show when no object is selected. + /// + public string NoSelectionText; + /// /// Initializes a new instance of the class. /// /// The text to show when no item is selected. public RootEditor(string noSelectionText) { - _noSelectionText = noSelectionText ?? "No selection"; + NoSelectionText = noSelectionText ?? "No selection"; } /// @@ -154,7 +158,7 @@ namespace FlaxEditor.CustomEditors } else { - var label = layout.Label(_noSelectionText, TextAlignment.Center); + var label = layout.Label(NoSelectionText, TextAlignment.Center); label.Label.Height = 20.0f; } @@ -251,6 +255,20 @@ namespace FlaxEditor.CustomEditors /// public object Owner; + /// + /// Gets or sets the text to show when no object is selected. + /// + public string NoSelectionText + { + get => Editor.NoSelectionText; + set + { + Editor.NoSelectionText = value; + if (SelectionCount == 0) + BuildLayoutOnUpdate(); + } + } + private bool _buildOnUpdate; /// diff --git a/Source/Editor/Windows/Assets/JsonAssetWindow.cs b/Source/Editor/Windows/Assets/JsonAssetWindow.cs index 5abddea32..1cb1cbdd0 100644 --- a/Source/Editor/Windows/Assets/JsonAssetWindow.cs +++ b/Source/Editor/Windows/Assets/JsonAssetWindow.cs @@ -1,5 +1,6 @@ // Copyright (c) 2012-2022 Wojciech Figat. All rights reserved. +using System; using FlaxEditor.Content; using FlaxEditor.CustomEditors; using FlaxEditor.GUI; @@ -106,13 +107,36 @@ namespace FlaxEditor.Windows.Assets /// protected override void OnAssetLoaded() { - _object = Asset.CreateInstance(); + _object = Asset.Instance; + if (_object == null) + { + // Hint developer about cause of failure + var dataTypeName = Asset.DataTypeName; + var type = Type.GetType(dataTypeName); + if (type != null) + { + try + { + var obj = Activator.CreateInstance(type); + var data = Asset.Data; + FlaxEngine.Json.JsonSerializer.Deserialize(obj, data); + } + catch (Exception ex) + { + _presenter.NoSelectionText = "Failed to load asset. See log for more. " + ex.Message.Replace('\n', ' '); + } + } + else + { + _presenter.NoSelectionText = string.Format("Missing type '{0}'.", dataTypeName); + } + } _presenter.Select(_object); _undo.Clear(); ClearEditedFlag(); // Auto-close on scripting reload if json asset is from game scripts (it might be reloaded) - if (_object != null && FlaxEngine.Scripting.IsTypeFromGameScripts(_object.GetType()) && !_isRegisteredForScriptsReload) + if ((_object == null || FlaxEngine.Scripting.IsTypeFromGameScripts(_object.GetType())) && !_isRegisteredForScriptsReload) { _isRegisteredForScriptsReload = true; ScriptsBuilder.ScriptsReloadBegin += OnScriptsReloadBegin; @@ -121,6 +145,14 @@ namespace FlaxEditor.Windows.Assets base.OnAssetLoaded(); } + /// + protected override void OnAssetLoadFailed() + { + _presenter.NoSelectionText = "Failed to load the asset."; + + base.OnAssetLoadFailed(); + } + /// public override void OnItemReimported(ContentItem item) { diff --git a/Source/Engine/Core/Config/GameSettings.cs b/Source/Engine/Core/Config/GameSettings.cs index e721da61d..a8519fe4c 100644 --- a/Source/Engine/Core/Config/GameSettings.cs +++ b/Source/Engine/Core/Config/GameSettings.cs @@ -202,7 +202,7 @@ namespace FlaxEditor.Content.Settings var asset = FlaxEngine.Content.LoadAsync(GameSettingsAssetPath); if (asset && !asset.WaitForLoaded()) { - if (asset.CreateInstance() is GameSettings result) + if (asset.Instance is GameSettings result) return result; } return new GameSettings(); @@ -212,7 +212,7 @@ namespace FlaxEditor.Content.Settings { if (asset && !asset.WaitForLoaded()) { - if (asset.CreateInstance() is T result) + if (asset.Instance is T result) return result; } return new T(); @@ -222,7 +222,7 @@ namespace FlaxEditor.Content.Settings { if (asset && !asset.WaitForLoaded() && asset.DataTypeName == typename) { - if (asset.CreateInstance() is SettingsBase result) + if (asset.Instance is SettingsBase result) return result; } return null; @@ -314,7 +314,7 @@ namespace FlaxEditor.Content.Settings { if (e.Value && !e.Value.WaitForLoaded() && e.Value.DataTypeName == type.FullName) { - var custom = e.Value.CreateInstance(); + var custom = e.Value.Instance; if (custom is T result) return result; }