diff --git a/Source/Editor/CustomEditors/CustomEditorsUtil.cs b/Source/Editor/CustomEditors/CustomEditorsUtil.cs
index 476219960..2323333e2 100644
--- a/Source/Editor/CustomEditors/CustomEditorsUtil.cs
+++ b/Source/Editor/CustomEditors/CustomEditorsUtil.cs
@@ -58,11 +58,13 @@ namespace FlaxEditor.CustomEditors
if (targetType.Type == typeof(object) && values.Count > 0 && values[0] != null && !values.HasDifferentTypes)
return CreateEditor(TypeUtils.GetObjectType(values[0]), canUseRefPicker);
- // Special case if property is interface but the value is implemented as Scripting Object that should use reference picker
- if (targetType.IsInterface && canUseRefPicker && values.Count > 0 && values[0] is FlaxEngine.Object)
- return new DummyEditor();
-
// Use editor for the property type
+ if (canUseRefPicker &&
+ targetType.IsInterface &&
+ values.GetAttributes().Any(x => x is ScriptingObjectInterfaceReferenceAttribute || x is SoftObjectInterfaceReferenceAttribute))
+ {
+ return new FlaxObjectRefEditor();
+ }
return CreateEditor(targetType, canUseRefPicker);
}
diff --git a/Source/Editor/CustomEditors/Editors/DictionaryEditor.cs b/Source/Editor/CustomEditors/Editors/DictionaryEditor.cs
index 1fe849f09..7dfce6055 100644
--- a/Source/Editor/CustomEditors/Editors/DictionaryEditor.cs
+++ b/Source/Editor/CustomEditors/Editors/DictionaryEditor.cs
@@ -260,7 +260,7 @@ namespace FlaxEditor.CustomEditors.Editors
var overrideEditor = overrideEditorType != null ? (CustomEditor)Activator.CreateInstance(overrideEditorType) : null;
var property = panel.AddPropertyItem(new DictionaryItemLabel(this, key));
var itemLayout = useSharedLayout ? (LayoutElementsContainer)property : property.VerticalPanel();
- itemLayout.Object(new DictionaryValueContainer(valuesType, key, Values), overrideEditor);
+ itemLayout.Object(new DictionaryValueContainer(valuesType, key, Values, attributes), overrideEditor);
if (_readOnly && itemLayout.Children.Count > 0)
GenericEditor.OnReadOnlyProperty(itemLayout);
}
diff --git a/Source/Editor/CustomEditors/Editors/FlaxObjectRefEditor.cs b/Source/Editor/CustomEditors/Editors/FlaxObjectRefEditor.cs
index a6a02ae1f..7ce707a77 100644
--- a/Source/Editor/CustomEditors/Editors/FlaxObjectRefEditor.cs
+++ b/Source/Editor/CustomEditors/Editors/FlaxObjectRefEditor.cs
@@ -48,7 +48,7 @@ namespace FlaxEditor.CustomEditors.Editors
public IPresenterOwner PresenterContext;
///
- /// Gets or sets the allowed objects type (given type and all subclasses). Must be type of any subclass.
+ /// Gets or sets the allowed objects type (given type and all subclasses). Must be type of any subclass or a scripting interface.
///
public ScriptType Type
{
@@ -57,11 +57,12 @@ namespace FlaxEditor.CustomEditors.Editors
{
if (_type == value)
return;
- if (value == ScriptType.Null || (value.Type != typeof(Object) && !value.IsSubclassOf(ScriptType.Object)))
+ if (value == ScriptType.Null || (!value.IsInterface && value.Type != typeof(Object) && !value.IsSubclassOf(ScriptType.Object)))
throw new ArgumentException(string.Format("Invalid type for FlaxObjectRefEditor. Input type: {0}", value != ScriptType.Null ? value.TypeName : "null"));
_type = value;
- _supportsPickDropDown = new ScriptType(typeof(Actor)).IsAssignableFrom(value) ||
+ _supportsPickDropDown = value.IsInterface ||
+ new ScriptType(typeof(Actor)).IsAssignableFrom(value) ||
new ScriptType(typeof(Script)).IsAssignableFrom(value);
// Deselect value if it's not valid now
@@ -149,13 +150,22 @@ namespace FlaxEditor.CustomEditors.Editors
protected virtual bool IsValid(Object obj)
{
var type = TypeUtils.GetObjectType(obj);
- return obj == null || _type.IsAssignableFrom(type) && (CheckValid == null || CheckValid(obj, type));
+ return obj == null || (!_type.IsInterface || obj is SceneObject) && _type.IsAssignableFrom(type) && (CheckValid == null || CheckValid(obj, type));
}
private void ShowDropDownMenu()
{
Focus();
- if (new ScriptType(typeof(Actor)).IsAssignableFrom(_type))
+ if (_type.IsInterface)
+ {
+ SceneObjectSearchPopup.Show(this, new Float2(0, Height), IsValid, obj =>
+ {
+ Value = obj;
+ RootWindow.Focus();
+ Focus();
+ }, PresenterContext);
+ }
+ else if (new ScriptType(typeof(Actor)).IsAssignableFrom(_type))
{
ActorSearchPopup.Show(this, new Float2(0, Height), IsValid, actor =>
{
diff --git a/Source/Editor/CustomEditors/Values/DictionaryValueContainer.cs b/Source/Editor/CustomEditors/Values/DictionaryValueContainer.cs
index b143b44b1..435e5a515 100644
--- a/Source/Editor/CustomEditors/Values/DictionaryValueContainer.cs
+++ b/Source/Editor/CustomEditors/Values/DictionaryValueContainer.cs
@@ -14,6 +14,8 @@ namespace FlaxEditor.CustomEditors
[HideInEditor]
public class DictionaryValueContainer : ValueContainer
{
+ private readonly object[] _attributes;
+
///
/// The key in the collection.
///
@@ -36,9 +38,12 @@ namespace FlaxEditor.CustomEditors
/// Type of the collection elements.
/// The key.
/// The collection values.
- public DictionaryValueContainer(ScriptType elementType, object key, ValueContainer values)
+ /// The dictionary property attributes to inherit.
+ public DictionaryValueContainer(ScriptType elementType, object key, ValueContainer values, object[] attributes = null)
: this(elementType, key)
{
+ _attributes = attributes;
+
Capacity = values.Count;
for (int i = 0; i < values.Count; i++)
{
@@ -123,5 +128,11 @@ namespace FlaxEditor.CustomEditors
_hasReferenceValue = true;
}
}
+
+ ///
+ public override object[] GetAttributes()
+ {
+ return _attributes ?? base.GetAttributes();
+ }
}
}
diff --git a/Source/Editor/GUI/Popups/SceneObjectSearchPopup.cs b/Source/Editor/GUI/Popups/SceneObjectSearchPopup.cs
new file mode 100644
index 000000000..4868dd049
--- /dev/null
+++ b/Source/Editor/GUI/Popups/SceneObjectSearchPopup.cs
@@ -0,0 +1,146 @@
+// Copyright (c) Wojciech Figat. All rights reserved.
+
+using System;
+using FlaxEditor.Windows;
+using FlaxEditor.Windows.Assets;
+using FlaxEngine;
+using FlaxEngine.GUI;
+using FlaxEngine.Utilities;
+
+namespace FlaxEditor.GUI
+{
+ ///
+ /// Popup that shows the list of scene objects to pick. Supports searching and basic type filtering.
+ ///
+ ///
+ public class SceneObjectSearchPopup : ItemsListContextMenu
+ {
+ ///
+ /// The scene object item.
+ ///
+ ///
+ public class SceneObjectItemView : Item
+ {
+ private SceneObject _object;
+
+ ///
+ /// Gets the scene object.
+ ///
+ public SceneObject Object => _object;
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// The object.
+ public SceneObjectItemView(SceneObject obj)
+ {
+ _object = obj;
+ Category = obj is Actor ? "Actors" : "Scripts";
+ if (obj is Script script)
+ {
+ var type = TypeUtils.GetObjectType(script);
+ Name = script.Actor ? $"{type.Name} ({script.Actor.Name})" : type.Name;
+ }
+ else if (obj is Actor actor)
+ {
+ Name = actor.Name;
+ }
+ else
+ {
+ Name = obj.ToString();
+ }
+ TooltipText = Utilities.Utils.GetTooltip(obj);
+ }
+
+ ///
+ public override void OnDestroy()
+ {
+ _object = null;
+ base.OnDestroy();
+ }
+ }
+
+ ///
+ /// Validates if the given scene object item can be used to pick it.
+ ///
+ /// The scene object.
+ /// True if is valid.
+ public delegate bool IsValidDelegate(SceneObject obj);
+
+ private IsValidDelegate _isValid;
+ private Action _selected;
+
+ private SceneObjectSearchPopup(IsValidDelegate isValid, Action selected, CustomEditors.IPresenterOwner context)
+ {
+ _isValid = isValid;
+ _selected = selected;
+
+ ItemClicked += OnItemClicked;
+
+ if (context is PropertiesWindow || context == null)
+ {
+ // TODO: use async thread to search scenes
+ for (int i = 0; i < Level.ScenesCount; i++)
+ {
+ Find(Level.GetScene(i));
+ }
+ }
+ else if (context is PrefabWindow prefabWindow)
+ {
+ Find(prefabWindow.Graph.MainActor);
+ }
+
+ SortItems();
+ }
+
+ private void OnItemClicked(Item item)
+ {
+ _selected(((SceneObjectItemView)item).Object);
+ }
+
+ private void Find(Actor actor)
+ {
+ if (!actor)
+ return;
+
+ if (_isValid(actor))
+ AddItem(new SceneObjectItemView(actor));
+
+ for (int i = 0; i < actor.ScriptsCount; i++)
+ {
+ var script = actor.GetScript(i);
+ if (_isValid(script))
+ AddItem(new SceneObjectItemView(script));
+ }
+
+ for (int i = 0; i < actor.ChildrenCount; i++)
+ {
+ Find(actor.GetChild(i));
+ }
+ }
+
+ ///
+ /// Shows the popup.
+ ///
+ /// The show target.
+ /// The show target location.
+ /// Event called to check if a given scene object item is valid to be used.
+ /// Event called on scene object item pick.
+ /// The presenter owner context (i.e. PrefabWindow, PropertiesWindow).
+ /// The dialog.
+ public static SceneObjectSearchPopup Show(Control showTarget, Float2 showTargetLocation, IsValidDelegate isValid, Action selected, CustomEditors.IPresenterOwner context)
+ {
+ var popup = new SceneObjectSearchPopup(isValid, selected, context);
+ popup.Show(showTarget, showTargetLocation);
+ return popup;
+ }
+
+ ///
+ public override void OnDestroy()
+ {
+ _isValid = null;
+ _selected = null;
+ base.OnDestroy();
+ }
+ }
+}
diff --git a/Source/Engine/Scripting/Attributes/Editor/ScriptingObjectInterfaceReferenceAttribute.cs b/Source/Engine/Scripting/Attributes/Editor/ScriptingObjectInterfaceReferenceAttribute.cs
new file mode 100644
index 000000000..720497b2c
--- /dev/null
+++ b/Source/Engine/Scripting/Attributes/Editor/ScriptingObjectInterfaceReferenceAttribute.cs
@@ -0,0 +1,14 @@
+// Copyright (c) Wojciech Figat. All rights reserved.
+
+using System;
+
+namespace FlaxEngine
+{
+ ///
+ /// Marks a generated interface property as a native scripting object interface reference.
+ ///
+ [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
+ public sealed class ScriptingObjectInterfaceReferenceAttribute : Attribute
+ {
+ }
+}
diff --git a/Source/Engine/Scripting/Attributes/Editor/SoftObjectInterfaceReferenceAttribute.cs b/Source/Engine/Scripting/Attributes/Editor/SoftObjectInterfaceReferenceAttribute.cs
new file mode 100644
index 000000000..ec106412f
--- /dev/null
+++ b/Source/Engine/Scripting/Attributes/Editor/SoftObjectInterfaceReferenceAttribute.cs
@@ -0,0 +1,14 @@
+// Copyright (c) Wojciech Figat. All rights reserved.
+
+using System;
+
+namespace FlaxEngine
+{
+ ///
+ /// Marks a generated interface property as a native soft object interface reference.
+ ///
+ [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
+ public sealed class SoftObjectInterfaceReferenceAttribute : Attribute
+ {
+ }
+}
diff --git a/Source/Engine/Scripting/Internal/InternalCalls.h b/Source/Engine/Scripting/Internal/InternalCalls.h
index 4535952ea..d179fd8fd 100644
--- a/Source/Engine/Scripting/Internal/InternalCalls.h
+++ b/Source/Engine/Scripting/Internal/InternalCalls.h
@@ -37,7 +37,7 @@ struct FLAXENGINE_API VTableFunctionInjector
#if USE_NETCORE
#define ADD_INTERNAL_CALL(fullName, method)
-#define DEFINE_INTERNAL_CALL(returnType) extern "C" DLLEXPORT returnType
+#define DEFINE_INTERNAL_CALL(returnType) extern "C" DLLEXPORT USED returnType
#else
extern "C" FLAXENGINE_API void mono_add_internal_call(const char* name, const void* method);
#define ADD_INTERNAL_CALL(fullName, method) mono_add_internal_call(fullName, (const void*)method)
diff --git a/Source/Engine/Scripting/ManagedCLR/MUtils.h b/Source/Engine/Scripting/ManagedCLR/MUtils.h
index 5598dbee0..aed0c48c5 100644
--- a/Source/Engine/Scripting/ManagedCLR/MUtils.h
+++ b/Source/Engine/Scripting/ManagedCLR/MUtils.h
@@ -278,6 +278,10 @@ struct MConverter::Val
// Converter for ScriptingObject References.
template
class ScriptingObjectReference;
+template
+class ScriptingObjectInterfaceReference;
+template
+class SoftObjectInterfaceReference;
template
struct MConverter>
@@ -311,6 +315,50 @@ struct MConverter>
}
};
+template
+struct MInterfaceReferenceConverter
+{
+ MObject* Box(const TReference& data, const MClass* klass)
+ {
+ return data.GetManagedInstance();
+ }
+
+ void Unbox(TReference& result, MObject* data)
+ {
+ result = ScriptingObject::ToInterface(ScriptingObject::ToNative(data));
+ }
+
+ void ToManagedArray(MArray* result, const Span& data)
+ {
+ if (data.Length() == 0)
+ return;
+ MObject** objects = (MObject**)Allocator::Allocate(data.Length() * sizeof(MObject*));
+ for (int32 i = 0; i < data.Length(); i++)
+ objects[i] = data[i].GetManagedInstance();
+ MCore::GC::WriteArrayRef(result, Span(objects, data.Length()));
+ Allocator::Free(objects);
+ }
+
+ void ToNativeArray(Span& result, const MArray* data)
+ {
+ MObject** dataPtr = MCore::Array::GetAddress(data);
+ for (int32 i = 0; i < result.Length(); i++)
+ result.Get()[i] = ScriptingObject::ToInterface(ScriptingObject::ToNative(dataPtr[i]));
+ }
+};
+
+// Converter for Scripting Interface References.
+template
+struct MConverter> : MInterfaceReferenceConverter, T>
+{
+};
+
+// Converter for Soft Object Interface References.
+template
+struct MConverter> : MInterfaceReferenceConverter, T>
+{
+};
+
// Converter for Asset References.
template
class AssetReference;
diff --git a/Source/Engine/Scripting/ScriptingObject.cpp b/Source/Engine/Scripting/ScriptingObject.cpp
index dee97a849..dd530c50f 100644
--- a/Source/Engine/Scripting/ScriptingObject.cpp
+++ b/Source/Engine/Scripting/ScriptingObject.cpp
@@ -717,6 +717,12 @@ DEFINE_INTERNAL_CALL(MString*) ObjectInternal_GetTypeName(ScriptingObject* obj)
return MUtils::ToString(obj->GetType().Fullname);
}
+FORCE_INLINE bool ObjectInternal_MatchesType(ScriptingObject* obj, MClass* klass)
+{
+ return !klass ||
+ (klass->IsInterface() ? obj->GetClass()->HasInterface(klass) : obj->Is(klass));
+}
+
DEFINE_INTERNAL_CALL(MObject*) ObjectInternal_FindObject(Guid* id, MTypeObject* type, bool skipLog = false)
{
if (!id->IsValid())
@@ -732,7 +738,7 @@ DEFINE_INTERNAL_CALL(MObject*) ObjectInternal_FindObject(Guid* id, MTypeObject*
}
if (obj)
{
- if (klass && !obj->Is(klass))
+ if (!ObjectInternal_MatchesType(obj, klass))
{
if (!skipLog)
{
@@ -762,7 +768,7 @@ DEFINE_INTERNAL_CALL(MObject*) ObjectInternal_FindObject(Guid* id, MTypeObject*
DEFINE_INTERNAL_CALL(MObject*) ObjectInternal_TryFindObject(Guid* id, MTypeObject* type)
{
ScriptingObject* obj = Scripting::TryFindObject(*id);
- if (obj && !obj->Is(MUtils::GetClass(type)))
+ if (obj && !ObjectInternal_MatchesType(obj, MUtils::GetClass(type)))
obj = nullptr;
return obj ? obj->GetOrCreateManagedInstance() : nullptr;
}
diff --git a/Source/Engine/Scripting/ScriptingObjectInterfaceReference.h b/Source/Engine/Scripting/ScriptingObjectInterfaceReference.h
new file mode 100644
index 000000000..65c479075
--- /dev/null
+++ b/Source/Engine/Scripting/ScriptingObjectInterfaceReference.h
@@ -0,0 +1,195 @@
+// Copyright (c) Wojciech Figat. All rights reserved.
+
+#pragma once
+
+#include "Engine/Scripting/ScriptingObjectInterfaceReferenceUtils.h"
+
+///
+/// The scene object interface reference.
+///
+/// The type of the scripting interface.
+template
+API_CLASS(InBuild) class ScriptingObjectInterfaceReference : public ScriptingObjectReferenceBase
+{
+ typedef ScriptingObjectInterfaceReferenceHelper Helper;
+
+public:
+ typedef ScriptingObjectInterfaceReference Type;
+
+public:
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ ScriptingObjectInterfaceReference()
+ {
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// The object to link.
+ ScriptingObjectInterfaceReference(SceneObject* obj)
+ : ScriptingObjectReferenceBase(Helper::IsValidObject(obj) ? obj : nullptr)
+ {
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// The interface object to link.
+ ScriptingObjectInterfaceReference(T* interfaceObj)
+ : ScriptingObjectReferenceBase(Helper::GetSceneObject(interfaceObj))
+ {
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// The other property.
+ ScriptingObjectInterfaceReference(const ScriptingObjectInterfaceReference& other)
+ : ScriptingObjectReferenceBase(other._object)
+ {
+ }
+
+ ScriptingObjectInterfaceReference(ScriptingObjectInterfaceReference&& other) noexcept
+ : ScriptingObjectReferenceBase(MoveTemp(other))
+ {
+ }
+
+ ///
+ /// Finalizes an instance of the class.
+ ///
+ ~ScriptingObjectInterfaceReference()
+ {
+ }
+
+public:
+ FORCE_INLINE bool operator==(SceneObject* other) const
+ {
+ return _object == other;
+ }
+
+ FORCE_INLINE bool operator!=(SceneObject* other) const
+ {
+ return _object != other;
+ }
+
+ FORCE_INLINE bool operator==(T* other) const
+ {
+ return Get() == other;
+ }
+
+ FORCE_INLINE bool operator!=(T* other) const
+ {
+ return Get() != other;
+ }
+
+ FORCE_INLINE bool operator==(const ScriptingObjectInterfaceReference& other) const
+ {
+ return _object == other._object;
+ }
+
+ FORCE_INLINE bool operator!=(const ScriptingObjectInterfaceReference& other) const
+ {
+ return _object != other._object;
+ }
+
+ FORCE_INLINE ScriptingObjectInterfaceReference& operator=(SceneObject* other)
+ {
+ OnSet(Helper::IsValidObject(other) ? other : nullptr);
+ return *this;
+ }
+
+ FORCE_INLINE ScriptingObjectInterfaceReference& operator=(T* other)
+ {
+ OnSet(Helper::GetSceneObject(other));
+ return *this;
+ }
+
+ ScriptingObjectInterfaceReference& operator=(const ScriptingObjectInterfaceReference& other)
+ {
+ OnSet(other._object);
+ return *this;
+ }
+
+ ScriptingObjectInterfaceReference& operator=(ScriptingObjectInterfaceReference&& other) noexcept
+ {
+ ScriptingObjectReferenceBase::operator=(MoveTemp(other));
+ return *this;
+ }
+
+ FORCE_INLINE ScriptingObjectInterfaceReference& operator=(const Guid& id)
+ {
+ OnSet(Helper::FindSceneObject(id));
+ return *this;
+ }
+
+ ///
+ /// Implicit conversion to the interface.
+ ///
+ FORCE_INLINE operator T*() const
+ {
+ return Get();
+ }
+
+ ///
+ /// Implicit conversion to boolean value.
+ ///
+ FORCE_INLINE operator bool() const
+ {
+ return _object != nullptr;
+ }
+
+ ///
+ /// Interface accessor.
+ ///
+ FORCE_INLINE T* operator->() const
+ {
+ return Get();
+ }
+
+ ///
+ /// Gets the interface pointer.
+ ///
+ FORCE_INLINE T* Get() const
+ {
+ return ScriptingObject::ToInterface(_object);
+ }
+
+ ///
+ /// Gets the referenced object.
+ ///
+ FORCE_INLINE SceneObject* GetObject() const
+ {
+ return static_cast(_object);
+ }
+
+ ///
+ /// Copies the object ID into the raw storage.
+ ///
+ FORCE_INLINE void CopyID(uint32 id[4]) const
+ {
+ memset(id, 0, sizeof(uint32) * 4);
+ if (_object)
+ {
+ const Guid value = GetID();
+ memcpy(id, &value, sizeof(uint32) * 4);
+ }
+ }
+
+ ///
+ /// Gets the object as a given type (static cast).
+ ///
+ template
+ FORCE_INLINE U* As() const
+ {
+ return static_cast(_object);
+ }
+
+};
+
+template
+uint32 GetHash(const ScriptingObjectInterfaceReference& key)
+{
+ return GetHash(key.GetID());
+}
diff --git a/Source/Engine/Scripting/ScriptingObjectInterfaceReferenceUtils.h b/Source/Engine/Scripting/ScriptingObjectInterfaceReferenceUtils.h
new file mode 100644
index 000000000..d18df3d80
--- /dev/null
+++ b/Source/Engine/Scripting/ScriptingObjectInterfaceReferenceUtils.h
@@ -0,0 +1,30 @@
+// Copyright (c) Wojciech Figat. All rights reserved.
+
+#pragma once
+
+#include "Engine/Scripting/ScriptingObjectReference.h"
+#include "Engine/Level/SceneObject.h"
+
+///
+/// Utility methods for scene object interface references.
+///
+/// The type of the scripting interface.
+template
+struct ScriptingObjectInterfaceReferenceHelper
+{
+ FORCE_INLINE static bool IsValidObject(const SceneObject* obj)
+ {
+ return !obj || obj->GetType().GetInterface(T::TypeInitializer) != nullptr;
+ }
+
+ FORCE_INLINE static SceneObject* GetSceneObject(T* interfaceObj)
+ {
+ return ScriptingObject::Cast(ScriptingObject::FromInterface(interfaceObj));
+ }
+
+ FORCE_INLINE static SceneObject* FindSceneObject(const Guid& id)
+ {
+ SceneObject* obj = static_cast(FindObject(id, SceneObject::GetStaticClass()));
+ return IsValidObject(obj) ? obj : nullptr;
+ }
+};
diff --git a/Source/Engine/Scripting/SoftObjectInterfaceReference.h b/Source/Engine/Scripting/SoftObjectInterfaceReference.h
new file mode 100644
index 000000000..0814f4927
--- /dev/null
+++ b/Source/Engine/Scripting/SoftObjectInterfaceReference.h
@@ -0,0 +1,258 @@
+// Copyright (c) Wojciech Figat. All rights reserved.
+
+#pragma once
+
+#include "Engine/Scripting/SoftObjectReference.h"
+#include "Engine/Scripting/ScriptingObjectInterfaceReferenceUtils.h"
+
+///
+/// The scene object soft interface reference. Objects gets referenced on use (ID reference is resolving it).
+///
+/// The type of the scripting interface.
+template
+API_CLASS(InBuild) class SoftObjectInterfaceReference : public SoftObjectReferenceBase
+{
+ typedef ScriptingObjectInterfaceReferenceHelper Helper;
+
+public:
+ typedef SoftObjectInterfaceReference Type;
+
+public:
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ SoftObjectInterfaceReference()
+ {
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// The object to link.
+ SoftObjectInterfaceReference(SceneObject* obj)
+ {
+ OnSet(Helper::IsValidObject(obj) ? obj : nullptr);
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// The interface object to link.
+ SoftObjectInterfaceReference(T* interfaceObj)
+ {
+ OnSet(Helper::GetSceneObject(interfaceObj));
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// The other property.
+ SoftObjectInterfaceReference(const SoftObjectInterfaceReference& other)
+ {
+ OnSet(other.GetID());
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// The other property.
+ SoftObjectInterfaceReference(SoftObjectInterfaceReference&& other)
+ {
+ OnSet(other.GetID());
+ other.OnSet(nullptr);
+ }
+
+ ///
+ /// Finalizes an instance of the class.
+ ///
+ ~SoftObjectInterfaceReference()
+ {
+ }
+
+public:
+ FORCE_INLINE bool operator==(SceneObject* other)
+ {
+ return GetObject() == other;
+ }
+
+ FORCE_INLINE bool operator!=(SceneObject* other)
+ {
+ return GetObject() != other;
+ }
+
+ FORCE_INLINE bool operator==(T* other)
+ {
+ return Get() == other;
+ }
+
+ FORCE_INLINE bool operator!=(T* other)
+ {
+ return Get() != other;
+ }
+
+ FORCE_INLINE bool operator==(const SoftObjectInterfaceReference& other)
+ {
+ return GetID() == other.GetID();
+ }
+
+ FORCE_INLINE bool operator!=(const SoftObjectInterfaceReference& other)
+ {
+ return GetID() != other.GetID();
+ }
+
+ SoftObjectInterfaceReference& operator=(const SoftObjectInterfaceReference& other)
+ {
+ if (this != &other)
+ OnSet(other.GetID());
+ return *this;
+ }
+
+ SoftObjectInterfaceReference& operator=(SoftObjectInterfaceReference&& other)
+ {
+ if (this != &other)
+ {
+ OnSet(other.GetID());
+ other.OnSet(nullptr);
+ }
+ return *this;
+ }
+
+ FORCE_INLINE SoftObjectInterfaceReference& operator=(SceneObject* other)
+ {
+ OnSet(Helper::IsValidObject(other) ? other : nullptr);
+ return *this;
+ }
+
+ FORCE_INLINE SoftObjectInterfaceReference& operator=(T* other)
+ {
+ OnSet(Helper::GetSceneObject(other));
+ return *this;
+ }
+
+ FORCE_INLINE SoftObjectInterfaceReference& operator=(const Guid& id)
+ {
+ OnSet(id);
+ return *this;
+ }
+
+ ///
+ /// Implicit conversion to the interface.
+ ///
+ FORCE_INLINE operator T*() const
+ {
+ return Get();
+ }
+
+ ///
+ /// Implicit conversion to boolean value.
+ ///
+ FORCE_INLINE operator bool() const
+ {
+ return Get() != nullptr;
+ }
+
+ ///
+ /// Interface accessor.
+ ///
+ FORCE_INLINE T* operator->() const
+ {
+ return Get();
+ }
+
+ ///
+ /// Gets the object as a given type (static cast).
+ ///
+ template
+ FORCE_INLINE U* As() const
+ {
+ return static_cast(GetObject());
+ }
+
+public:
+ ///
+ /// Gets the interface pointer.
+ ///
+ FORCE_INLINE T* Get() const
+ {
+ return ScriptingObject::ToInterface(GetObject());
+ }
+
+ ///
+ /// Gets the referenced object.
+ ///
+ SceneObject* GetObject() const
+ {
+ if (!_object)
+ const_cast(this)->OnResolve(SceneObject::GetStaticClass());
+ return Helper::IsValidObject(static_cast(_object)) ? static_cast(_object) : nullptr;
+ }
+
+ ///
+ /// Gets managed instance object (or null if no object linked).
+ ///
+ MObject* GetManagedInstance() const
+ {
+ auto object = GetObject();
+ return object ? object->GetOrCreateManagedInstance() : nullptr;
+ }
+
+ ///
+ /// Determines whether object is assigned and managed instance of the object is alive.
+ ///
+ bool HasManagedInstance() const
+ {
+ auto object = GetObject();
+ return object && object->HasManagedInstance();
+ }
+
+ ///
+ /// Gets the managed instance object or creates it if missing or null if not assigned.
+ ///
+ MObject* GetOrCreateManagedInstance() const
+ {
+ auto object = GetObject();
+ return object ? object->GetOrCreateManagedInstance() : nullptr;
+ }
+
+ ///
+ /// Copies the object ID into the raw storage.
+ ///
+ FORCE_INLINE void CopyID(uint32 id[4]) const
+ {
+ const Guid value = GetID();
+ memcpy(id, &value, sizeof(uint32) * 4);
+ }
+
+ ///
+ /// Sets the object.
+ ///
+ /// The object ID. Uses Scripting to find the registered object of the given ID.
+ FORCE_INLINE void Set(const Guid& id)
+ {
+ OnSet(id);
+ }
+
+ ///
+ /// Sets the object.
+ ///
+ /// The object.
+ FORCE_INLINE void Set(SceneObject* object)
+ {
+ OnSet(Helper::IsValidObject(object) ? object : nullptr);
+ }
+
+ ///
+ /// Sets the object.
+ ///
+ /// The interface object.
+ FORCE_INLINE void Set(T* interfaceObj)
+ {
+ OnSet(Helper::GetSceneObject(interfaceObj));
+ }
+};
+
+template
+uint32 GetHash(const SoftObjectInterfaceReference& key)
+{
+ return GetHash(key.GetID());
+}
diff --git a/Source/Engine/Serialization/JsonCustomSerializers/ExtendedDefaultContractResolver.cs b/Source/Engine/Serialization/JsonCustomSerializers/ExtendedDefaultContractResolver.cs
index 4f2690863..84b4729fd 100644
--- a/Source/Engine/Serialization/JsonCustomSerializers/ExtendedDefaultContractResolver.cs
+++ b/Source/Engine/Serialization/JsonCustomSerializers/ExtendedDefaultContractResolver.cs
@@ -13,6 +13,7 @@ namespace FlaxEngine.Json.JsonCustomSerializers
internal class ExtendedDefaultContractResolver : DefaultContractResolver
{
private readonly Type _flaxType = typeof(Object);
+ private static readonly JsonConverter InterfaceObjectReferenceConverterInstance = new InterfaceObjectReferenceConverter();
private readonly Type[] AttributesIgnoreList =
{
@@ -34,6 +35,88 @@ namespace FlaxEngine.Json.JsonCustomSerializers
_attributesIgnoreList = isManagedOnly ? AttributesIgnoreListManaged : AttributesIgnoreList;
}
+ private static bool HasObjectInterfaceReferenceAttribute(IEnumerable attributes)
+ {
+ return attributes.Any(x => x is ScriptingObjectInterfaceReferenceAttribute || x is SoftObjectInterfaceReferenceAttribute);
+ }
+
+ private static Type GetCollectionItemType(Type type)
+ {
+ if (type.IsArray)
+ return type.GetElementType();
+ if (!type.IsGenericType || type == typeof(string))
+ return null;
+
+ var types = type.GetInterfaces().Concat(new[] { type });
+ var dictionaryType = types.FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IDictionary<,>));
+ if (dictionaryType != null)
+ return dictionaryType.GetGenericArguments()[1];
+ var enumerableType = types.FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IEnumerable<>));
+ return enumerableType?.GetGenericArguments()[0];
+ }
+
+ private static void SetupInterfaceObjectReferenceItems(JsonContainerContract contract, Type itemType)
+ {
+ if (itemType?.IsInterface == true)
+ {
+ contract.ItemReferenceLoopHandling = ReferenceLoopHandling.Serialize;
+ contract.ItemConverter = InterfaceObjectReferenceConverterInstance;
+ }
+ }
+
+ private void SetupObjectReferenceProperty(JsonProperty jsonProperty, Type type, IEnumerable attributes)
+ {
+ var hasObjectInterfaceReferenceAttribute = HasObjectInterfaceReferenceAttribute(attributes);
+ if (_flaxType.IsAssignableFrom(type) || (type.IsInterface && hasObjectInterfaceReferenceAttribute))
+ {
+ jsonProperty.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
+ jsonProperty.Converter = JsonSerializer.ObjectConverter;
+ }
+ if (hasObjectInterfaceReferenceAttribute && GetCollectionItemType(type)?.IsInterface == true)
+ {
+ jsonProperty.ItemReferenceLoopHandling = ReferenceLoopHandling.Serialize;
+ jsonProperty.ItemConverter = JsonSerializer.ObjectConverter;
+ }
+ }
+
+ private sealed class InterfaceObjectReferenceConverter : JsonConverter
+ {
+ public override unsafe void WriteJson(JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ if (value is Object obj)
+ {
+ var id = obj.ID;
+ writer.WriteValue(JsonSerializer.GetStringID(&id));
+ }
+ else if (value == null)
+ {
+ writer.WriteNull();
+ }
+ else
+ {
+ serializer.Serialize(writer, value, value.GetType());
+ }
+ }
+
+ public override object ReadJson(JsonReader reader, Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ if (reader.TokenType == JsonToken.String && JsonSerializer.TryParseID((string)reader.Value, out var id))
+ {
+ return Object.Find(ref id, objectType, true);
+ }
+ if (reader.TokenType == JsonToken.Null)
+ return null;
+ // objectType is the same interface item type that selected this converter. Passing it back to
+ // Newtonsoft can cause this converter to be chosen again and recurse until the stack overflows.
+ return Newtonsoft.Json.Linq.JToken.Load(reader).ToObject