Add JsonAsset.Instance for C# asset object
This commit is contained in:
@@ -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<JsonAsset, &JsonAsset::OnScriptsReloadStart>(this);
|
||||
Level::ScriptsReloaded.Bind<JsonAsset, &JsonAsset::OnScriptsReloaded>(this);
|
||||
}
|
||||
// Reload instance when module with this type gets reloaded
|
||||
Level::ScriptsReloadStart.Bind<JsonAsset, &JsonAsset::OnScriptsReloadStart>(this);
|
||||
Level::ScriptsReloaded.Bind<JsonAsset, &JsonAsset::OnScriptsReloaded>(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<JsonAsset, &JsonAsset::OnScriptsReloadStart>(this);
|
||||
Level::ScriptsReloaded.Unbind<JsonAsset, &JsonAsset::OnScriptsReloaded>(this);
|
||||
Level::ScriptsReloadStart.Unbind<JsonAsset, &JsonAsset::OnScriptsReloadStart>(this);
|
||||
Level::ScriptsReloaded.Unbind<JsonAsset, &JsonAsset::OnScriptsReloaded>(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);
|
||||
|
||||
@@ -7,19 +7,28 @@ namespace FlaxEngine
|
||||
{
|
||||
partial class JsonAsset
|
||||
{
|
||||
private object _instance;
|
||||
|
||||
/// <summary>
|
||||
/// Creates the serialized object instance from the json asset data.
|
||||
/// Gets the instance of the serialized object from the json asset data. Cached internally.
|
||||
/// </summary>
|
||||
/// <returns>The created object or null.</returns>
|
||||
public object Instance => _instance ?? (_instance = CreateInstance());
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of the serialized object from the json asset data.
|
||||
/// </summary>
|
||||
/// <remarks>Use <see cref="Instance"/> to get cached object.</remarks>
|
||||
/// <returns>The new object or null if failed.</returns>
|
||||
public T CreateInstance<T>()
|
||||
{
|
||||
return (T)CreateInstance();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates the serialized object instance from the json asset data.
|
||||
/// Creates a new instance of the serialized object from the json asset data.
|
||||
/// </summary>
|
||||
/// <returns>The created object or null.</returns>
|
||||
/// <remarks>Use <see cref="Instance"/> to get cached object.</remarks>
|
||||
/// <returns>The new object or null if failed.</returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user