Skip to content

Commit b5cfa6f

Browse files
committed
Implement basic support for generic types while using SerializeReference; more unit tests covering types filtering & constraints; add warning when pasting invalid/mismatched reference; update samples
1 parent db56817 commit b5cfa6f

43 files changed

Lines changed: 687 additions & 437 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Assets/Editor Toolbox/Editor/ContextMenu/Operations/SerializeReference/PasteSerializeReferenceOperation.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,29 @@
55

66
namespace Toolbox.Editor.ContextMenu.Operations
77
{
8-
98
internal class PasteSerializeReferenceOperation : IContextMenuOperation
109
{
10+
private bool IsAssignmentValid(SerializedProperty property, object newValue)
11+
{
12+
if (newValue == null)
13+
{
14+
return true;
15+
}
16+
17+
if (!TypeUtility.TryGetTypeFromManagedReferenceFullTypeName(property.managedReferenceFieldTypename, out var referenceType))
18+
{
19+
return true;
20+
}
21+
22+
var newValueType = newValue.GetType();
23+
if (TypeUtility.IsTypeAssignableFrom(referenceType, newValueType))
24+
{
25+
return true;
26+
}
27+
28+
return false;
29+
}
30+
1131
private object GetCachedManagedReferenceValue()
1232
{
1333
var cachedData = CopySerializeReferenceOperation.Cache;
@@ -42,8 +62,15 @@ public void Perform(SerializedProperty property)
4262
var targetProperty = property.Copy();
4363
try
4464
{
65+
var newValue = GetCachedManagedReferenceValue();
66+
if (!IsAssignmentValid(targetProperty, newValue))
67+
{
68+
ToolboxEditorLog.LogWarning("Cannot perform paste operation, types are mismatched.");
69+
return;
70+
}
71+
4572
targetProperty.serializedObject.Update();
46-
targetProperty.managedReferenceValue = GetCachedManagedReferenceValue();
73+
targetProperty.managedReferenceValue = newValue;
4774
targetProperty.serializedObject.ApplyModifiedProperties();
4875
}
4976
catch (Exception)

Assets/Editor Toolbox/Editor/Drawers/Regular/SerializedTypeDrawer.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace Toolbox.Editor.Drawers
77
{
8-
using Toolbox.Editor.Internal;
8+
using Toolbox.Editor.Internal.Types;
99

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

18-
1918
private bool IsDefaultField(TypeConstraintAttribute attribute)
2019
{
2120
return attribute == null || attribute.AssemblyType == null;
@@ -65,7 +64,6 @@ private void UpdateAppearance(TypeConstraintAttribute attribute)
6564
sharedAppearance.TypeGrouping = attribute.TypeGrouping;
6665
}
6766

68-
6967
protected override float GetPropertyHeightSafe(SerializedProperty property, GUIContent label)
7068
{
7169
return EditorStyles.popup.CalcHeight(GUIContent.none, 0);
@@ -101,7 +99,6 @@ protected override void OnGUISafe(Rect position, SerializedProperty property, GU
10199
EditorGUI.EndProperty();
102100
}
103101

104-
105102
///<inheritdoc/>
106103
public override bool IsPropertyValid(SerializedProperty property)
107104
{

Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelf/ReferencePickerAttributeDrawer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Toolbox.Editor.Drawers
88
{
99
using Toolbox.Editor.Internal;
10+
using Toolbox.Editor.Internal.Types;
1011

1112
public class ReferencePickerAttributeDrawer : ToolboxSelfPropertyDrawer<ReferencePickerAttribute>
1213
{
@@ -42,7 +43,7 @@ private Type GetParentType(ReferencePickerAttribute attribute, SerializedPropert
4243

4344
private void CreateTypeProperty(SerializedProperty property, Type parentType, ReferencePickerAttribute attribute, Rect position)
4445
{
45-
TypeUtilities.TryGetTypeFromManagedReferenceFullTypeName(property.managedReferenceFullTypename, out var currentType);
46+
TypeUtility.TryGetTypeFromManagedReferenceFullTypeName(property.managedReferenceFullTypename, out var currentType);
4647
typeField.OnGui(position, attribute.AddTextSearchField, (type) =>
4748
{
4849
try

Assets/Editor Toolbox/Editor/Internal/TypeAppearanceContext.cs.meta

Lines changed: 0 additions & 11 deletions
This file was deleted.

Assets/Editor Toolbox/Editor/Internal/TypeConstraintContext.cs.meta

Lines changed: 0 additions & 11 deletions
This file was deleted.

Assets/Editor Toolbox/Editor/Internal/TypeConstraintSerializeReference.cs.meta

Lines changed: 0 additions & 11 deletions
This file was deleted.

Assets/Editor Toolbox/Editor/Internal/TypeConstraintStandard.cs.meta

Lines changed: 0 additions & 11 deletions
This file was deleted.

Assets/Editor Toolbox/Editor/Internal/TypeField.cs.meta

Lines changed: 0 additions & 11 deletions
This file was deleted.

Assets/Editor Toolbox/Editor/Internal/Types.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Editor Toolbox/Editor/Internal/TypeAppearanceContext.cs renamed to Assets/Editor Toolbox/Editor/Internal/Types/TypeAppearanceContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
using UnityEngine;
44

5-
namespace Toolbox.Editor.Internal
5+
namespace Toolbox.Editor.Internal.Types
66
{
77
public class TypeAppearanceContext
88
{

0 commit comments

Comments
 (0)