diff --git a/Source/Engine/Content/JsonAsset.cpp b/Source/Engine/Content/JsonAsset.cpp index 2332181b9..0fc5df599 100644 --- a/Source/Engine/Content/JsonAsset.cpp +++ b/Source/Engine/Content/JsonAsset.cpp @@ -19,6 +19,8 @@ #include "Engine/Debug/Exceptions/JsonParseException.h" #include "Engine/Profiler/ProfilerCPU.h" #include "Engine/Scripting/Scripting.h" +#include "Engine/Scripting/ManagedCLR/MClass.h" +#include "Engine/Scripting/ManagedCLR/MField.h" #include "Engine/Utilities/StringConverter.h" JsonAssetBase::JsonAssetBase(const SpawnParams& params, const AssetInfo* info) @@ -212,13 +214,11 @@ Asset::LoadResult JsonAsset::loadAsset() if (CreateInstance()) return LoadResult::Failed; + #if USE_EDITOR - if (Instance) - { - // Reload instance when module with this type gets reloaded - Level::ScriptsReloadStart.Bind(this); - Level::ScriptsReloaded.Bind(this); - } + // Reload instance when module with this type gets reloaded + Level::ScriptsReloadStart.Bind(this); + Level::ScriptsReloaded.Bind(this); #endif return LoadResult::Ok; @@ -226,14 +226,11 @@ Asset::LoadResult JsonAsset::loadAsset() void JsonAsset::unload(bool isReloading) { - if (Instance) - { #if USE_EDITOR - Level::ScriptsReloadStart.Unbind(this); - Level::ScriptsReloaded.Unbind(this); + Level::ScriptsReloadStart.Unbind(this); + Level::ScriptsReloaded.Unbind(this); #endif - DeleteInstance(); - } + DeleteInstance(); JsonAssetBase::unload(isReloading); } @@ -281,6 +278,13 @@ bool JsonAsset::CreateInstance() void JsonAsset::DeleteInstance() { + // C# instance + if (MObject* object = GetManagedInstance()) + { + GetClass()->GetField("_instance")->SetValue(object, nullptr); + } + + // C++ instance if (!Instance || !_dtor) return; _dtor(Instance); diff --git a/Source/Engine/Content/JsonAsset.cs b/Source/Engine/Content/JsonAsset.cs index 9b9441435..13c53f532 100644 --- a/Source/Engine/Content/JsonAsset.cs +++ b/Source/Engine/Content/JsonAsset.cs @@ -7,19 +7,28 @@ namespace FlaxEngine { partial class JsonAsset { + private object _instance; + /// - /// Creates the serialized object instance from the json asset data. + /// Gets the instance of the serialized object from the json asset data. Cached internally. /// - /// The created object or null. + public object Instance => _instance ?? (_instance = CreateInstance()); + + /// + /// Creates a new instance of the serialized object from the json asset data. + /// + /// Use to get cached object. + /// The new object or null if failed. public T CreateInstance() { return (T)CreateInstance(); } /// - /// Creates the serialized object instance from the json asset data. + /// Creates a new instance of the serialized object from the json asset data. /// - /// The created object or null. + /// Use to get cached object. + /// The new object or null if failed. public object CreateInstance() { if (WaitForLoaded()) @@ -41,9 +50,14 @@ namespace FlaxEngine } catch (Exception ex) { - Debug.LogException(ex); + Debug.LogException(ex, this); } } + else + { + Debug.LogError(string.Format("Missing type '{0}' to create Json Asset instance.", dataTypeName), this); + } + return obj; } }