Skip to content

Commit 351ee5d

Browse files
committed
Basic script responsible for duplicating [SerializeReference]-based fields [WIP]
1 parent 263e1df commit 351ee5d

3 files changed

Lines changed: 66 additions & 2 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
4+
namespace Toolbox.Editor.Internal
5+
{
6+
[InitializeOnLoad]
7+
public static class CopySerializeReferenceUtility
8+
{
9+
static CopySerializeReferenceUtility()
10+
{
11+
propertyToCopy = null;
12+
EditorApplication.contextualPropertyMenu -= OnContextMenuOpening;
13+
EditorApplication.contextualPropertyMenu += OnContextMenuOpening;
14+
}
15+
16+
private static SerializedProperty propertyToCopy;
17+
18+
private static void OnContextMenuOpening(GenericMenu menu, SerializedProperty property)
19+
{
20+
if (property.propertyType != SerializedPropertyType.ManagedReference ||
21+
!PropertyUtility.IsSerializableArrayElement(property))
22+
{
23+
return;
24+
}
25+
26+
menu.AddItem(new GUIContent("Copy Reference"), false, () =>
27+
{
28+
propertyToCopy = property;
29+
});
30+
menu.AddItem(new GUIContent("Duplicate Reference"), false, () =>
31+
{
32+
var parent = property.GetParent();
33+
parent.arraySize++;
34+
parent.serializedObject.ApplyModifiedProperties();
35+
var newProperty = parent.GetArrayElementAtIndex(parent.arraySize - 1);
36+
});
37+
if (propertyToCopy != null)
38+
{
39+
menu.AddItem(new GUIContent("Paste Reference"), false, () =>
40+
{
41+
property.serializedObject.Update();
42+
//property.managedReferenceValue = null;
43+
property.serializedObject.ApplyModifiedProperties();
44+
propertyToCopy = null;
45+
});
46+
}
47+
else
48+
{
49+
menu.AddDisabledItem(new GUIContent("Paste Reference"));
50+
}
51+
}
52+
}
53+
}

Assets/Editor Toolbox/Editor/Internal/CopySerializeReferenceUtility.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.

Assets/Editor Toolbox/Editor/Internal/ReorderableListBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ private void DoDraggingAndSelection()
141141

142142
//don't allow arrowing through the ends of the list
143143
Index = Mathf.Clamp(Index, 0, List.arraySize - 1);
144-
145144
}
146145

147146
break;
@@ -474,7 +473,8 @@ public void AppendElement()
474473
var property = List.GetArrayElementAtIndex(Index);
475474
var newValue = overrideNewElementCallback(Index);
476475
//update property directly by the reflection
477-
property.SetProperValue(property.GetFieldInfo(), newValue, false);
476+
var fieldInfo = property.GetFieldInfo();
477+
property.SetProperValue(fieldInfo, newValue, false);
478478
}
479479

480480
public void RemoveElement()

0 commit comments

Comments
 (0)