1
1
// NOTE: managedReferenceValue getter is available only in Unity 2021.3 or later.
2
2
#if UNITY_2021_3_OR_NEWER
3
+ using System ;
3
4
using UnityEditor ;
4
5
using UnityEngine ;
5
6
6
7
namespace MackySoft . SerializeReferenceExtensions . Editor
7
8
{
8
- public static class CopyAndPasteProperty
9
+ public static class ManagedReferenceContextualPropertyMenu
9
10
{
10
11
11
12
const string kCopiedPropertyPathKey = "SerializeReferenceExtensions.CopiedPropertyPath" ;
12
13
const string kClipboardKey = "SerializeReferenceExtensions.CopyAndPasteProperty" ;
13
14
14
15
static readonly GUIContent kPasteContent = new GUIContent ( "Paste Property" ) ;
16
+ static readonly GUIContent kNewInstanceContent = new GUIContent ( "New Instance" ) ;
17
+ static readonly GUIContent kResetAndNewInstanceContent = new GUIContent ( "Reset and New Instance" ) ;
15
18
16
19
[ InitializeOnLoadMethod ]
17
20
static void Initialize ( )
@@ -38,6 +41,20 @@ static void OnContextualPropertyMenu (GenericMenu menu, SerializedProperty prope
38
41
{
39
42
menu . AddDisabledItem ( kPasteContent ) ;
40
43
}
44
+
45
+ menu . AddSeparator ( "" ) ;
46
+
47
+ bool hasInstance = clonedProperty . managedReferenceValue != null ;
48
+ if ( hasInstance )
49
+ {
50
+ menu . AddItem ( kNewInstanceContent , false , NewInstance , clonedProperty ) ;
51
+ menu . AddItem ( kResetAndNewInstanceContent , false , ResetAndNewInstance , clonedProperty ) ;
52
+ }
53
+ else
54
+ {
55
+ menu . AddDisabledItem ( kNewInstanceContent ) ;
56
+ menu . AddDisabledItem ( kResetAndNewInstanceContent ) ;
57
+ }
41
58
}
42
59
}
43
60
@@ -62,6 +79,29 @@ static void Paste (object customData)
62
79
JsonUtility . FromJsonOverwrite ( json , property . managedReferenceValue ) ;
63
80
property . serializedObject . ApplyModifiedProperties ( ) ;
64
81
}
82
+
83
+ static void NewInstance ( object customData )
84
+ {
85
+ SerializedProperty property = ( SerializedProperty ) customData ;
86
+ string json = JsonUtility . ToJson ( property . managedReferenceValue ) ;
87
+
88
+ Undo . RecordObject ( property . serializedObject . targetObject , "New Instance" ) ;
89
+ property . managedReferenceValue = JsonUtility . FromJson ( json , property . managedReferenceValue . GetType ( ) ) ;
90
+ property . serializedObject . ApplyModifiedProperties ( ) ;
91
+
92
+ Debug . Log ( $ "Create new instance of \" { property . propertyPath } \" .") ;
93
+ }
94
+
95
+ static void ResetAndNewInstance ( object customData )
96
+ {
97
+ SerializedProperty property = ( SerializedProperty ) customData ;
98
+
99
+ Undo . RecordObject ( property . serializedObject . targetObject , "Reset and New Instance" ) ;
100
+ property . managedReferenceValue = Activator . CreateInstance ( property . managedReferenceValue . GetType ( ) ) ;
101
+ property . serializedObject . ApplyModifiedProperties ( ) ;
102
+
103
+ Debug . Log ( $ "Reset property and created new instance of \" { property . propertyPath } \" .") ;
104
+ }
65
105
}
66
106
}
67
107
#endif
0 commit comments