Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Copy & Paste Component Or Asset Fields

Discussion in 'Editor & General Support' started by OrdinaryDev83, Jul 30, 2020.

  1. OrdinaryDev83

    OrdinaryDev83

    Joined:
    May 8, 2014
    Posts:
    23
    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 :

    Code (CSharp):
    1.  #if UNITY_EDITOR
    2. using UnityEngine;
    3. using UnityEditor;
    4. public static class ComponentUtil
    5. {
    6.      static SerializedObject source;
    7.      [MenuItem("CONTEXT/Component/CopySerialized")]
    8.      public static void CopySerializedFromBase(MenuCommand command)
    9.      { source = new SerializedObject(command.context); }
    10.      [MenuItem("CONTEXT/Component/PasteSerialized")]
    11.      public static void PasteSerializedFromBase(MenuCommand command)
    12.      {
    13.          SerializedObject dest = new SerializedObject(command.context);
    14.          SerializedProperty prop_iterator = source.GetIterator();
    15.          //jump into serialized object, this will skip script type so that we dont override the destination component's type
    16.          if (prop_iterator.NextVisible(true))
    17.          {
    18.              while (prop_iterator.NextVisible(true)) //itterate through all serializedProperties
    19.              {
    20.                  //try obtaining the property in destination component
    21.                  SerializedProperty prop_element = dest.FindProperty(prop_iterator.name);
    22.                  //validate that the properties are present in both components, and that they're the same type
    23.                  if (prop_element != null && prop_element.propertyType == prop_iterator.propertyType)
    24.                  {
    25.                      //copy value from source to destination component
    26.                      dest.CopyFromSerializedProperty(prop_iterator);
    27.                  }
    28.              }
    29.          }
    30.          dest.ApplyModifiedProperties();
    31.      }
    32. }
    33. #endif
    From : http://answers.unity.com/answers/1538287/view.html

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

    Code (CSharp):
    1. #if UNITY_EDITOR
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System.Security.Cryptography;
    5.  
    6. public static class ComponentUtil
    7. {
    8.      static SerializedObject source;
    9.      [MenuItem("CONTEXT/Object/Copy Serialized Fields %#&c")]
    10.      public static void CopySerializedFromBase(MenuCommand command)
    11.      {
    12.         source = new SerializedObject(command.context);
    13.     }
    14.     [MenuItem("CONTEXT/Object/Paste Serialized Fields %#&v")]
    15.     public static void PasteSerializedFromBase(MenuCommand command)
    16.      {
    17.          SerializedObject dest = new SerializedObject(command.context);
    18.          SerializedProperty prop_iterator = source.GetIterator();
    19.         //jump into serialized object, this will skip script type so that we dont override the destination component's type
    20.         if (prop_iterator.NextVisible(true))
    21.          {
    22.              while (prop_iterator.Next(true)) //itterate through all serializedProperties
    23.              {
    24.                 //try obtaining the property in destination component
    25.                 SerializedProperty prop_element = dest.FindProperty(prop_iterator.name);
    26.                  //validate that the properties are present in both components, and that they're the same type
    27.                  if (prop_element != null && prop_element.propertyType == prop_iterator.propertyType)
    28.                  {
    29.                      //copy value from source to destination component
    30.                      dest.CopyFromSerializedProperty(prop_iterator);
    31.                  }
    32.              }
    33.          }
    34.          dest.ApplyModifiedProperties();
    35.      }
    36. }
    37. #endif
    38.  
    from : OrdinaryDev83
    Hope it helps!
     
    Last edited: Jul 30, 2020
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    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:
    upload_2020-7-30_9-59-34.png

    Then I paste values onto another component:

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

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

    OrdinaryDev83

    Joined:
    May 8, 2014
    Posts:
    23
    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.
     
    Last edited: Jul 30, 2020