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+ }
0 commit comments