- Home /
Copy / Paste As New not copying deep
I've got this class...
[System.Serializable]
public class LoopConfig
{
public AudioClip Clip = null;
public AnimationCurve Envelope = new AnimationCurve();
public float Gain = 1.0f;
}
I've got this other class that contains one of those...
public class CrossfadedLoopConfig : ScriptableObject
{
public LoopConfig LoopSettings = null;
public AnimationCurve Fader = new AnimationCurve();
}
I;ve got this MonoBehaviour...
public class LoopCrossfader : MonoBehaviour
{
public float Gain = 1.0f;
public float XFadePosition = 0.0f;
public List<CrossfadedLoopConfig> LooperSettings = new List<CrossfadedLoopConfig>();
void Start()
{
...
}
blah, blah, blah
}
I've got a custom editor for this component, and it works great. Here is my problem...
If I right click on the component's name and select "Copy Component", and then right click again on the component's name and select "Past Component as New"... I don't get a deep copy.
The new (pasted) component's list of looper settings "points to" the original component's looper settings. So, here is an example...
If I have an instance of this component with 2 items in the LooperSettings list, and I copy and paste, and then I edit the first item in the original component instance's list of looper settings, I see those changes in the pasted component instance as well.
So... is there any way for me to get the paste to do a deep copy?
Thanks, Buzz
I am currently trying to figure out the solution to this problem as well. Changing the values of a copied/pasted component changes the values on the other component which means they are pointing to the same variables rather than creating new one - which you would expect it would do when using 'paste component as new'.
Would it make sense for you to convert the CrossfadedLoopConfig class to $$anonymous$$onoBehavior, and attach it to the same object which has the LoopCrossfader? If you did this, duplicating the game object ins$$anonymous$$d of the component will give you duplicates of each config component.
What you're really asking is why won't Unity create new CrossfadedLoopConfigs for you when you paste the component values. The reason is because the values aren't "part of" the LoopCrossfader component, only the references are (and those are what gets copied).
Answer by Wanzyee · Jan 08, 2016 at 06:04 PM
Maybe, you can create a new list from the original one in script.
void Awake(){
LooperSettings = new List<CrossfadedLoopConfig>(LooperSettings);
}
Try to do that when Awake. it's called after the new MonoBehaviour being instantiated, just make sure the GameObject active. Or, implement ISerializationCallbackReceiver with OnAfterDeserialize.