Skip to content

Commit 46c88b5

Browse files
authored
Merge pull request #144 from arimger/develop
Develop - 0.14.2
2 parents 8c7112e + 1d68a42 commit 46c88b5

30 files changed

+510
-117
lines changed

Assets/Editor Toolbox/CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
## 0.14.2 [10.08.2025]
2+
3+
### Added:
4+
- `AddConfirmationBox` property to the [ReferencePicker]; allows to display an intermediate confirmation window before assigning a new reference
5+
6+
### Changed:
7+
- Fix [FormattedNumber] behaviour while multi-editing different values
8+
- [LabelByChild] now displays ToString() value for UnityEngine.Object references
9+
- Fix ReorderableLists indentation level for nested objects
10+
- Fix various smaller issues related to [SerializeReference]-based properties while being in the multi-editing mode
11+
- Rename 'Copy Serialize Reference' to 'Copy Serialized References', from now this operation also works on arrays and lists
12+
- Rename 'Paste Serialize Reference' to 'Paste Serialized References', from now this operation also works on arrays and lists
13+
114
## 0.14.1 [22.04.2025]
215

316
### Changed:

Assets/Editor Toolbox/Editor/ContextMenu/Management/ToolboxContextMenuManager.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ static ToolboxContextMenuManager()
1515
{
1616
registeredOperations = new List<IContextMenuOperation>()
1717
{
18+
#if UNITY_2021_3_OR_NEWER
1819
new CopySerializeReferenceOperation(),
1920
new PasteSerializeReferenceOperation(),
2021
new DuplicateSerializeReferenceArrayElementOperation()
22+
#endif
2123
};
2224

2325
EditorApplication.contextualPropertyMenu -= OnContextMenuOpening;
Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
using System;
2+
using System.Collections.Generic;
23

34
namespace Toolbox.Editor.ContextMenu.Operations
45
{
6+
/// <summary>
7+
/// Cache created by the copy operation.
8+
/// All properties are based on data from the source <see cref="UnityEditor.SerializedProperty"/>.
9+
/// </summary>
510
internal class CopySerializeReferenceCache
611
{
7-
public CopySerializeReferenceCache(Type referenceType, string data)
12+
public CopySerializeReferenceCache(Type referenceType, IReadOnlyList<CopySerializeReferenceEntry> entires)
813
{
914
ReferenceType = referenceType;
10-
Data = data;
15+
Entries = entires;
1116
}
1217

18+
/// <summary>
19+
/// Base managed reference type of the source <see cref="UnityEditor.SerializedProperty"/>.
20+
/// </summary>
1321
public Type ReferenceType { get; }
14-
public string Data { get; }
22+
public IReadOnlyList<CopySerializeReferenceEntry> Entries { get; }
23+
public bool IsArrayCopy => Entries != null && Entries.Count > 1;
1524
}
1625
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
3+
namespace Toolbox.Editor.ContextMenu.Operations
4+
{
5+
internal class CopySerializeReferenceEntry
6+
{
7+
public CopySerializeReferenceEntry(Type referenceType, string referenceData)
8+
{
9+
ReferenceType = referenceType;
10+
ReferenceData = referenceData;
11+
}
12+
13+
public Type ReferenceType { get; }
14+
public string ReferenceData { get; }
15+
}
16+
}

Assets/Editor Toolbox/Editor/ContextMenu/Operations/SerializeReference/CopySerializeReferenceEntry.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using UnityEditor;
1+
#if UNITY_2021_3_OR_NEWER
2+
using System.Collections.Generic;
3+
4+
using UnityEditor;
25
using UnityEngine;
36

47
namespace Toolbox.Editor.ContextMenu.Operations
@@ -9,40 +12,67 @@ internal class CopySerializeReferenceOperation : IContextMenuOperation
912

1013
[InitializeOnLoadMethod]
1114
private static void Initialize()
15+
{
16+
Reset();
17+
}
18+
19+
private CopySerializeReferenceEntry CreateEntry(SerializedProperty property)
20+
{
21+
if (property == null)
22+
{
23+
return new CopySerializeReferenceEntry(null, null);
24+
}
25+
26+
var value = property.managedReferenceValue;
27+
if (value == null)
28+
{
29+
return new CopySerializeReferenceEntry(null, null);
30+
}
31+
32+
var referenceType = value.GetType();
33+
var data = JsonUtility.ToJson(value);
34+
return new CopySerializeReferenceEntry(referenceType, data);
35+
}
36+
37+
internal static void Reset()
1238
{
1339
Cache = null;
1440
}
1541

1642
public bool IsVisible(SerializedProperty property)
1743
{
18-
#if UNITY_2021_3_OR_NEWER
19-
return property != null && property.propertyType == SerializedPropertyType.ManagedReference;
20-
#else
21-
return false;
22-
#endif
44+
return PropertyUtility.IsSerializeReferenceProperty(property);
2345
}
2446

2547
public bool IsEnabled(SerializedProperty property)
2648
{
27-
return true;
49+
return property.isArray ? property.arraySize > 0 : true;
2850
}
2951

3052
public void Perform(SerializedProperty property)
3153
{
32-
#if UNITY_2021_3_OR_NEWER
33-
var value = property.managedReferenceValue;
34-
if (value != null)
54+
var entries = new List<CopySerializeReferenceEntry>();
55+
if (property.propertyType == SerializedPropertyType.ManagedReference)
56+
{
57+
var entry = CreateEntry(property);
58+
entries.Add(entry);
59+
}
60+
else if (property.isArray)
3561
{
36-
var referenceType = value.GetType();
37-
var data = JsonUtility.ToJson(value);
38-
Cache = new CopySerializeReferenceCache(referenceType, data);
39-
return;
62+
var propertiesCount = property.arraySize;
63+
for (var i = 0; i < propertiesCount; i++)
64+
{
65+
var childProperty = property.GetArrayElementAtIndex(i);
66+
var entry = CreateEntry(childProperty);
67+
entries.Add(entry);
68+
}
4069
}
4170

42-
Cache = new CopySerializeReferenceCache(null, null);
43-
#endif
71+
PropertyUtility.TryGetSerializeReferenceType(property, out var referenceType);
72+
Cache = new CopySerializeReferenceCache(referenceType, entries);
4473
}
4574

46-
public GUIContent Label => new GUIContent("Copy Serialize Reference");
75+
public GUIContent Label => new GUIContent("Copy Serialized References");
4776
}
48-
}
77+
}
78+
#endif

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using UnityEditor;
1+
#if UNITY_2021_3_OR_NEWER
2+
using UnityEditor;
23
using UnityEngine;
34

45
namespace Toolbox.Editor.ContextMenu.Operations
@@ -7,12 +8,8 @@ internal class DuplicateSerializeReferenceArrayElementOperation : IContextMenuOp
78
{
89
public bool IsVisible(SerializedProperty property)
910
{
10-
#if UNITY_2021_3_OR_NEWER
1111
return property != null && property.propertyType == SerializedPropertyType.ManagedReference &&
1212
PropertyUtility.IsSerializableArrayElement(property);
13-
#else
14-
return false;
15-
#endif
1613
}
1714

1815
public bool IsEnabled(SerializedProperty property)
@@ -22,7 +19,6 @@ public bool IsEnabled(SerializedProperty property)
2219

2320
public void Perform(SerializedProperty property)
2421
{
25-
#if UNITY_2021_3_OR_NEWER
2622
var sourceProperty = property.Copy();
2723
sourceProperty.serializedObject.Update();
2824
var sourceValue = sourceProperty.managedReferenceValue;
@@ -40,9 +36,9 @@ public void Perform(SerializedProperty property)
4036
}
4137

4238
sourceProperty.serializedObject.ApplyModifiedProperties();
43-
#endif
4439
}
4540

4641
public GUIContent Label => new GUIContent("Duplicate Serialize Reference Array Element");
4742
}
48-
}
43+
}
44+
#endif
Lines changed: 81 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System;
1+
#if UNITY_2021_3_OR_NEWER
2+
using System;
3+
using System.Collections.Generic;
24

35
using UnityEditor;
46
using UnityEngine;
@@ -7,34 +9,67 @@ namespace Toolbox.Editor.ContextMenu.Operations
79
{
810
internal class PasteSerializeReferenceOperation : IContextMenuOperation
911
{
10-
private bool IsAssignmentValid(SerializedProperty property, object newValue)
12+
private bool IsAssignmentValid(SerializedProperty targetProperty, IReadOnlyList<CopySerializeReferenceEntry> entires)
1113
{
12-
#if UNITY_2021_3_OR_NEWER
13-
if (newValue == null)
14+
if (!PropertyUtility.TryGetSerializeReferenceType(targetProperty, out var targetType))
15+
{
16+
return false;
17+
}
18+
19+
for (var i = 0; i < entires.Count; i++)
20+
{
21+
var entry = entires[i];
22+
if (!IsAssignmentValid(targetType, entry))
23+
{
24+
return false;
25+
}
26+
}
27+
28+
return true;
29+
}
30+
31+
private bool IsAssignmentValid(Type targetType, CopySerializeReferenceEntry entry)
32+
{
33+
var entryType = entry.ReferenceType;
34+
if (entryType == null)
1435
{
1536
return true;
1637
}
1738

18-
if (!TypeUtility.TryGetTypeFromManagedReferenceFullTypeName(property.managedReferenceFieldTypename, out var referenceType))
39+
return TypeUtility.IsTypeAssignableFrom(targetType, entry.ReferenceType);
40+
}
41+
42+
private bool IsOperationSupported(SerializedProperty targetProperty, CopySerializeReferenceCache cache)
43+
{
44+
if (cache == null)
45+
{
46+
return false;
47+
}
48+
49+
var entries = cache.Entries;
50+
if (entries == null || entries.Count == 0)
51+
{
52+
return false;
53+
}
54+
55+
if (cache.IsArrayCopy && targetProperty.isArray)
1956
{
2057
return true;
2158
}
2259

23-
var newValueType = newValue.GetType();
24-
if (TypeUtility.IsTypeAssignableFrom(referenceType, newValueType))
60+
if (!cache.IsArrayCopy && !targetProperty.isArray)
2561
{
2662
return true;
2763
}
28-
#endif
64+
2965
return false;
3066
}
3167

32-
private object GetCachedManagedReferenceValue()
68+
private object GetManagedReferenceValue(CopySerializeReferenceEntry entry)
3369
{
34-
var cachedData = CopySerializeReferenceOperation.Cache;
35-
if (cachedData.ReferenceType != null)
70+
if (entry.ReferenceType != null)
3671
{
37-
var newValue = JsonUtility.FromJson(cachedData.Data, cachedData.ReferenceType);
72+
var newValue = JsonUtility.FromJson(entry.ReferenceData, entry.ReferenceType);
3873
return newValue;
3974
}
4075
else
@@ -43,42 +78,60 @@ private object GetCachedManagedReferenceValue()
4378
}
4479
}
4580

81+
private void PasteEntry(SerializedProperty targetProperty, CopySerializeReferenceEntry entry)
82+
{
83+
var newValue = GetManagedReferenceValue(entry);
84+
targetProperty.managedReferenceValue = newValue;
85+
}
86+
4687
public bool IsVisible(SerializedProperty property)
4788
{
48-
#if UNITY_2021_3_OR_NEWER
49-
return property != null && property.propertyType == SerializedPropertyType.ManagedReference;
50-
#else
51-
return false;
52-
#endif
89+
return PropertyUtility.IsSerializeReferenceProperty(property);
5390
}
5491

5592
public bool IsEnabled(SerializedProperty property)
5693
{
57-
return CopySerializeReferenceOperation.Cache != null;
94+
return IsOperationSupported(property, CopySerializeReferenceOperation.Cache);
5895
}
5996

6097
public void Perform(SerializedProperty property)
6198
{
62-
#if UNITY_2019_3_OR_NEWER
99+
var cache = CopySerializeReferenceOperation.Cache;
100+
var entries = cache.Entries;
101+
if (!IsAssignmentValid(property, entries))
102+
{
103+
ToolboxEditorLog.LogWarning("Cannot perform paste operation, types are mismatched.");
104+
return;
105+
}
106+
63107
var targetProperty = property.Copy();
64108
try
65109
{
66-
var newValue = GetCachedManagedReferenceValue();
67-
if (!IsAssignmentValid(targetProperty, newValue))
110+
targetProperty.serializedObject.Update();
111+
if (targetProperty.isArray)
112+
{
113+
var arraySize = entries.Count;
114+
targetProperty.arraySize = arraySize;
115+
for (var i = 0; i < arraySize; i++)
116+
{
117+
var entry = entries[i];
118+
var childProperty = targetProperty.GetArrayElementAtIndex(i);
119+
PasteEntry(childProperty, entry);
120+
}
121+
}
122+
else
68123
{
69-
ToolboxEditorLog.LogWarning("Cannot perform paste operation, types are mismatched.");
70-
return;
124+
var entry = entries[0];
125+
PasteEntry(targetProperty, entry);
71126
}
72127

73-
targetProperty.serializedObject.Update();
74-
targetProperty.managedReferenceValue = newValue;
75128
targetProperty.serializedObject.ApplyModifiedProperties();
76129
}
77130
catch (Exception)
78131
{ }
79-
#endif
80132
}
81133

82-
public GUIContent Label => new GUIContent("Paste Serialize Reference");
134+
public GUIContent Label => new GUIContent("Paste Serialized References");
83135
}
84-
}
136+
}
137+
#endif

0 commit comments

Comments
 (0)