Add utility GetInstance method to Json Asset refs in csharp

#2774
This commit is contained in:
Wojtek Figat
2024-07-18 15:14:26 +02:00
parent f132198ead
commit d879b8e064
2 changed files with 20 additions and 0 deletions

View File

@@ -14,6 +14,17 @@ namespace FlaxEngine
/// </summary> /// </summary>
public object Instance => _instance ?? (_instance = CreateInstance()); public object Instance => _instance ?? (_instance = CreateInstance());
/// <summary>
/// Gets the instance of the serialized object from the json data. Cached internally.
/// </summary>
/// <returns>The asset instance object or null.</returns>
public T GetInstance<T>()
{
if (Instance is T instance)
return instance;
return default;
}
/// <summary> /// <summary>
/// Creates a new instance of the serialized object from the json asset data. /// Creates a new instance of the serialized object from the json asset data.
/// </summary> /// </summary>

View File

@@ -33,6 +33,15 @@ namespace FlaxEngine
Asset = asset; Asset = asset;
} }
/// <summary>
/// Gets the deserialized native object instance of the given type. Returns null if asset is not loaded or loaded object has different type.
/// </summary>
/// <returns>The asset instance object or null.</returns>
public U GetInstance<U>()
{
return Asset ? Asset.GetInstance<U>() : default(U);
}
/// <summary> /// <summary>
/// Implicit cast operator. /// Implicit cast operator.
/// </summary> /// </summary>