Improve Json Asset development workflow

This commit is contained in:
Wojtek Figat
2022-04-13 21:19:10 +02:00
parent 9fb4624b03
commit a27d69f852
3 changed files with 60 additions and 10 deletions

View File

@@ -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
/// <inheritdoc />
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();
}
/// <inheritdoc />
protected override void OnAssetLoadFailed()
{
_presenter.NoSelectionText = "Failed to load the asset.";
base.OnAssetLoadFailed();
}
/// <inheritdoc />
public override void OnItemReimported(ContentItem item)
{