Skip to content

Instantly share code, notes, and snippets.

@karljj1
Last active February 11, 2021 18:25
Show Gist options
  • Save karljj1/ccace256567c5a32c0422064d51d311b to your computer and use it in GitHub Desktop.
Save karljj1/ccace256567c5a32c0422064d51d311b to your computer and use it in GitHub Desktop.
using System;
using UnityEditor;
public class CopyPasteValues
{
const string kBufferName = "json-copy-buffer";
const string kTypeName = "json-copy-type";
[MenuItem("CONTEXT/Component/Copy Values With Json")]
static void Copy(MenuCommand command)
{
SessionState.SetString(kTypeName, command.context.GetType().AssemblyQualifiedName);
SessionState.SetString(kBufferName, EditorJsonUtility.ToJson(command.context));
}
[MenuItem("CONTEXT/Component/Paste Values With Json")]
static void Paste(MenuCommand command)
{
var json = SessionState.GetString(kBufferName, string.Empty);
EditorJsonUtility.FromJsonOverwrite(json, command.context);
}
[MenuItem("CONTEXT/Component/Paste Values With Json", validate = true)]
static bool PasteValidate(MenuCommand command)
{
var json = SessionState.GetString(kBufferName, string.Empty);
var copyType = Type.GetType(SessionState.GetString(kTypeName, string.Empty), false);
var type = command.context.GetType();
return !string.IsNullOrEmpty(json) && type != null && copyType.IsAssignableFrom(type);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment