Skip to content

Commit

Permalink
Implement basic support for generic types while using SerializeRefere…
Browse files Browse the repository at this point in the history
…nce; more unit tests covering types filtering & constraints; add warning when pasting invalid/mismatched reference; update samples
  • Loading branch information
arimger committed Aug 28, 2024
1 parent db56817 commit 5ab6559
Show file tree
Hide file tree
Showing 44 changed files with 862 additions and 576 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,29 @@

namespace Toolbox.Editor.ContextMenu.Operations
{

internal class PasteSerializeReferenceOperation : IContextMenuOperation
{
private bool IsAssignmentValid(SerializedProperty property, object newValue)
{
if (newValue == null)
{
return true;
}

if (!TypeUtility.TryGetTypeFromManagedReferenceFullTypeName(property.managedReferenceFieldTypename, out var referenceType))
{
return true;
}

var newValueType = newValue.GetType();
if (TypeUtility.IsTypeAssignableFrom(referenceType, newValueType))
{
return true;
}

return false;
}

private object GetCachedManagedReferenceValue()
{
var cachedData = CopySerializeReferenceOperation.Cache;
Expand Down Expand Up @@ -42,8 +62,15 @@ public void Perform(SerializedProperty property)
var targetProperty = property.Copy();
try
{
var newValue = GetCachedManagedReferenceValue();
if (!IsAssignmentValid(targetProperty, newValue))
{
ToolboxEditorLog.LogWarning("Cannot perform paste operation, types are mismatched.");
return;
}

targetProperty.serializedObject.Update();
targetProperty.managedReferenceValue = GetCachedManagedReferenceValue();
targetProperty.managedReferenceValue = newValue;
targetProperty.serializedObject.ApplyModifiedProperties();
}
catch (Exception)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Toolbox.Editor.Drawers
{
using Toolbox.Editor.Internal;
using Toolbox.Editor.Internal.Types;

[CustomPropertyDrawer(typeof(TypeConstraintAttribute), true)]
[CustomPropertyDrawer(typeof(SerializedType))]
Expand All @@ -15,7 +15,6 @@ public sealed class SerializedTypeDrawer : PropertyDrawerBase
private static readonly TypeAppearanceContext sharedAppearance = new TypeAppearanceContext(sharedConstraint, TypeGrouping.None, true);
private static readonly TypeField typeField = new TypeField(sharedConstraint, sharedAppearance);


private bool IsDefaultField(TypeConstraintAttribute attribute)
{
return attribute == null || attribute.AssemblyType == null;
Expand Down Expand Up @@ -65,7 +64,6 @@ private void UpdateAppearance(TypeConstraintAttribute attribute)
sharedAppearance.TypeGrouping = attribute.TypeGrouping;
}


protected override float GetPropertyHeightSafe(SerializedProperty property, GUIContent label)
{
return EditorStyles.popup.CalcHeight(GUIContent.none, 0);
Expand Down Expand Up @@ -101,7 +99,6 @@ protected override void OnGUISafe(Rect position, SerializedProperty property, GU
EditorGUI.EndProperty();
}


///<inheritdoc/>
public override bool IsPropertyValid(SerializedProperty property)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Toolbox.Editor.Drawers
{
using Toolbox.Editor.Internal;
using Toolbox.Editor.Internal.Types;

public class ReferencePickerAttributeDrawer : ToolboxSelfPropertyDrawer<ReferencePickerAttribute>
{
Expand Down Expand Up @@ -42,7 +43,7 @@ private Type GetParentType(ReferencePickerAttribute attribute, SerializedPropert

private void CreateTypeProperty(SerializedProperty property, Type parentType, ReferencePickerAttribute attribute, Rect position)
{
TypeUtilities.TryGetTypeFromManagedReferenceFullTypeName(property.managedReferenceFullTypename, out var currentType);
TypeUtility.TryGetTypeFromManagedReferenceFullTypeName(property.managedReferenceFullTypename, out var currentType);
typeField.OnGui(position, attribute.AddTextSearchField, (type) =>
{
try
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

11 changes: 0 additions & 11 deletions Assets/Editor Toolbox/Editor/Internal/TypeField.cs.meta

This file was deleted.

8 changes: 8 additions & 0 deletions Assets/Editor Toolbox/Editor/Internal/Types.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

using UnityEngine;

namespace Toolbox.Editor.Internal
namespace Toolbox.Editor.Internal.Types
{
public class TypeAppearanceContext
{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;

namespace Toolbox.Editor.Internal
namespace Toolbox.Editor.Internal.Types
{
public class TypeConstraintContext
{
Expand All @@ -14,13 +14,7 @@ public TypeConstraintContext(Type targetType)

public virtual bool IsSatisfied(Type type)
{
#if UNITY_2019_2_OR_NEWER
return type.IsVisible;
#else
return type.IsVisible && (targetType.IsGenericType
? targetType.IsAssignableFromGeneric(type)
: targetType.IsAssignableFrom(type));
#endif
}

public virtual void ApplyTarget(Type type)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using Object = UnityEngine.Object;

namespace Toolbox.Editor.Internal
namespace Toolbox.Editor.Internal.Types
{
/// <summary>
/// Dedicated <see cref="TypeConstraintContext"/> for SerializeReference-based types.
Expand All @@ -15,6 +15,13 @@ public TypeConstraintSerializeReference(Type targetType) : base(targetType)

public override bool IsSatisfied(Type type)
{
//NOTE: generic types are not supported below Unity 2023 while using the Serialize References
#if !UNITY_2023_2_OR_NEWER
if (type.IsGenericType)
{
return false;
}
#endif
return base.IsSatisfied(type) &&
!type.IsInterface &&
!type.IsAbstract &&
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

using UnityEngine;

namespace Toolbox.Editor.Internal
namespace Toolbox.Editor.Internal.Types
{
public class TypeConstraintStandard : TypeConstraintContext
{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System;
using System.Collections.Generic;

using UnityEditor;
using UnityEngine;

namespace Toolbox.Editor.Internal
namespace Toolbox.Editor.Internal.Types
{
/// <summary>
/// Out-of-the-box field that can be used as a <see cref="Type"/> picker.
Expand Down Expand Up @@ -58,7 +59,7 @@ public void OnGui(Rect position, bool addSearchField, Action<Type> onSelect, Typ
{
var addEmptyValue = AppearanceContext.AddEmptyValue;

var collection = TypeUtilities.GetCollection(AppearanceContext);
var collection = TypeUtility.GetCollection(AppearanceContext);
var values = collection.Values;
var labels = collection.Labels;
var index = collection.IndexOf(activeType);
Expand Down
2 changes: 2 additions & 0 deletions Assets/Editor Toolbox/Editor/Internal/Types/TypeField.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
using System.Collections;
using System.Collections.Generic;

namespace Toolbox.Editor.Internal
namespace Toolbox.Editor.Internal.Types
{
public class TypesCachedCollection : IEnumerable<Type>
{
private readonly List<Type> values;


public TypesCachedCollection() : this(new List<Type>())
{ }

Expand All @@ -17,7 +16,6 @@ public TypesCachedCollection(List<Type> values)
this.values = values;
}


public virtual int IndexOf(Type type)
{
return values.IndexOf(type);
Expand All @@ -38,7 +36,6 @@ IEnumerator IEnumerable.GetEnumerator()
return values.GetEnumerator();
}


public IReadOnlyList<Type> Values => values;

public static implicit operator List<Type>(TypesCachedCollection collection) => collection.values;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5ab6559

Please sign in to comment.