Skip to content

Commit 76f8e53

Browse files
committed
Basic implementation of custom Context Menus for SerializedProperties
1 parent 56edeff commit 76f8e53

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

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

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

0 commit comments

Comments
 (0)