Add JsonAssetReference type for scripting
This commit is contained in:
@@ -51,10 +51,13 @@ namespace FlaxEditor.CustomEditors.Editors
|
||||
return;
|
||||
Picker = layout.Custom<AssetPicker>().CustomControl;
|
||||
|
||||
_valueType = Values.Type.Type != typeof(object) || Values[0] == null ? Values.Type : TypeUtils.GetObjectType(Values[0]);
|
||||
var value = Values[0];
|
||||
_valueType = Values.Type.Type != typeof(object) || value == null ? Values.Type : TypeUtils.GetObjectType(value);
|
||||
var assetType = _valueType;
|
||||
if (assetType == typeof(string))
|
||||
assetType = new ScriptType(typeof(Asset));
|
||||
else if (_valueType.Type != null && _valueType.Type.Name == typeof(JsonAssetReference<>).Name)
|
||||
assetType = new ScriptType(_valueType.Type.GenericTypeArguments[0]);
|
||||
|
||||
float height = 48;
|
||||
var attributes = Values.GetAttributes();
|
||||
@@ -102,6 +105,12 @@ namespace FlaxEditor.CustomEditors.Editors
|
||||
SetValue(new SceneReference(Picker.Validator.SelectedID));
|
||||
else if (_valueType.Type == typeof(string))
|
||||
SetValue(Picker.Validator.SelectedPath);
|
||||
else if (_valueType.Type.Name == typeof(JsonAssetReference<>).Name)
|
||||
{
|
||||
var value = Values[0];
|
||||
value.GetType().GetField("Asset").SetValue(value, Picker.Validator.SelectedAsset as JsonAsset);
|
||||
SetValue(value);
|
||||
}
|
||||
else
|
||||
SetValue(Picker.Validator.SelectedAsset);
|
||||
}
|
||||
@@ -114,16 +123,19 @@ namespace FlaxEditor.CustomEditors.Editors
|
||||
if (!HasDifferentValues)
|
||||
{
|
||||
_isRefreshing = true;
|
||||
if (Values[0] is AssetItem assetItem)
|
||||
var value = Values[0];
|
||||
if (value is AssetItem assetItem)
|
||||
Picker.Validator.SelectedItem = assetItem;
|
||||
else if (Values[0] is Guid guid)
|
||||
else if (value is Guid guid)
|
||||
Picker.Validator.SelectedID = guid;
|
||||
else if (Values[0] is SceneReference sceneAsset)
|
||||
else if (value is SceneReference sceneAsset)
|
||||
Picker.Validator.SelectedItem = Editor.Instance.ContentDatabase.FindAsset(sceneAsset.ID);
|
||||
else if (Values[0] is string path)
|
||||
else if (value is string path)
|
||||
Picker.Validator.SelectedPath = path;
|
||||
else if (value != null && value.GetType().Name == typeof(JsonAssetReference<>).Name)
|
||||
Picker.Validator.SelectedAsset = value.GetType().GetField("Asset").GetValue(value) as JsonAsset;
|
||||
else
|
||||
Picker.Validator.SelectedAsset = Values[0] as Asset;
|
||||
Picker.Validator.SelectedAsset = value as Asset;
|
||||
_isRefreshing = false;
|
||||
}
|
||||
}
|
||||
|
||||
83
Source/Engine/Content/JsonAssetReference.cs
Normal file
83
Source/Engine/Content/JsonAssetReference.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
|
||||
|
||||
using System;
|
||||
|
||||
namespace FlaxEngine
|
||||
{
|
||||
/// <summary>
|
||||
/// Json asset reference utility. References resource with a typed data type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type of the asset instance type.</typeparam>
|
||||
#if FLAX_EDITOR
|
||||
[CustomEditor(typeof(FlaxEditor.CustomEditors.Editors.AssetRefEditor))]
|
||||
#endif
|
||||
public struct JsonAssetReference<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the referenced asset.
|
||||
/// </summary>
|
||||
public JsonAsset Asset;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the instance of the Json Asset. Null if unset.
|
||||
/// </summary>
|
||||
/// <returns>instance of the Json Asset or null if unset.</returns>
|
||||
public T Get()
|
||||
{
|
||||
return (T)Asset?.Instance;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="JsonAssetReference{T}"/> structure.
|
||||
/// </summary>
|
||||
/// <param name="asset">The Json Asset.</param>
|
||||
public JsonAssetReference(JsonAsset asset)
|
||||
{
|
||||
Asset = asset;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implicit cast operator.
|
||||
/// </summary>
|
||||
public static implicit operator JsonAsset(JsonAssetReference<T> value)
|
||||
{
|
||||
return value.Asset;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implicit cast operator.
|
||||
/// </summary>
|
||||
public static implicit operator IntPtr(JsonAssetReference<T> value)
|
||||
{
|
||||
return Object.GetUnmanagedPtr(value.Asset);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implicit cast operator.
|
||||
/// </summary>
|
||||
public static implicit operator JsonAssetReference<T>(JsonAsset value)
|
||||
{
|
||||
return new JsonAssetReference<T>(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implicit cast operator.
|
||||
/// </summary>
|
||||
public static implicit operator JsonAssetReference<T>(IntPtr valuePtr)
|
||||
{
|
||||
return new JsonAssetReference<T>(Object.FromUnmanagedPtr(valuePtr) as JsonAsset);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString()
|
||||
{
|
||||
return Asset?.ToString();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return Asset?.GetHashCode() ?? 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
25
Source/Engine/Content/JsonAssetReference.h
Normal file
25
Source/Engine/Content/JsonAssetReference.h
Normal file
@@ -0,0 +1,25 @@
|
||||
// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Engine/Content/JsonAsset.h"
|
||||
#include "Engine/Content/AssetReference.h"
|
||||
|
||||
/// <summary>
|
||||
/// Json asset reference utility. References resource with a typed data type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type of the asset instance type.</typeparam>
|
||||
template<typename T>
|
||||
API_STRUCT(NoDefault, Template, MarshalAs=JsonAsset*) struct JsonAssetReference : AssetReference<JsonAsset>
|
||||
{
|
||||
JsonAssetReference& operator=(JsonAsset* asset) noexcept
|
||||
{
|
||||
OnSet(asset);
|
||||
return *this;
|
||||
}
|
||||
|
||||
operator JsonAsset*() const
|
||||
{
|
||||
return Get();
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user