Skip to content

Instantly share code, notes, and snippets.

@andybak
Last active August 5, 2018 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andybak/e8b15841a94b3df4ff4af56774e85370 to your computer and use it in GitHub Desktop.
Save andybak/e8b15841a94b3df4ff4af56774e85370 to your computer and use it in GitHub Desktop.
// https://www.reddit.com/r/Unity3D/comments/94rpgc/i_wrote_a_piece_of_code_that_allows_for/
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
public static class ComponentUtil
{
static SerializedObject source;
[MenuItem("CONTEXT/Component/Copy Serialized")]
public static void CopySerialized(MenuCommand command)
{ source = new SerializedObject(command.context); }
[MenuItem("CONTEXT/Component/Paste Serialized Values")]
public static void PasteSerialized(MenuCommand command)
{
//check if they're the same type - if so do the ordinary copy/paste
if(source.targetObject.GetType() == command.context.GetType())
{
EditorUtility.CopySerialized(source.targetObject, command.context);
return;
}
SerializedObject dest = new SerializedObject(command.context);
SerializedProperty prop_iterator = source.GetIterator();
//jump into serialized object, this will skip script type so that we dont override the destination component's type
if (prop_iterator.NextVisible(true))
{
while (prop_iterator.NextVisible(true)) //itterate through all serializedProperties
{
//try obtaining the property in destination component
SerializedProperty prop_element = dest.FindProperty(prop_iterator.name);
//validate that the properties are present in both components, and that they're the same type
if (prop_element != null && prop_element.propertyType == prop_iterator.propertyType)
{
//copy value from source to destination component
dest.CopyFromSerializedProperty(prop_iterator);
}
}
}
dest.ApplyModifiedProperties();
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment