-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from mackysoft/feature/copy-paste-property
Implement copy and paste property
- Loading branch information
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/CopyAndPasteProperty.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#if UNITY_2019_3_OR_NEWER | ||
using UnityEditor; | ||
using UnityEngine; | ||
|
||
namespace MackySoft.SerializeReferenceExtensions.Editor | ||
{ | ||
public static class CopyAndPasteProperty | ||
{ | ||
|
||
const string kCopiedPropertyPathKey = "SerializeReferenceExtensions.CopiedPropertyPath"; | ||
const string kClipboardKey = "SerializeReferenceExtensions.CopyAndPasteProperty"; | ||
|
||
static readonly GUIContent kPasteContent = new GUIContent("Paste Property"); | ||
|
||
[InitializeOnLoadMethod] | ||
static void Initialize () | ||
{ | ||
EditorApplication.contextualPropertyMenu += OnContextualPropertyMenu; | ||
} | ||
|
||
static void OnContextualPropertyMenu (GenericMenu menu, SerializedProperty property) | ||
{ | ||
if (property.propertyType == SerializedPropertyType.ManagedReference) | ||
{ | ||
// NOTE: When the callback function is called, the SerializedProperty is rewritten to the property that was being moused over at the time, | ||
// so a new SerializedProperty instance must be created. | ||
SerializedProperty clonedProperty = property.Copy(); | ||
|
||
menu.AddItem(new GUIContent($"Copy \"{property.propertyPath}\" property"), false, Copy, clonedProperty); | ||
|
||
string copiedPropertyPath = SessionState.GetString(kCopiedPropertyPathKey, string.Empty); | ||
if (!string.IsNullOrEmpty(copiedPropertyPath)) | ||
{ | ||
menu.AddItem(new GUIContent($"Paste \"{copiedPropertyPath}\" property"), false, Paste, clonedProperty); | ||
} | ||
else | ||
{ | ||
menu.AddDisabledItem(kPasteContent); | ||
} | ||
} | ||
} | ||
|
||
static void Copy (object customData) | ||
{ | ||
SerializedProperty property = (SerializedProperty)customData; | ||
string json = JsonUtility.ToJson(property.managedReferenceValue); | ||
SessionState.SetString(kCopiedPropertyPathKey, property.propertyPath); | ||
SessionState.SetString(kClipboardKey, json); | ||
} | ||
|
||
static void Paste (object customData) | ||
{ | ||
SerializedProperty property = (SerializedProperty)customData; | ||
string json = SessionState.GetString(kClipboardKey, string.Empty); | ||
if (string.IsNullOrEmpty(json)) | ||
{ | ||
return; | ||
} | ||
|
||
Undo.RecordObject(property.serializedObject.targetObject, "Paste Property"); | ||
JsonUtility.FromJsonOverwrite(json, property.managedReferenceValue); | ||
property.serializedObject.ApplyModifiedProperties(); | ||
} | ||
} | ||
} | ||
#endif |
11 changes: 11 additions & 0 deletions
11
Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/CopyAndPasteProperty.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.