Copy & Paste Component Or Asset Fields

I thought I would share it, because it’s very useful, for components in the scene or assets. (e.g : ruletiles or/and child class ruletiles)
Theses scripts can copy and paste properties through different classes of the same inheritance group (base / parent class).
Component Copy & Paste :

 #if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
public static class ComponentUtil
{
     static SerializedObject source;
     [MenuItem("CONTEXT/Component/CopySerialized")]
     public static void CopySerializedFromBase(MenuCommand command)
     { source = new SerializedObject(command.context); }
     [MenuItem("CONTEXT/Component/PasteSerialized")]
     public static void PasteSerializedFromBase(MenuCommand command)
     {
         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

From : http://answers.unity.com/answers/1538287/view.html

I adapted this code to match with assets:
Asset Copy & Paste :

#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
using System.Security.Cryptography;

public static class ComponentUtil
{
     static SerializedObject source;
     [MenuItem("CONTEXT/Object/Copy Serialized Fields %#&c")]
     public static void CopySerializedFromBase(MenuCommand command)
     {
        source = new SerializedObject(command.context);
    }
    [MenuItem("CONTEXT/Object/Paste Serialized Fields %#&v")]
    public static void PasteSerializedFromBase(MenuCommand command)
     {
         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.Next(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

from : OrdinaryDev83
Hope it helps!

2 Likes

First, sharing code doesn’t really work too well here. This post will just kind of fall off the first few pages quickly, and then no one is likely to see it. Maybe try one of the other sub-forums? Or, if you really want to share some features, consider putting something on the asset store as a free utility.

Second, I’m not clear on what your script does, and how it’s different from the built-in “paste component values” that I can already do on components:

First I copy:
6150721--671914--upload_2020-7-30_9-59-34.png

Then I paste values onto another component:

6150721--671917--upload_2020-7-30_10-0-30.png

And everything’s all wired up. Does your code do something different that I’m missing?

Hello, firstly sorry for being unclear here : this code can copy and paste from a base class to its child class or the reverse. It does work for components and also for assets, your example is restricted to the exact same class.
Secondly, maybe you didn’t have the problem yourself, but composing scripts by inheritance and associating it with assets or prefabs can lead to some values maybe redondance which can be solved quickly by my scripts.
Edit : I made it clear in my first message now.

Thanks for sharing, exactly what I was looking for :wink: