How would I copy AudioSource curves?

Hi, I made a custom editor for AudioSource where I can reference another AudioSource and any changes made are transfered to that reference.

In order to actually making these changes I’m using the code for copying component I found here in the forum:

public static T CopyComponent<T>(T original, GameObject destination) where T : Component {
        Type type = original.GetType();
        Component existing = destination.GetComponent(type);
        if (existing != null)
            DestroyImmediate(existing);
        Component copy = destination.AddComponent(type);
        BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Default | BindingFlags.DeclaredOnly;
        PropertyInfo[] properties = type.GetProperties(flags);
        FieldInfo[] fields = type.GetFields(flags);
        foreach (PropertyInfo property in properties) {
            if (property.Name == "minVolume" || property.Name == "maxVolume" || property.Name == "rolloffFactor" || property.Name == "name")
                continue;
            try {
                property.SetValue(copy, property.GetValue(original, null), null);
            }
            catch { }
        }
        foreach (FieldInfo field in fields)
            field.SetValue(copy, field.GetValue(original));
        return copy as T;
    }

The problem appears to be that AudioSource curves are neither properties or fields so I dont know how I could copy them.

Duplicating the object is not an option.

While I haven’t tried it, I image that a good first place to start would be to call GetCustomCurve()/SetCustomCurve() with the AudioSourceCurveType enums that interest you.

1 Like

Thanks! That worked, although I had to make a custom copy method just for AudioSources