Serialization of Unity Generated Scripts, Modifying Methods

Im trying to modify the ARCameraBackground script that is on the camera by default in the AR template project. Every time I modify it and save it, it reverts back to the original. I believe this is due to serialization. According to the serialization docs, to use field serialization you must ensure that the field

  • Is public, or has a SerializeField attribute
  • Is not static
  • Is not const
  • Is not readonly
  • Has a field type that can be serialized

If I make a regular public field in the class that meets these requirements, it disappears after saving. It only stays if I put [SerializeField] above it. This contradicts the docs. Also I dont see anything about methods. When I try to modify a method it reverts back. Am I overlooking something obvious?

Doesn't sound like serialization.

Are you sure these aren't in a package?

If they're in a package they will revert always if changed.

To be honest, I am not sure. It might be in AR Foundation? But like I said, it stays if I put [SerializeField] above it. To re-create this you can simply make a new project with the AR template and look at the scripts on the camera in the scene.

Does the field get removed from your code, or does a value you assign in the Inspector get reset to the default?

If your code is being changed, it's not serialization. It does sound like you're changing a package and it's being reverted.

1 Like

On further testing, the field is removed if it has a value assigned to it and when I build.
If it is in a package, whats the best way to modify its functionality?

No, I guess you just drawn the wrong conclusion. You just haven’t figured out which actions actually trigger the revert of the script which belongs to a package. As it was already mentioned by others, the actual script code is not “serialized”.

I never really done anything with AR. I guess you talk about this class? If that’s the case, as you can see, it’s part of a package, so you should not edit it at all. What exactly do you want to change anyways? Can’t that be a separate script? They declared the class as a “sealed” class, so they don’t even want you to derive any class from that class. This can have several reasons and could even mean that some other engine code may depend on a particular implementation.

Well, as I said, you shouldn’t. Again, can you explain what kind of changes you talk about?


Thats odd, it doesnt say sealed when I look at it in my project. Im using unity 2022.3.13.
public class ARCameraBackground : MonoBehaviour

Im trying to access the textures in eventArgs.textures.

void OnCameraFrameReceived(ARCameraFrameEventArgs eventArgs)
    var activeRenderingMode = currentRenderingMode;

    // Enable background rendering when first frame is received.
    if (m_BackgroundRenderingEnabled)
        if (eventArgs.textures.Count == 0 || activeRenderingMode == XRCameraBackgroundRenderingMode.None)

        if (m_CommandBuffer != null)
            if (m_CommandBufferRenderOrderState != activeRenderingMode)



            else if (m_CommandBufferCullingState != shouldInvertCulling)
    else if (eventArgs.textures.Count > 0 && activeRenderingMode != XRCameraBackgroundRenderingMode.None)

    Material material = this.material;
    if (material != null)
        var count = eventArgs.textures.Count;
        for (int i = 0; i < count; ++i)
            material.SetTexture(eventArgs.propertyNameIds[i], eventArgs.textures[i]);

        if (eventArgs.displayMatrix.HasValue)
            material.SetMatrix(k_DisplayTransformId, eventArgs.displayMatrix.Value);

        SetMaterialKeywords(material, eventArgs.enabledMaterialKeywords, eventArgs.disabledMaterialKeywords);

    if (eventArgs.projectionMatrix.HasValue)
        camera.projectionMatrix = eventArgs.projectionMatrix.Value;
        const float twiceRad2Deg = 2 * Mathf.Rad2Deg;
        var halfHeightOverNear = 1 / camera.projectionMatrix[1, 1];
        camera.fieldOfView = Mathf.Atan(halfHeightOverNear) * twiceRad2Deg;

Just create your own version, copy all the code from the original script, modify it at will, and use it? or check how they are getting this event from, subscribe to it, and receive it too?