Files
FlaxEngine/Source/Engine/Content/JsonAsset.cs
Wojciech Figat eebc4951de Merge branch '1.5' into dotnet7
# Conflicts:
#	Source/Platforms/DotNet/NUnit/agents/net40/nunit-agent.exe
#	Source/Platforms/DotNet/NUnit/agents/net40/nunit.engine.api.dll
#	Source/Platforms/DotNet/NUnit/agents/net40/nunit.engine.core.dll
#	Source/Platforms/DotNet/NUnit/agents/net7.0/nunit.agent.addins
#	Source/Platforms/DotNet/NUnit/nunit.engine.api.dll
#	Source/Platforms/DotNet/NUnit/nunit.engine.core.dll
#	Source/Platforms/DotNet/NUnit/nunit.engine.dll
#	Source/Platforms/DotNet/NUnit/nunit3-console.exe
#	Source/Platforms/DotNet/NUnit/nunit3-console.exe.config
#	Source/Platforms/DotNet/NUnit/testcentric.engine.metadata.dll
#	Source/Tools/Flax.Build/Deps/Downloader.cs
#	Source/Tools/Flax.Stats/CodeFrame.cs
#	Source/Tools/Flax.Stats/CodeFrameNode.cs
#	Source/Tools/Flax.Stats/Flax.Stats.Build.cs
#	Source/Tools/Flax.Stats/Languages.cs
#	Source/Tools/Flax.Stats/Program.cs
#	Source/Tools/Flax.Stats/TaskType.cs
#	Source/Tools/Flax.Stats/Tools.cs
#	Source/Tools/FlaxEngine.Tests/TestEditorUtils.cs
2023-01-10 15:49:44 +01:00

85 lines
3.0 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();
// Going through the assemblies in order will return collected assemblies first,
// use reverse order instead to find the type currently loaded assemblies instead.
for (int i = assemblies.Length-1; i >= 0; 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;
}
}
}