1

I am currently working on a room editor. The room is a base prefab with scripts. It has a child transform with tiles, which is empty.

In my editor, I instantiate a base prefab room, and design whatever tiles should be in it, and save it as a prefab variant, so only variants are populated with tiles.

This was all good and dandy, until I came to the part where I would like to make deletion a part of my editor. Since I get this diamond

InvalidOperationException: Destroying a GameObject inside a Prefab instance is not allowed. UnityEngine.Object.DestroyImmediate (UnityEngine.Object obj)

So the obvious solution seem to use
PrefabUtility.UnpackPrefabInstance(room, PrefabUnpackMode.OutermostRoot, InteractionMode.AutomatedAction);

After I instantiate my room, and it seems to work... Until I save the prefab:

    public static void SaveRoomPrefab(GameObject room, string regionName)
    {
        string directoryPath = Path.Combine($"{RoomsPrefabPath}/", regionName);

        // Create the directory if it doesn't exist
        if (!Directory.Exists(directoryPath))
        {
            Directory.CreateDirectory(directoryPath);
        }

        // Construct the prefab path
        string prefabPath = Path.Combine(directoryPath, room.name + ".prefab");

        // If a prefab with the same name already exists, ask the user if they want to replace it
        if (File.Exists(prefabPath))
        {
            if (!EditorUtility.DisplayDialog("Prefab Already Exists",
                    $"A prefab with the name {room.name} already exists in the {regionName} directory. Do you want to replace it?",
                    "Yes", "No"))
            {
                return;
            }
        }

        // Save the room as a prefab variant
        PrefabUtility.SaveAsPrefabAssetAndConnect(room, prefabPath, InteractionMode.UserAction);
    }

now what happens is that it does no longer acknowledge my room is a Prefab, and effectively overrides the base prefab rather than save to a new variant... :(

I feel like I am at my wits end with this issue.

        private void AttemptDeleteTile()
    {
        Vector3 newPosition = tileEditorInputComponent.NewTilePosition;
        Tile existingTile = GetExistingTileAtNewPosition(newPosition);

        SelectTile();
        if (currentTile != null && existingTile)
        {
            GameObject previousTileObject = currentTile.gameObject;
    
            Object prefabInstanceHandle = PrefabUtility.GetPrefabInstanceHandle(previousTileObject);
            if (PrefabUtility.IsPartOfPrefabInstance(prefabInstanceHandle))
            {
                PrefabUtility.RevertPrefabInstance(previousTileObject, InteractionMode.AutomatedAction);
                PrefabUtility.RevertAddedGameObject(previousTileObject, InteractionMode.AutomatedAction);
            }
            else
            {
                Object.DestroyImmediate(previousTileObject);
            }
        }
    }

This is the code I am trying to make work. So to sum up, I want an editor that can load and modify prefab variants I've created and save them back again. Unpacking the variant to just be a prefab seems impossible, even if in the editor it definitely looks like its a prefab, the "PrefabUtility.IsPartOfModelPrefab(room)" is false when I try to save it after I've unpacked it.

Any nifty ideas to work around this?

derHugo
  • 83,094
  • 9
  • 75
  • 115
Ava
  • 502
  • 1
  • 6
  • 21

0 Answers0