# PrefabUtility.EditPrefabContentsScope

> Disposable helper struct for automatically loading the contents of a Prefab file, saving the contents and unloading the contents again.

## Definition

* **Type:** Struct
* **Namespace:** [UnityEditor](/engine/6000.6/script-reference/unityeditor.md)
* **Assembly:** UnityEditor.CoreModule
* **Implements:** [IDisposable](https://learn.microsoft.com/dotnet/api/system.idisposable)

```csharp
public struct PrefabUtility.EditPrefabContentsScope : IDisposable
```

## Remarks

This struct allows you to temporarily load the GameObject contents of a Prefab file, modify the contents to your liking inside of a block of code and automatically save the result back to the Prefab file as well as unload the temporary contents when the scope is exited.

This scope struct wraps the API’s [PrefabUtility.LoadPrefabContents](/engine/6000.6/script-reference/unityeditor/prefabutility/loadprefabcontents.md), [PrefabUtility.SaveAsPrefabAsset](/engine/6000.6/script-reference/unityeditor/prefabutility/saveasprefabasset.md) and [PrefabUtility.UnloadPrefabContents](/engine/6000.6/script-reference/unityeditor/prefabutility/unloadprefabcontents.md).

## Examples

```csharp
using UnityEditor;
using UnityEngine;

public static class PrefabUtilityTesting
{
    [MenuItem("Prefabs/Test_EditPrefabContentsScope")]
    public static void Test()
    {
        // Create a simple test Prefab Asset. Looks like this:
        // Root
        //    A
        //    B
        //    C
        var assetPath = "Assets/MyTempPrefab.prefab";
        var source = new GameObject("Root");
        var childA = new GameObject("A");
        var childB = new GameObject("B");
        var childC = new GameObject("C");
        childA.transform.parent = source.transform;
        childB.transform.parent = source.transform;
        childC.transform.parent = source.transform;
        PrefabUtility.SaveAsPrefabAsset(source, assetPath);

        using (var editingScope = new PrefabUtility.EditPrefabContentsScope(assetPath))
        {
            var prefabRoot = editingScope.prefabContentsRoot;

            // Removing GameObjects is supported
            Object.DestroyImmediate(prefabRoot.transform.GetChild(2).gameObject);

            // Reordering and reparenting are supported
            prefabRoot.transform.GetChild(1).parent = prefabRoot.transform.GetChild(0);

            // Adding GameObjects is supported
            var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
            cube.transform.parent = prefabRoot.transform;
            cube.name = "D";

            // Adding and removing components are supported
            prefabRoot.AddComponent<AudioSource>();
        }

        // Prefab Asset now looks like this:
        // Root
        //    A
        //       B
        //    D
    }
}
```

## Fields

| Value                                                                                                                         | Description                                 |
| ----------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------- |
| [assetPath](/engine/6000.6/script-reference/unityeditor/prefabutility/editprefabcontentsscope/assetpath.md)                   | File path of the Prefab asset.              |
| [prefabContentsRoot](/engine/6000.6/script-reference/unityeditor/prefabutility/editprefabcontentsscope/prefabcontentsroot.md) | The root GameObject of the Prefab contents. |
