Files
FlaxEngine/Source/Engine/Content/JsonAsset.cs
Wojciech Figat a7e428a21c Merge branch 'master' into 1.5
# Conflicts:
#	Content/Shaders/GI/DDGI.flax
#	Content/Shaders/GI/GlobalSurfaceAtlas.flax
#	Content/Shaders/TAA.flax
#	Content/Shaders/VolumetricFog.flax
#	Source/Editor/CustomEditors/Editors/ActorTagEditor.cs
#	Source/Engine/Core/Config/GraphicsSettings.cpp
#	Source/Engine/Engine/PostProcessEffect.cs
#	Source/Engine/Graphics/GPUResourcesCollection.cpp
#	Source/Engine/Graphics/GPUResourcesCollection.h
#	Source/Engine/Graphics/PostProcessBase.h
#	Source/FlaxEngine.Gen.cs
2023-01-10 15:37:55 +01:00

82 lines
2.8 KiB
C#

// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
using System;
using FlaxEngine.Json;
namespace FlaxEngine
{
partial class JsonAsset
{
private object _instance;
/// <summary>
/// Gets the instance of the serialized object from the json asset data. Cached internally.
/// </summary>
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 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 object CreateInstance()
{
if (WaitForLoaded())
return null;
var dataTypeName = DataTypeName;
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
for (int i = 0; i < assemblies.Length; i++)
{
var assembly = assemblies[i];
if (assembly != null)
{
var type = assembly.GetType(dataTypeName);
if (type != null)
{
object obj = null;
try
{
// Create instance
obj = Activator.CreateInstance(type);
// Deserialize object
var data = Data;
JsonSerializer.Deserialize(obj, data);
}
catch (Exception ex)
{
Debug.LogException(ex, this);
}
return obj;
}
}
}
Debug.LogError(string.Format("Missing type '{0}' to create Json Asset instance.", dataTypeName), this);
return null;
}
/// <summary>
/// Sets the instance of the asset (for both C# and C++). Doesn't save asset to the file (runtime only).
/// </summary>
/// <param name="instance">The new instance.</param>
public void SetInstance(object instance)
{
_instance = instance;
string str = instance != null ? JsonSerializer.Serialize(instance) : null;
Data = str;
}
}
}